Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to compare histograms of two images using OpenCV Python?
The histograms of two images can be compared using cv2.compareHist() function. This function accepts three input arguments: hist1, hist2, and compare_method. The hist1 and hist2 are histograms of the two input images, while compare_method is a metric to compute the matching between the histograms. It returns a numerical parameter that expresses how well two histograms match with each other.
Comparison Methods
OpenCV provides four different histogram comparison methods:
| Method | Description | Perfect Match | No Match |
|---|---|---|---|
HISTCMP_CORREL |
Correlation | 1 | 0 |
HISTCMP_CHISQR |
Chi-Square | 0 | ? |
HISTCMP_INTERSECT |
Intersection | Higher values = better match | 0 |
HISTCMP_BHATTACHARYYA |
Bhattacharyya distance | 0 | 1 |
Steps
To compare the histograms of two images, follow these steps:
Import the required libraries: OpenCV and Matplotlib
Read the input images using cv2.imread() function
Calculate the histograms using cv2.calcHist()
Normalize the histograms using cv2.normalize()
Compare histograms using cv2.compareHist() with your chosen method
Interpret the comparison metric value
Example 1: Basic Histogram Comparison
This example demonstrates comparing two different images using the correlation method :
import cv2
import numpy as np
# Create sample images for demonstration
img1 = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
img2 = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
# Calculate histograms for each color channel
hist_img1 = cv2.calcHist([img1], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256])
hist_img2 = cv2.calcHist([img2], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256])
# Normalize the histograms
cv2.normalize(hist_img1, hist_img1, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
cv2.normalize(hist_img2, hist_img2, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
# Compare using correlation method
metric_val = cv2.compareHist(hist_img1, hist_img2, cv2.HISTCMP_CORREL)
print(f"Correlation value: {metric_val:.4f}")
Correlation value: 0.0156
Example 2: All Comparison Methods
This example compares histograms using all four available methods :
import cv2
import numpy as np
# Create two sample images
img1 = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
img2 = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
# Calculate and normalize histograms
hist_img1 = cv2.calcHist([img1], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256])
hist_img2 = cv2.calcHist([img2], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256])
cv2.normalize(hist_img1, hist_img1, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
cv2.normalize(hist_img2, hist_img2, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
# Compare using all methods
correl = cv2.compareHist(hist_img1, hist_img2, cv2.HISTCMP_CORREL)
chisqr = cv2.compareHist(hist_img1, hist_img2, cv2.HISTCMP_CHISQR)
intersect = cv2.compareHist(hist_img1, hist_img2, cv2.HISTCMP_INTERSECT)
bhattacharyya = cv2.compareHist(hist_img1, hist_img2, cv2.HISTCMP_BHATTACHARYYA)
print(f"Correlation: {correl:.4f}")
print(f"Chi-Square: {chisqr:.4f}")
print(f"Intersection: {intersect:.4f}")
print(f"Bhattacharyya: {bhattacharyya:.4f}")
Correlation: -0.0094 Chi-Square: 1.9980 Intersection: 0.0977 Bhattacharyya: 0.7071
Interpreting Results
Understanding the comparison values:
Correlation: Values closer to 1 indicate better similarity. Negative values suggest inverse correlation.
Chi-Square: Lower values indicate better similarity. Zero means perfect match.
Intersection: Higher values indicate better similarity.
Bhattacharyya: Values closer to 0 indicate better similarity. Values closer to 1 indicate dissimilarity.
Conclusion
Use cv2.compareHist() to quantify histogram similarity between images. Choose the comparison method based on your specific requirements: correlation for general similarity, chi-square for statistical difference, or Bhattacharyya for probabilistic distance.
