Complete the code to read an image from a file using OpenCV.
import cv2 image = cv2.[1]('photo.jpg')
The cv2.imread function reads an image from a file and loads it into memory.
Complete the code to read an image in grayscale mode.
import cv2 image = cv2.imread('photo.jpg', [1])
Using cv2.IMREAD_GRAYSCALE loads the image as a grayscale (black and white) image.
Fix the error in the code to correctly read an image file.
import cv2 img = cv2.imread[1]
The cv2.imread function requires parentheses around the filename and optional flag. Using cv2.imread('image.png', cv2.IMREAD_COLOR) correctly reads the image in color mode.
Fill both blanks to read an image and check if it was loaded successfully.
import cv2 image = cv2.imread('test.jpg', [1]) if image is [2]: print('Failed to load image')
We read the image in color mode with cv2.IMREAD_COLOR. If image is None, it means loading failed.
Fill all three blanks to read an image, convert it to grayscale, and display it.
import cv2 image = cv2.imread('pic.jpg', [1]) gray = cv2.[2](image, [3]) cv2.imshow('Gray Image', gray) cv2.waitKey(0) cv2.destroyAllWindows()
First, read the image in color mode. Then convert it to grayscale using cv2.cvtColor with the flag cv2.COLOR_BGR2GRAY. Finally, display the grayscale image.