OpenCV Read Image - cv2 imread() - 3 Python Examples
OpenCV cv2 imread()
You can read image into a numpy array using opencv library. The array contains pixel level data. And as per the requirement, you may modify the data of the image at a pixel level by updating the array values.
To read an image in Python using OpenCV, use cv2.imread() function. imread() returns a 2D or 3D matrix based on the number of color channels present in the image. For a binary or grey scale image, 2D array is sufficient. But for a colored image, you need 3D array.
In this tutorial, we shall learn in detail how to read an image using OpenCV, by considering some of the regular scenarios.
We will also learn the order in which imread() function decodes the color channels from an image and how imread() treats different image extensions.
Syntax of cv2.imread()
The syntax of cv2.imread() function is given below.
cv2.imread(/path/to/image, flag)
where /path/to/image has to be the complete absolute path to the image. The flag is optional and one of the following possible values can be passed for the flag.
- cv2.IMREAD_COLOR reads the image with RGB colors but no transparency channel. This is the default value for the flag when no value is provided as the second argument for cv2.imread().
- cv2.IMREAD_GRAYSCALE reads the image as grey image. If the source image is color image, grey value of each pixel is calculated by taking the average of color channels, and is read into the array.
- cv2.IMREAD_UNCHANGED reads the image as is from the source. If the source image is an RGB, it loads the image into array with Red, Green and Blue channels. If the source image is ARGB, it loads the image with three color components along with the alpha or transparency channel.
Examples
1. Read color image using imread()
In this example, we will read a color image. As the default value of the flag
argument is cv2.IMREAD_COLOR
, we are not passing the flag explicitly.
Python Program
import cv2
#read image
img = cv2.imread('D:/image-1.png')
#print its shape
print('Image Dimensions :', img.shape)
Output
Run the above python program, and you shall get the following output.
Image Dimensions : (400, 640, 3)
img.shape
returns tuple representing (height, width, number_of_channels). Height of the image is 400 pixels, width is 640 and there are three color channels in the image. For cv2.IMREAD_COLOR, transparency channel is ignored even if present.
2. OpenCV cv2 - Read image as greyscale
In this example, we will read image as a grey scale image. Input can be color image or grey scale image. But, if flag argument is cv2.IMREAD_GRAYSCALE
, the image is read as grey scale image.
Python Program
import cv2
img = cv2.imread('D:/image-1.png', cv2.IMREAD_GRAYSCALE)
print('Image Dimensions :', img.shape)
Output
Image Dimensions : (400, 640)
Height of the image is 400 pixels, width is 640. Each element in the array represents grey scale value at the respective pixel.
3. OpenCV cv2 - Read image with transparency channel
In this example, we will read an image with transparency channel. If there is a transparency channel in the image, then we can pass cv2.IMREAD_UNCHANGED to read the transparency channel along with the color channels.
Python Program
import cv2
img = cv2.imread('D:/image-1.png', cv2.IMREAD_UNCHANGED)
print('Image Dimensions :', img.shape)
Output
Image Dimensions : (400, 640, 4)
We have read all the four channels of the image. Namely Red, Green, Blue and Transparency.
imread() and Color Channels
imread() decodes the image into a matrix with the color channels stored in the order of Blue, Green, Red and A (Transparency) respectively.
If (400, 640, 4) is the shape of the image, then
- (:, :, 0) represents Blue channel
- (:, :, 1) represents Green channel
- (:, :, 2) represents Red channel
- (:, :, 3) represents Transparency channel
imread() and File Extensions
There are many extensions used for images based on the operating system, compression technique, etc.
When imread() method reads image, it does not consider the extension of the image file name to determine the format of the image. But decides the extension based on the format present in the file data.
imread() supports JPEGs, PNGs, and TIFFs across all platforms. But for the combination of other formats and Operating Systems, imread() may consider the operating system level codecs. You may refer the official documentation of imread() for these special scenarios.
Summary
In this Python OpenCV Tutorial, we learned how to use cv2.imread() method to read an image into a Python Array.