OpenCV3 圖像讀取和保存 ( imread() 和 imwrite() )

目錄

HighGUI模塊

cv::imread()

CV::imwrite()


 

HighGUI模塊

OpenCV把用於操作系統、文件系統以及攝像機等硬件設備交互的函數納入到HighGUI(High-level Graphical User Interface)模塊中 。有了HighGUI模塊,對於窗口打開、顯示圖像、讀出或者寫入圖像相關的文件(包含圖像和視頻)、處理簡單的鼠標點擊、鼠標移動和鍵盤事件,創建一些有用的控件,eg:滑動條,並將其融入窗口之中。

HighGUI庫可以劃分三個部分:硬件相關部分、文件系統部分、圖形用戶界面。

cv::imread()

/*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 \*.pxm, \*.pnm (always supported)
-   Sun rasters - \*.sr, \*.ras (always supported)
-   TIFF files - \*.tiff, \*.tif (see the *Notes* section)
-   OpenEXR Image files - \*.exr (see the *Notes* section)
-   Radiance HDR - \*.hdr, \*.pic (always supported)
-   Raster and Vector geospatial data supported by Gdal (see the *Notes* section)

@note

-   The function determines the type of an image by the content, not by the file extension.
-   In the case of color images, the decoded images will have the channels stored in **B G R** order.
-   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.
-   In the case you set *WITH_GDAL* flag to true in CMake and @ref IMREAD_LOAD_GDAL to load the image,
    then [GDAL](http://www.gdal.org) driver will be used in order to decode the image by supporting
    the following formats: [Raster](http://www.gdal.org/formats_list.html),
    [Vector](http://www.gdal.org/ogr_formats.html).
-   If EXIF information are embedded in the image file, the EXIF orientation will be taken into account
    and thus the image will be rotated accordingly except if the flag @ref IMREAD_IGNORE_ORIENTATION is passed.
@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes
*/
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );

cv::imread()讀取一張圖片時,cv::imread()並不關心文件的擴展名是什麼,而是分析文件中前幾個字節(被稱爲文件的識別標識或者“魔法序列”)來確定圖像的編碼格式,第二個參數flags可以設置爲一下的格式。

{
       IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
       IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image.
       IMREAD_COLOR                = 1,  //!< If set, always convert image to the 3 channel BGR color image.
       IMREAD_ANYDEPTH             = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
       IMREAD_ANYCOLOR             = 4,  //!< If set, the image is read in any possible color format.
       IMREAD_LOAD_GDAL            = 8,  //!< If set, use the gdal driver for loading the image.
       IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
       IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
       IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
       IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
       IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
       IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
       IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
}

使用cv::imread()讀取一張圖片時,並不關心文件的擴展名是什麼,而是分析文件中前幾個字節(被稱爲文件識別標識或者“魔法序列”)來確定圖像的編碼格式。

第二個參數flags可以被設置爲上述表格中的任意一個值,默認情況下,flag被設置爲cv::IMREAD_COLOR。這個值表示圖片將被以三通道8位的格式讀取。在這種情況下即使原圖像是灰度圖像,讀到內存中仍然有三通道,每個通道擁有相同數據的圖像,如果flags被設置爲cv::IMREAD_GRAYSCALE,那麼不管文件內部圖像是幾通道,圖像都將以灰度圖的格式加載。當flags是cv::IMREAD_ANYCOLOR。在此種情況下,圖片的載入方式取決於其內部的圖像的情況,如果是彩色圖像的就以三通道的形式讀取,如果是灰度圖,則按單通道的形式載入。 

cv::IMREAD_UNCHANGED具有另一個獨特的效果:讀取圖像的時候,它將保留圖像中的alpha通道。

cv::IMREAD_ANYDEPTH標誌,表明輸入的通道數據大於8位,使得圖像載入過程不會發生數據類型轉換(分配用於存儲數據的數組類型將會由其內部數據類型決定)。

cv::imread()在載入圖像失敗時,它不會拋出異常,而是僅返回一空的cv::Mat,可以使用cv::Mat::empty()==true來判斷。

CV::imwrite()

imwrite()函數與cv::imread()函數形成功能互補關係,輸入爲三個參數,如下所示

bool cv::imwrite
( const string& filename,
  InputArray img,
  const vector<int>& params=vector<int>()
);

第一個參數給定了文件名,文件名的擴展部分用來決定以何鍾格式保存圖像。

第二個參數是待存儲的輸入圖像。第三個參數被用作特殊類型文件的寫入操作時所需要的數據。輸入參數爲內部整型數據的一個STL vector中的整型序列的具體內容爲:一系列的參數ID,以及與該參數對應的參數值,每一個參數ID之後跟着其對應的值。

    IMWRITE_JPEG_QUALITY =1,
    IMWRITE_PNG_COMPRESSION =16,
    IMWRITE_PNG_STRATEGY =17,
    IMWRITE_PNG_BILEVEL =18,
    IMWRITE_PNG_STRATEGY_DEFAULT =0,
    IMWRITE_PNG_STRATEGY_FILTERED =1,
    IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY =2,
    IMWRITE_PNG_STRATEGY_RLE =3,
    IMWRITE_PNG_STRATEGY_FIXED =4,
    IMWRITE_PXM_BINARY =32
標誌 含義 取值範圍 默認值
IMWRITE_JPEG_QUALITY JPEG的質量 0~100 95
IMWRITE_PNG_COMPRESSION  PNG壓縮值(更高意味着更多的壓縮) 0~9 3
IMWRITE_PXM_BINARY  對PPM,PGM或PBM文件是否使用二值格式 0或1 1

cv::imwrite()函數會將大多數格式的圖像以8位單通道或多通道形式保存,但對於像PNG、TIFF或者JPG2000這些非常靈活的文件格式,則允許以16位甚至是浮點數格式保存,有些可以保存4通道(BGRA),如果成功保存則返回true,否則通常會返回false(有些會報異常)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章