Updated April 10, 2023
Introduction to OpenCV Load Image
Whenever there is a necessity to load the image from a location specified by the path of the file, we make use of a function called imread() function in OpenCV using which the image can be read into our program either as a color image or as a grayscale image or the image as is based on flag we have provided to the imread() function and the file to be loaded to the program using imread() function must be present in a working directory in the path specified to read the image successfully and the reading of image can fail because of several reasons like no correct permissions, no file present, invalid format of the file etc.
The syntax to define imread() function in OpenCV is as follows:
imread(‘path_to_the_file’, flag)
where path_to_the_file specifies path to the location of the file where the file is present which it to be read and
flag specifies the mode in which the file must-read. There are three modes in which the file can be read namely color mode, grayscale mode and the image as is. This parameter is optional.
How to work imread() function in OpenCV?
In order to load an image to the program from a location specified by the path of the file, we make use of a function called imread() function in OpenCV.
The image to read from a location specified by the path of the file, image must be present in a working directory.
The reading of the file from a location specified by the path of the file fails if proper permissions are not provided to the image file, if the format of the file to be read is not supported or if the file is not present at all.
The image to read can be read in three modes namely color mode, grayscale mode or the image as is.
cv.IMREAD_COLOR flag must be passed to the imread() function along with the path to the file to read the image in color mode or passing the integer value 1 along with the path to the file to be read can read the file in color mode.
The imread() function reads the given image in color mode by default.
cv.IMREAD_GRAYSCALE flag must be passed to the imread() function along with the path to the file to read the image in grayscale mode or passing the integer value 0 along with the path to the file to be read can read the file in grayscale mode.
cv.IMREAD_UNCHANGES flag must be passed to the imread() function along with the path to the file to read the image as is or passing the integer value -1 along with the path to the file to be read can read the file in as is.
The imread() function reads the image from the location specified by the path to the file.
Examples
Let us discuss examples of OpenCV Load Image.
Example #1
OpenCV program in python to demonstrate imread() function to read an image from a location specified by the path to the file in color mode and display the image as the output on the screen:
#importing the module cv2
import cv2
#reading the image from the location specified by the path to the file
imageread = cv2.imread('C:/Users/admin/Desktop/tree.jpg')
#displaying the image as the output on the screen
cv2.imshow('Display_image', imageread)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image to be displayed as the output on the screen using imread() function from the location specified by the path to the file in color mode and then displaying the image as the output on the screen.
Example #2
OpenCV program in python to demonstrate imread() function to read an image from a location specified by the path to the file in grayscale mode and display the image as the output on the screen:
#importing the module cv2
import cv2
#reading the image from the location specified by the path to the file
Imageread = cv2.imread('C:/Users/admin/Desktop/educba.jpg', cv2.IMREAD_GRAYSCALE)
#displaying the image as the output on the screen
cv2.imshow('Display_image', imageread)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image to be displayed as the output on the screen using imread() function from the location specified by the path to the file in grayscale mode and then displaying the image as the output on the screen.
Example #3
OpenCV program in python to demonstrate imread() function to read an image as is from a location specified by the path to the file and display the image as the output on the screen:
#importing the module cv2
import cv2
#reading the image from the location specified by the path to the file
Imageread = cv2.imread('C:/Users/admin/Desktop/logo.png', cv2.IMREAD_UNCHANGED)
#displaying the image as the output on the screen
cv2.imshow('Display_image', imageread)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image as is to be displayed as the output on the screen using imread() function from the location specified by the path to the file and then displaying the image as the output on the screen.
Example #4
OpenCV program in python to demonstrate imread() function to read an image from a location specified by the path to the file in color mode and display the image as the output on the screen:
#importing the module cv2
import cv2
#reading the image from the location specified by the path to the file
imageread = cv2.imread('C:/Users/admin/Desktop/plane.jpg')
#displaying the image as the output on the screen
cv2.imshow('Display_image', imageread)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image to be displayed as the output on the screen using imread() function from the location specified by the path to the file in color mode and then displaying the image as the output on the screen.
Conclusion
In this article, we have learnt the concept of reading the image from a location specified by the path to the file using imread() function in OpenCV with corresponding programming examples.
Recommended Articles
We hope that this EDUCBA information on “OpenCV Load Image” was beneficial to you. You can view EDUCBA’s recommended articles for more information.