OpenCV cv2.imread()

Topics Covered

Overview

To handle challenging real-world tasks, Python provides a wide range of contemporary libraries, like OpenCV for computer vision, TensorFlow for machine learning, Pandas, and Numpy for mathematical computing, etc. Projects involving computer vision and image processing, such as real-time face and hand detection, object tracking, etc., frequently employ the Python OpenCV package.

Syntax of OpenCV cv2.imread() Method

The imread() function loads a file with the specified image and returns it. An image must first be read to be processed. Depending on how many color channels are present in the image, imread() produces either a 2D or 3D matrix.

The syntax for the cv2.imread() method in OpenCV is:

Parameters of OpenCV cv2.imread() Method

The cv2.imread() method parameters are important because they allow you to specify how the image file should be read and processed.

ParametersDescription
pathThis parameter specifies the location of the image file to be read. It can be an absolute or relative path to the file, depending on the user's preference. The file extension, such as .jpg or .png, should also be included in the path parameter. If the image file is located in a subdirectory, the path parameter can also include the subdirectory path to navigate to the file.
flag(optional)This parameter specifies how the image should be read and stored in memory. It is an optional parameter, and if not provided, the default value cv2.IMREAD_COLOR is used. There are several flags available to customize the way the image is read:

Flag parameters:

The flags parameter in the cv2.imread() method is important because it allows you to specify how the image file should be read and processed.

FlagDescription
cv2.IMREAD_COLORTo return the image in BGR color format, use this flag. That is the default setting. An alternative option is to pass the integer value 1 for this flag.
cv2.IMREAD_ GRAYSCALETo return the image in grayscale format, use this flag. As an alternative, we can set this flag's integer value to 0.
cv2.IMREAD_UNCHANGEDThis parameter receives an RGB image from the source and outputs the same RGB image. Similarly to this, if an image is ARGB and also contains a transparency channel, it will load in ARGB mode. The cv2.imread() function can be instructed to utilize this flag by passing it the integer value "-1".

Return Value of OpenCV cv2.imread() Method

The cv2.imread() method in OpenCV returns a NumPy array representing the image read from the file. Here are four important points to keep in mind regarding the return value of this method:

  • If the image file could not be read or does not exist, the cv2.imread() method returns None.

  • The returned NumPy array contains the pixel values of the image in BGR (blue-green-red) format unless the flags parameter is set to cv2.IMREAD_GRAYSCALE or cv2.IMREAD_UNCHANGED.

  • The dimensions of the returned NumPy array correspond to the dimensions of the image, with the number of rows and columns determined by the height and width of the image, respectively.

  • If the flags parameter is set to cv2.IMREAD_UNCHANGED, the returned NumPy array may contain an additional alpha channel, which represents the level of transparency for each pixel.

Exceptions of OpenCV cv2.imread() Method

  • If the specified file does not exist or cannot be accessed, then the cv2.imread() method will raise a "FileNotFoundError" exception.
  • If the specified file is not in a valid image format, then the cv2.imread() method will raise a "cv2.error" exception.
  • If the specified file path contains non-ASCII characters or if the file path is too long, then the cv2.imread() method will raise an "UnicodeError" exception.
  • If the memory is not available to read the image file, then the cv2.imread() method will raise a "cv2.error" exception.
  • If the specified file is password-protected or encrypted, then the cv2.imread() method will raise a "cv2.error" exception.

How does the OpenCV cv2.imread() Method Work?

The cv2.imread() function loads a file with the specified image and returns it. An image must be firstread to be processed. and depending on how many color channels are present in the image, imread() produces either a 2D or 3D matrix.

The function produces an empty matrix if the picture cannot be read (due to a missing file, incorrect permissions, an unsupported format, or an invalid format).

  • A function in OpenCV called imread() is used to load images into programs from locations indicated by the file's path.
  • If the image is already present in the current working directory, then all that needs to be mentioned is the image's name and its extension, such as.jpg,.png, etc or if the image is located elsewhere on your computer and isn't the working directory, you must provide the whole path, also known as the absolute path of the image.
  • Also, the images can be read in several different ways. The second argument in the function call specifies how the image should be processed. A picture can be processed in three different ways.

cv2.imread_color:

A colored version of the image will be loaded into this format. If the image contains transparency, it will be disregarded and the default flag will be set.

cv2.imread_grayscale:

An image is typically converted to a grayscale image for processing and analysis so that algorithms can be used to classify it. This function generates the image in grayscale mode.

cv2.imread_unchanged:

This mode enables alpha channel loading for images.

These are the three modes that aid in interpreting and processing images at the same time. Instead of using these lengthy names for the methods, you can use a shorter method of simply passing three flags represented by integers. We can supply the parameter as 1 if the image should be colored, 0 if it should be monochrome, and -1 if it should be unmodified.

Examples of OpenCV cv2.imread() Method

cv2.IMREAD_COLOR is the default method used to read a color image. It reads the image in the BGR (Blue-Green-Red) color format.

Example - 1: OpenCV cv2- Read Color Image

Output:

color-image

In this code, we use the cv2.imread() method to read a color image file. The second argument cv2.IMREAD_COLOR is used to specify that we want to read the image in color mode. We then display the image using the cv2.imshow() method

Example - 2: OpenCV cv2 – Read Image as Grayscale

Color or grayscale images can be used as input. If the flag argument is cv2.IMREAD GRAYSCALE, reads the image in a single color channel.

Output:

grayscale-image

The image has a height of 225 pixels and a width of 225 pixels. The array's elements represent the grayscale value at each pixel.

Example - 3: OpenCV cv2 – Read Image with Transparency Channel

The cv2.IMREAD_UNCHANGED method is used to read an image including an alpha channel. It reads the image in the BGR color format.

Output:

We have read the image's four channels in their entirety. Red, Green, Blue, and Transparent are their names.

Example - 4: cv2.imread() Function and Color Channels

Output:

output-image-of-cv2-imread-function

In the above code, cv2.imread() function is used to read the image in color mode and store it in the input_image variable. Then, the cv2.split() function is used to split the image into its three color channels (blue, green, and red), which are stored in blue, green, and red variables respectively. Finally, all the images are displayed using cv2.imshow().

Conclusion

  • The function cv2.imread() reads an image from the specified path.
  • cv2.imread() accepts two arguments: path variable and flag.
  • The image must be in the working directory or a full path to the image must be specified.
  • The image is rendered using another code, cv2.imshow().
  • The image is read in RGB color mode by the default flag value, and in Grayscale mode by the 0 value.
  • The flag value -1 or cv2.IMREAD_UNCHANGED is used in the cv2.imread() method to read an image based on the source image.

See Also

OpenCV documentation:

  1. Image file reading and writing
  2. Flags used for image file reading and writing