Thursday, December 26, 2024
Google search engine
HomeLanguagesPython OpenCV | cv2.imread() method

Python OpenCV | cv2.imread() method

OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imread() method loads an image from the specified file. If the image cannot be read (because of the missing file, improper permissions, or unsupported or invalid format) then this method returns an empty matrix.

Example:

Python3




import cv2
 
# Load the image
image = cv2.imread("jg.png")
 
# Display the image
cv2.imshow("Image", image)
 
# Wait for the user to press a key
cv2.waitKey(0)
 
# Close all windows
cv2.destroyAllWindows()


Output:

Python OpenCV

 

Syntax of OpenCV cv2.imread() Method

Syntax: cv2.imread(filename, flag) 

Parameters:

  1. filename: The path to the image file.
  2. flag: The flag specifies the way how the image should be read.
  • cv2.IMREAD_COLOR – It specifies to load a color image. Any transparency of image will be neglected. It is the default flag. Alternatively, we can pass integer value 1 for this flag.
  • cv2.IMREAD_GRAYSCALE – It specifies to load an image in grayscale mode. Alternatively, we can pass integer value 0 for this flag. 
  • cv2.IMREAD_UNCHANGED – It specifies to load an image as such including alpha channel. Alternatively, we can pass integer value -1 for this flag.

Return Value:

The cv2.imread() function return a NumPy array if the image is loaded successfully.

Examples of OpenCV cv2.imread() Method 

cv2.imread() image

Input Image

Python OpenCV Read Image – cv2 imread()

In this example, we are reading the image as a color image.

Python3




import cv2
 
# Load the image
image = cv2.imread("gfg.jpeg")
 
# Display the image
cv2.imshow("Image", image)
 
# Wait for the user to press a key
cv2.waitKey(0)
 
# Close all windows
cv2.destroyAllWindows()


Output:

 

cv2.imread() image

image

 

Python OpenCV Read image grayscale

In this example, we are reading the image as a greyscale image. Both color and grayscale images are acceptable as input.

Python3




import cv2
 
# Load the image
image = cv2.imread("gfg.jpeg",cv2.IMREAD_GRAYSCALE)
 
# Display the image
cv2.imshow("Image", image)
 
# Wait for the user to press a key
cv2.waitKey(0)
 
# Close all windows
cv2.destroyAllWindows()


Output:

cv2.imread() image

Image

Python OpenCV | Read PNG Image with Transparency

In this example, we are reading the image with the transparency channel.

Python3




import cv2
 
# Load the image
image = cv2.imread("gfg.jpeg",cv2.IMREAD_UNCHANGED)
 
# Display the image
cv2.imshow("Image", image)
 
# Wait for the user to press a key
cv2.waitKey(0)
 
# Close all windows
cv2.destroyAllWindows()


Output:

cv2.imread() image

image

imread() and Color Channels

A NumPy array is produced after reading an image file with the cv2.imread() method. By default, the image is saved in the BGR color space. As a result, the blue, green, and red color channels, respectively, correspond to the first three channels of the NumPy array.

r1 = image[:,:,0] # get blue channel
g1 = image[:,:,1] # get green channel
b1 = image[:,:,2] # get red channel

RELATED ARTICLES

Most Popular

Recent Comments