v9.1.1
Menu

Histogram Comparison

mediumFREEBrief HistoryImage AnalysisCV: Introduction to Computer Vision

Implement a function to compare the similarity between two grayscale images using histogram intersection. This technique is based on the idea of representing images as histograms, which are graphical representations of the distribution of pixel intensities.

The comparison of two histograms, AA and BB, is done by calculating the intersection between them, which is a measure of the amount of overlap between the two distributions.

  1. Represent each image as a histogram with 256 bins, where each bin corresponds to a possible pixel intensity value.
  2. Calculate the minimum value between corresponding bins in the two histograms.
  3. Sum up these minimum values to obtain the histogram intersection.
  4. Normalise by dividing by the number of pixels in the smaller image, and round the result to 4 decimal places.
H(A,B)=∑i=0255min⁡(Ai,Bi)H(A,B) = \sum_{i=0}^{255} \min(A_i, B_i)

This technique is widely used in image retrieval and object recognition systems.

Example

Input
histogram_intersection([[0,1,2]], [[0,1,2]])
Output
1.0
Reasoning
  • Inputs: img1 and img2 are both 1×31 \times 3 grayscale images holding the intensities 0, 1 and 2. Each is summarised as a 256-bin histogram: AvA_v is the number of pixels in the first image with intensity exactly vv.
  • Histograms: both images give A0=A1=A2=1A_0 = A_1 = A_2 = 1 and B0=B1=B2=1B_0 = B_1 = B_2 = 1, with every other bin (3 through 255) equal to 0.
  • Intersection: H(A,B)=min⁡(1,1)+min⁡(1,1)+min⁡(1,1)+253⋅min⁡(0,0)=3H(A,B) = \min(1,1) + \min(1,1) + \min(1,1) + 253 \cdot \min(0,0) = 3. Empty bins add nothing.
  • Normalisation: the smaller image has 3 pixels, so the score is 3/3=13 / 3 = 1, reported as 1.0.
  • Notice: the histogram ignores where pixels sit. Reversing one image to [2,1,0][2, 1, 0] still scores 1.0. With [0,0,128,128][0,0,128,128] vs [0,128,128,255][0,128,128,255], bin 0 gives min⁡(2,1)=1\min(2,1)=1, bin 128 gives min⁡(2,2)=2\min(2,2)=2, bin 255 gives min⁡(0,1)=0\min(0,1)=0, so H=3H = 3 and the score is 3/4=0.753/4 = 0.75.

Constraints

  • Input images are 2D grayscale arrays with values in [0, 255]
  • Return the intersection value normalized by total pixels in smaller image

More from CV: Introduction to Computer Vision

solution.py

Test results

Sign in to run your code against the test cases.