OpenCV - The IMREAD_XXX Flag



OpenCV supports various types of images such as colored, binary, grayscale, etc. Using the imread() method and predefined fields of the Imgcodecs class, you can read a given image as another type.

The flags parameter of imread() method (IMREAD_XXX)

In the earlier chapters, we have seen the syntax of imread() method of the Imgcodecs class. It accepts a string argument representing the location of the image that is to be read.

imread(filename)

The imread() method has another syntax.

imread(filename, int flags)

This syntax accepts two parameters −

  • filename − It accepts an argument (filename), a variable of the String type representing the path of the file that is to be read.

  • flags − An integer value representing a predefined flag value. For each value, this reads the given image as a specific type (gray scale color etc.)

Following is the table listing various fields provided in the Imgproc class as values for this parameter.

S.No Fields and Description
1

IMREAD_COLOR

If the flag is set to this value, the loaded image will be converted to a 3-channel BGR (Blue Green Red) color image.

2

IMREAD_GRAYSCALE

If the flag is set to this value, the loaded image will be converted to a single-channel grayscale image.

3

IMREAD_LOAD_GDAL

If the flag is set to this value, you can load the image using the gdal driver.

4

IMREAD_ANYCOLOR

If the flag is set to this value, the image is read in any possible color format.

5

IMREAD_REDUCED_COLOR_2

IMREAD_REDUCED_COLOR_4

IMREAD_REDUCED_COLOR_8

If the flag is set to this value, the image is read as three-channel BGR, and the size of the image is reduced to ½, ¼th or ⅛th of the original size of the image with respect to the field used.

6

IMREAD_REDUCED_GRAYSCALE_2

IMREAD_REDUCED_GRAYSCALE_4

IMREAD_REDUCED_GRAYSCALE_8

If the flag is set to this value, the image is read as a single-channel grayscale image, and the size of the image is reduced to ½, ¼th or ⅛th of the original size of the image with respect to the field used.

7

IMREAD_UNCHANGED

If the flag is set to this value, the loaded image is returned as it is.

Advertisements