opencv 筆記 00Begin

Load and Display an Image

Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );                   // Show our image inside it.

You’ll almost always end up using the:

  • core section, as here are defined the basic building blocks of the library
  • highgui module, as this contains the functions for input and output operations

imread function which loads the image name specified by the first argument (argv[1]). The second argument specifies the format in what we want the image. This may be:

  • CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
  • CV_LOAD_IMAGE_GRAYSCALE ( 0) loads the image as an intensity one
  • CV_LOAD_IMAGE_COLOR (>0) loads the image in the RGB format

namedWindow ,For this you need to specify its name and how it should handle the change of the image it contains from a size point of view. It may be:

  • CV_WINDOW_AUTOSIZE is the only supported one if you do not use the Qt backend. In this case the window size will take up the size of the image it shows. No resize permitted!
  • CV_WINDOW_NORMAL on Qt you may use this to allow window resize. The image will resize itself according to the current window size. By using the | operator you also need to specify if you would like the image to keep its aspect ratio (CV_WINDOW_KEEPRATIO) or not (CV_WINDOW_FREERATIO).

imshow function. Specify the OpenCV window name to update and the image to use during this operation:

讀取、修改、保存圖像

Mat image;
image = imread( imageName, CV_LOAD_IMAGE_COLOR);
Mat gray_image;
cvtColor( image, gray_image, CV_BGR2GRAY );
imwrite( "../../images/Gray_Image.jpg", gray_image );

cvtColor 的參數爲:

  • 源圖像 (image) 。
  • 目標圖像 (gray_image),用於保存轉換圖像。
  • 附加參數,用於指定轉換的類型
 imwrite該函數,將圖像寫入到指定的文件夾下,程序執行時需保證該文件夾存在


發佈了23 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章