Complete the code to compute the histogram of a grayscale image using OpenCV.
import cv2 image = cv2.imread('image.jpg', 0) hist = cv2.calcHist([image], [0], None, [256], [0, [1]])
The histogram range for pixel values in OpenCV should be from 0 to 256 to cover all 256 grayscale levels.
Complete the code to normalize the histogram so that the sum of all bins equals 1.
hist_norm = hist / [1]Dividing by the sum of all histogram bins normalizes the histogram to represent probabilities.
Fix the error in the code to compute a color histogram for a BGR image.
image = cv2.imread('color.jpg') hist = cv2.calcHist([image], [[1]], None, [256], [0, 256])
Channel 0 corresponds to the Blue channel in a BGR image, which is valid for histogram calculation.
Fill both blanks to compute and normalize a histogram for the red channel of a color image.
image = cv2.imread('color.jpg') hist = cv2.calcHist([image], [[1]], None, [256], [0, 256]) hist_norm = hist / [2]
Channel 2 is red in BGR images. Normalizing by hist.sum() scales the histogram to probabilities.
Fill all three blanks to create a histogram for the green channel, normalize it, and then find the bin with the maximum frequency.
image = cv2.imread('color.jpg') hist = cv2.calcHist([image], [[1]], None, [256], [0, 256]) hist_norm = hist / [2] max_bin = hist_norm.[3]()
Channel 1 is green. Normalizing by hist.sum() scales the histogram. argmax() finds the bin index with the highest frequency.