A histogram counts how many pixels in an image have each possible intensity value. It is one of the most fundamental tools in image analysis, used for contrast adjustment, thresholding, and feature extraction.
Given a 2D grayscale image where each pixel value is an integer in [0, 255], compute the intensity histogram.
Input:
image = [[0, 1], [1, 2]]
Output:
[1, 2, 1, 0, 0, ..., 0] (256 elements)
Value 0 appears once, value 1 appears twice, value 2 appears once. All other bins are zero.
Input:
image = [[128, 128], [128, 128]]
Output:
[0, 0, ..., 0, 4, 0, ..., 0] (histogram[128] = 4)
All four pixels share the same value, so only bin 128 is nonzero.
Initialize a list of 256 zeros. Then loop over every row and every pixel in each row.
The pixel value itself is the index into the histogram. No sorting or searching is needed.
Sign in to take notes on this problem
Accepts: array
A histogram counts how many pixels in an image have each possible intensity value. It is one of the most fundamental tools in image analysis, used for contrast adjustment, thresholding, and feature extraction.
Given a 2D grayscale image where each pixel value is an integer in [0, 255], compute the intensity histogram.
Input:
image = [[0, 1], [1, 2]]
Output:
[1, 2, 1, 0, 0, ..., 0] (256 elements)
Value 0 appears once, value 1 appears twice, value 2 appears once. All other bins are zero.
Input:
image = [[128, 128], [128, 128]]
Output:
[0, 0, ..., 0, 4, 0, ..., 0] (histogram[128] = 4)
All four pixels share the same value, so only bin 128 is nonzero.
Initialize a list of 256 zeros. Then loop over every row and every pixel in each row.
The pixel value itself is the index into the histogram. No sorting or searching is needed.
Sign in to take notes on this problem
Accepts: array