1. 程式人生 > >【OpenCV筆記 01】OpenCV基本函式介紹

【OpenCV筆記 01】OpenCV基本函式介紹

本文主要介紹OpenCV基本函式,包括imread(), imshow(), namedWindow(), imwrite(),函式功能分別對應影象載入,影象顯示,建立視窗,輸出影象到檔案

1.函式imread(),用於影象的載入。

1.1函式原型
 Mat cv::imread
( const String &  filename, 
int  flags = IMREAD_COLOR   
)

備註:第一個引數為文件名第二個引數flag 為int型別的載入標識,指定載入影象的顏色型別 

1.2引數列表(用於引數說明)
Parameters
filename Name of file to be loaded. 
flags Flags specifying the color type of a loaded image:
  • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
  • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
  • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
  • **>0** Return a 3-channel color image.

備註:如果flag>0,返回一個三通道的彩色影象,flag=0返回一個灰度影象,flag<0,返回包含Alpha通道的載入影象
用法舉例:
Mat image1 = imread("try,jpg", 2 | 4);//載入無損的原影象
Mat image2 = imread("try,jpg", 0);//載入灰度影象
Mat image3 = imread("try,jpg", 199);//載入三通道彩色影象


Note
In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.
  • **=0** Return a grayscale image.
  • **<0** Return the loaded image as is (with alpha channel).

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ). Currently, the following file formats are supported:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files - *.jp2 (see the Notes section)
  • Portable Network Graphics - *.png (see the Notes section)
  • WebP - *.webp (see the Notes section)
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)
    備註:imread函式支援以上格式的影象載入,有比較常用bmp,jpg,png,tiff等。
Note
  • The function determines the type of an image by the content, not by the file extension.
  • On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.
  • On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, "libjpeg-dev", in Debian* and Ubuntu*) to get the codec support or turn on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
Note
In the case of color images, the decoded images will have the channels stored in B G R order.
備註:在彩色影象的條件下,解碼影象將會有RGB三色通道的儲存資訊

訪問路徑:

【Main modules】》》【imgcodecs. Image file reading and writing】》》【Function】》》【imread()】