Opencv -- image data acquisition and storage (imread() and imwrite() functions)

Posted by mrjiggyhill on Sun, 19 Jan 2020 05:05:34 +0100

When cv::imread() is used to read a picture, it doesn't care what the extension of the file is, but analyzes the first few bytes in the file (known as the file identification ID or "magic sequence") to determine the encoding format of the image.

The second parameter flags can be set to any value in the above table. By default, flag is set to cv::IMREAD_COLOR. This value indicates that the image will be read in three channel 8-bit format. In this case, even if the original image is a gray-scale image, there are still three channels in memory. Each channel has an image with the same data. If flags is set to CV:: imread? Gray, the image will be loaded in the gray-scale image format no matter how many channels the internal image of the file is. When flags is CV:: imread? Anycolor. In this case, the loading mode of the picture depends on the internal image. If it is a color image, it will be read in the form of three channels. If it is a gray-scale image, it will be loaded in the form of a single channel.

CV:: imread? Unchanged has another unique effect: when reading an image, it will retain the alpha channel in the image.

The CV:: imread ﹣ anydepth flag indicates that the input channel data is greater than 8 bits, so that no data type conversion will occur during image loading (the array type allocated for data storage will be determined by its internal data type).

When cv::imread() fails to load the image, it will not throw an exception, but only return a null cv::Mat, which can be judged by cv::Mat::empty()==true.

{
       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::imwrite()
The imwrite() function and the cv::imread() function form a functional complementary relationship. The input is three parameters, as shown below

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

The first parameter gives the file name, and the extension of the file name is used to decide what format to save the image.

The second parameter is the input image to be stored.
The third parameter is used as the data required for write operations of special types of files. The specific content of the integer sequence in an STL vector whose input parameter is internal integer data is: a series of parameter IDs and the corresponding parameter values of the parameter. Each parameter ID is followed by its corresponding value.

    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

The cv::imwrite() function will save most images in 8-bit single channel or multi-channel format, but for very flexible file formats such as PNG, TIFF or JPG2000, it is allowed to save them in 16 bit or even floating-point format. Some of them can save 4-channel (BGRA). If they are saved successfully, they will return true, otherwise they will usually return false (some will report exceptions).

https://blog.csdn.net/u012785169/article/details/96974928
https://blog.csdn.net/mao_hui_fei/article/details/78413268

Published 15 original articles, praised 0, visited 4593
Private letter follow

Topics: encoding