A histogram helps us see how often different colors or brightness levels appear in an image. It shows the picture's color or light distribution in a simple graph.
Histogram computation in Computer Vision
Start learning this pattern below
Jump into concepts and practice - no test required
cv2.calcHist(images, channels, mask, histSize, ranges) # images: list of images (usually one image in a list) # channels: list of channel indices (e.g., [0] for grayscale or blue channel) # mask: None or mask image to select part of image # histSize: list with number of bins (e.g., [256]) # ranges: list with pixel value range (e.g., [0, 256])
The function returns a histogram array showing counts of pixels in each bin.
For color images, you can compute histograms for each color channel separately.
hist = cv2.calcHist([image], [0], None, [256], [0, 256])
hist_b = cv2.calcHist([image], [0], None, [256], [0, 256]) hist_g = cv2.calcHist([image], [1], None, [256], [0, 256]) hist_r = cv2.calcHist([image], [2], None, [256], [0, 256])
This code creates a small grayscale image with pixel values 50, 100, 150, and 200 repeated. It computes the histogram and prints how many pixels have each value.
import cv2 import numpy as np # Create a simple grayscale image with two pixel values image = np.array([[50, 50, 100, 100], [50, 50, 100, 100], [150, 150, 200, 200], [150, 150, 200, 200]], dtype=np.uint8) # Compute histogram with 256 bins for pixel values 0-255 hist = cv2.calcHist([image], [0], None, [256], [0, 256]) # Print histogram values for bins with counts for i, val in enumerate(hist): if val[0] > 0: print(f"Pixel value {i}: {int(val[0])} pixels")
Histograms are useful to understand image brightness and color distribution quickly.
Using a mask lets you compute histograms for only part of the image.
Bins group pixel values; more bins mean finer detail but more data.
A histogram counts how many pixels fall into each brightness or color range.
It helps us see if an image is dark, bright, or balanced.
OpenCV's calcHist function makes it easy to compute histograms.
Practice
Solution
Step 1: Understand what a histogram measures
A histogram counts how many pixels fall into each brightness or color range in an image.Step 2: Compare options with this definition
Only The count of pixels for each brightness or color value correctly describes this counting of pixels by brightness or color.Final Answer:
The count of pixels for each brightness or color value -> Option BQuick Check:
Histogram = pixel counts by brightness/color [OK]
- Confusing histogram with image size
- Thinking histogram shows edges
- Mixing histogram with file format
calcHist function for a grayscale image stored in variable img?Solution
Step 1: Recall the correct syntax of cv2.calcHist
The function requires the image inside a list, channels as a list, mask (None if no mask), histogram size as a list, and ranges as a list.Step 2: Match options to this syntax
Only cv2.calcHist([img], [0], None, [256], [0,256]) correctly uses lists for image, channels, histogram size, and ranges.Final Answer:
cv2.calcHist([img], [0], None, [256], [0,256]) -> Option AQuick Check:
Use lists for parameters in calcHist [OK]
- Passing image directly without list
- Using integers instead of lists for channels or bins
- Incorrect number of arguments
hist = cv2.calcHist([img], [0], None, [128], [0,256]) print(hist.shape)
Solution
Step 1: Understand the bins parameter in calcHist
The bins parameter is [128], so the histogram will have 128 bins.Step 2: Check the shape of the returned histogram
OpenCV returns a 2D array with shape (bins, 1), so shape is (128, 1).Final Answer:
(128, 1) -> Option AQuick Check:
Bins = 128 means shape (128, 1) [OK]
- Assuming shape is (bins,) 1D array
- Confusing bins with range size
- Expecting (1, bins) shape
img:hist = cv2.calcHist([img], [0, 1, 2], None, [256], [0,256])
Solution
Step 1: Check the channels and bins parameters
Channels are [0,1,2] for 3 color channels, so bins must be a list with 3 values, one per channel.Step 2: Identify the mistake in bins argument
Bins is given as [256], a single value, which is incorrect for 3 channels.Final Answer:
The bins parameter should be a list with one value per channel -> Option CQuick Check:
Bins list length = channels count [OK]
- Using single bins value for multiple channels
- Not wrapping image in list
- Misusing ranges parameter
Solution
Step 1: Understand the need for fair comparison
To compare brightness distributions, histograms must be computed with the same bin count to align ranges.Step 2: Importance of normalization
Normalizing histograms removes effects of image size differences, making comparison meaningful.Final Answer:
Compute histograms with the same number of bins and normalize them before comparing -> Option DQuick Check:
Same bins + normalization = fair histogram comparison [OK]
- Using different bin sizes for each image
- Comparing raw counts without normalization
- Ignoring histogram and comparing pixels directly
