Problems
Loading...
1 / 1

Image Histogram

Computer Vision
Easy

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.

Algorithm

  1. Create a list of 256 zeros, one bin for each possible intensity (0 through 255).
  2. For every pixel in the image, increment the bin at the index matching that pixel's value.
Loading visualization...

Examples

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.

Hint 1

Initialize a list of 256 zeros. Then loop over every row and every pixel in each row.

Hint 2

The pixel value itself is the index into the histogram. No sorting or searching is needed.

Requirements

  • Count every pixel in all rows and columns of the image
  • Return exactly 256 bins covering intensities 0 through 255
  • Handle images of any size (including a single pixel)

Constraints

  • The image has at least one pixel
  • All pixel values are integers in [0, 255]
  • Always return a list of exactly 256 integers
  • Time limit: 300 ms
Try Similar Problems