The cv2.imread() function in OpenCV is used to read an image from a file and store it in a NumPy array. This function takes two arguments: the file path of the image and a flag that specifies how the image should be read.
The flag parameter is optional and can be one of the following values:
The cv2.imread() function returns a NumPy array that represents the image. You can then use various functions in OpenCV to manipulate the image or perform image processing tasks on it.
Here is an example of how to use the cv2.imread() function to read an image and display it using OpenCV:
import cv2
import numpy as np
# Read the image
image = cv2.imread('image.jpg')
# Check that the image was successfully read
if image is None:
print("Error reading image")
exit()
# Display the image
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
The flag parameter is optional and can be one of the following values:
- cv2.IMREAD_COLOR: Loads the image in color (RGB) mode. This is the default value.
- cv2.IMREAD_GRAYSCALE: Loads the image in grayscale mode. cv2.IMREAD_UNCHANGED: Loads the image as is, including the alpha channel.
- For example, to read an image in grayscale mode, you can use the following code:
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
Comments
Post a Comment