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.
Each bin contributes the squared difference of its two counts divided by the bin's combined count. A bin where both counts are zero contributes nothing (its term would be ) and is skipped. The distance is the total over all bins, returned as a float rounded to 4 decimal places.
This technique is widely used in image retrieval and object recognition systems to compare the color content of images.
Example
h1 = [1, 2, 3, 4], h2 = [1, 2, 3, 4]
0.0
- Inputs: and are two 4-bin colour histograms. Entry is the number of pixels whose colour fell into bin .
- Bin totals: gives . All are positive, so no bin is skipped and every denominator is safe.
- Bin differences: in every bin, so each squared numerator is .
- Sum: , which at 4 decimal places is the float
0.0. - Notice: the distance is exactly when the histograms match bin for bin, and it grows as counts shift between bins. For against it is . Dividing by the bin total means a gap of 2 counts weighs more in a sparse bin than in a crowded one.
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