Complete the code to read an image using OpenCV.
import cv2 image = cv2.[1]('input.jpg', 0)
The cv2.imread function reads an image from a file. The second argument 0 means read as grayscale.
Complete the code to apply histogram equalization on a grayscale image.
equalized_image = cv2.[1](image)The cv2.equalizeHist function performs histogram equalization on a grayscale image.
Fix the error in the code to display the equalized image using OpenCV.
cv2.[1]('Equalized Image', equalized_image) cv2.waitKey(0) cv2.destroyAllWindows()
The cv2.imshow function displays an image in a window.
Fill both blanks to create a histogram equalization function for color images using OpenCV.
def equalize_color_image(img): ycrcb = cv2.cvtColor(img, [1]) ycrcb[:, :, 0] = cv2.[2](ycrcb[:, :, 0]) return cv2.cvtColor(ycrcb, cv2.COLOR_YCrCb2BGR)
To equalize a color image, convert it to YCrCb color space (COLOR_BGR2YCrCb), equalize the Y channel using equalizeHist, then convert back.
Fill all three blanks to compute and plot the histogram of a grayscale image using matplotlib.
import matplotlib.pyplot as plt hist = cv2.calcHist([image], [[1]], None, [256], [0, 256]) plt.plot(hist) plt.title('[2] Histogram') plt.xlabel('[3]') plt.ylabel('Frequency') plt.show()
For grayscale images, the channel index is 0. The plot title is 'Grayscale Histogram' and the x-axis label is 'Pixel Intensity'.