Opencv save image and read image

content

One, store the image

1, store imwrite

2. Encoding format

3. Picture bit depth

Second, read the image

1, read imread

2, read type ImreadModes


One, store the image

1, store imwrite

imwrite("D:/im2.jpg", image2);

The third parameter is defaulted.

2. Encoding format

jpg is lossy compression, png is lossless compression, if you have high requirements for pictures, it is better to use png.

3. Picture bit depth

32F type pictures, after saving like this, will become 8U, and after reading, it will also be 8U,

Even if it is converted to 32F, it may be different from the original image.

Second, read the image

1, read imread

string path = "D:/im2.jpg";
Mat image = imread(path, IMREAD_UNCHANGED);
if (!image.data) {
	cout << "imread fail\n";
	return;
}

The second parameter is an enumeration of type ImreadModes, indicating the number of channels read

Returns an object of type Mat.

The member data is a pointer to uchar, if the read fails, the pointer is null

2, read type ImreadModes

Source code of ImreadModes:

enum ImreadModes {
       IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
       IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
       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.
     };

-1 IMREAD_UNCHANGED means the number of channels according to the picture itself

0 IMREAD_GRAYSCALE means grayscale image, i.e. single channel

1 IMREAD_COLOR means according to 3 channels

Guess you like

Origin blog.csdn.net/nameofcsdn/article/details/122966084