Bird
Raised Fist0
Computer Visionml~10 mins

Histogram computation in Computer Vision - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compute the histogram of a grayscale image using OpenCV.

Computer Vision
import cv2
image = cv2.imread('image.jpg', 0)
hist = cv2.calcHist([image], [0], None, [256], [0, [1]])
Drag options to blanks, or click blank then click option'
A256
B255
C257
D128
Attempts:
3 left
💡 Hint
Common Mistakes
Using 255 as the upper range causes the last pixel value to be excluded.
Using 128 limits the histogram to half the grayscale range.
2fill in blank
medium

Complete the code to normalize the histogram so that the sum of all bins equals 1.

Computer Vision
hist_norm = hist / [1]
Drag options to blanks, or click blank then click option'
Ahist.sum()
Bhist.max()
Clen(hist)
D256
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing by max() scales the histogram incorrectly.
Dividing by length or fixed number does not normalize properly.
3fill in blank
hard

Fix the error in the code to compute a color histogram for a BGR image.

Computer Vision
image = cv2.imread('color.jpg')
hist = cv2.calcHist([image], [[1]], None, [256], [0, 256])
Drag options to blanks, or click blank then click option'
A3
B0
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using channel 3 causes an index error because only 0,1,2 exist.
Using channel 1 or 2 is valid but not the fix requested.
4fill in blank
hard

Fill both blanks to compute and normalize a histogram for the red channel of a color image.

Computer Vision
image = cv2.imread('color.jpg')
hist = cv2.calcHist([image], [[1]], None, [256], [0, 256])
hist_norm = hist / [2]
Drag options to blanks, or click blank then click option'
A2
Bhist.sum()
C0
Dimage.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using channel 0 or 1 selects wrong color channels.
Dividing by image.size does not normalize histogram counts correctly.
5fill in blank
hard

Fill all three blanks to create a histogram for the green channel, normalize it, and then find the bin with the maximum frequency.

Computer Vision
image = cv2.imread('color.jpg')
hist = cv2.calcHist([image], [[1]], None, [256], [0, 256])
hist_norm = hist / [2]
max_bin = hist_norm.[3]()
Drag options to blanks, or click blank then click option'
A1
Bhist.sum()
Cargmax
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using max() returns the maximum value, not the bin index.
Using wrong channel index selects incorrect color channel.

Practice

(1/5)
1. What does a histogram represent in image processing?
easy
A. The file format of the image
B. The count of pixels for each brightness or color value
C. The number of edges detected in the image
D. The size of the image in pixels

Solution

  1. Step 1: Understand what a histogram measures

    A histogram counts how many pixels fall into each brightness or color range in an image.
  2. 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.
  3. Final Answer:

    The count of pixels for each brightness or color value -> Option B
  4. Quick Check:

    Histogram = pixel counts by brightness/color [OK]
Hint: Histogram counts pixels per brightness/color range [OK]
Common Mistakes:
  • Confusing histogram with image size
  • Thinking histogram shows edges
  • Mixing histogram with file format
2. Which of the following is the correct way to call OpenCV's calcHist function for a grayscale image stored in variable img?
easy
A. cv2.calcHist([img], [0], None, [256], [0,256])
B. cv2.calcHist(img, 0, None, 256, 0, 256)
C. cv2.calcHist(img, [0], None, [256], [0,256])
D. cv2.calcHist([img], 0, None, 256, [0,256])

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    cv2.calcHist([img], [0], None, [256], [0,256]) -> Option A
  4. Quick Check:

    Use lists for parameters in calcHist [OK]
Hint: Always wrap image and channels in lists for calcHist [OK]
Common Mistakes:
  • Passing image directly without list
  • Using integers instead of lists for channels or bins
  • Incorrect number of arguments
3. What will be the output shape of the histogram computed by this code snippet?
hist = cv2.calcHist([img], [0], None, [128], [0,256])
print(hist.shape)
medium
A. (128, 1)
B. (256, 1)
C. (1, 128)
D. (128,)

Solution

  1. Step 1: Understand the bins parameter in calcHist

    The bins parameter is [128], so the histogram will have 128 bins.
  2. Step 2: Check the shape of the returned histogram

    OpenCV returns a 2D array with shape (bins, 1), so shape is (128, 1).
  3. Final Answer:

    (128, 1) -> Option A
  4. Quick Check:

    Bins = 128 means shape (128, 1) [OK]
Hint: Histogram shape = (bins, 1) in OpenCV [OK]
Common Mistakes:
  • Assuming shape is (bins,) 1D array
  • Confusing bins with range size
  • Expecting (1, bins) shape
4. Identify the error in this code snippet for computing a color histogram of an image img:
hist = cv2.calcHist([img], [0, 1, 2], None, [256], [0,256])
medium
A. The ranges parameter should be a single number, not a list
B. The image should not be inside a list
C. The bins parameter should be a list with one value per channel
D. The mask parameter cannot be None

Solution

  1. 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.
  2. Step 2: Identify the mistake in bins argument

    Bins is given as [256], a single value, which is incorrect for 3 channels.
  3. Final Answer:

    The bins parameter should be a list with one value per channel -> Option C
  4. Quick Check:

    Bins list length = channels count [OK]
Hint: Bins list length must match channels count [OK]
Common Mistakes:
  • Using single bins value for multiple channels
  • Not wrapping image in list
  • Misusing ranges parameter
5. You want to compare the brightness distribution of two grayscale images using histograms. Which approach is best to make the comparison fair and meaningful?
hard
A. Compute histograms with different bin sizes to capture details
B. Use histograms without normalization to keep original counts
C. Compare raw pixel values directly without histograms
D. Compute histograms with the same number of bins and normalize them before comparing

Solution

  1. Step 1: Understand the need for fair comparison

    To compare brightness distributions, histograms must be computed with the same bin count to align ranges.
  2. Step 2: Importance of normalization

    Normalizing histograms removes effects of image size differences, making comparison meaningful.
  3. Final Answer:

    Compute histograms with the same number of bins and normalize them before comparing -> Option D
  4. Quick Check:

    Same bins + normalization = fair histogram comparison [OK]
Hint: Normalize histograms with same bins for fair comparison [OK]
Common Mistakes:
  • Using different bin sizes for each image
  • Comparing raw counts without normalization
  • Ignoring histogram and comparing pixels directly