PIXELBANKv9.1.0
Menu

Color Histogram Comparison

Implement a function to compare two color histograms by calculating the chi-squared distance between them. This task involves understanding how to quantify the similarity between two distributions, which is crucial in computer vision for image analysis and object recognition.

The chi-squared distance is a measure used to compare the distribution of two histograms, which in this case represent the color distributions of two images. It is based on the idea of calculating the difference between the expected and observed frequencies in each bin of the histograms. The formula for the chi-squared distance is derived from the chi-squared statistic, which is commonly used in statistical tests to determine how well observed data fit expected distributions.

To calculate the chi-squared distance, follow these steps:

  1. Iterate through each bin in the histograms.
  2. For each bin, calculate the sum of the counts from both histograms.
  3. If the sum is greater than zero, calculate the squared difference between the counts from the two histograms divided by the sum of the counts.
  4. Accumulate these values for all bins.
χ2=∑i(h1[i]−h2[i])2h1[i]+h2[i]\chi^2 = \sum_{i} \frac{(h_1[i] - h_2[i])^2}{h_1[i] + h_2[i]}

This technique is widely used in image retrieval and object recognition systems to compare the color content of images.

Example:

Input:
h1 = [1, 2, 3, 4], h2 = [1, 2, 3, 4]
Output:
0.0
Reasoning:
  • The given color histograms are h1=[1,2,3,4]h_1 = [1, 2, 3, 4] and h2=[1,2,3,4]h_2 = [1, 2, 3, 4].
  • We calculate the chi-squared distance using the formula: χ2=∑i(h1[i]−h2[i])2h1[i]+h2[i]\chi^2 = \sum_{i} \frac{(h_1[i] - h_2[i])^2}{h_1[i] + h_2[i]}. Since h1[i]=h2[i]h_1[i] = h_2[i] for all ii, the numerator (h1[i]−h2[i])2(h_1[i] - h_2[i])^2 is 00 for all ii.
  • The chi-squared distance χ2\chi^2 is therefore 00, as the sum of zeros is 00.
  • The result is rounded to 4 decimal places, yielding a final output of 0.00.0.

Constraints:

  • h1 and h2 are lists of non-negative numbers with equal length
  • Skip bins where both counts are 0
  • Return a single float rounded to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.