ImagesComparisonMetrics Module¶
Implements metrics for comparing images, such as similarity and difference measures.
- HMB.ImagesComparisonMetrics.MutualInformation(image1, image2, bins=100)[source]¶
Compute the mutual information between two images.
\[I(X;Y) = H(X) + H(Y) - H(X, Y)\]- where:
\(H(X)\) is the entropy of image X.
\(H(Y)\) is the entropy of image Y.
\(H(X, Y)\) is the joint entropy of X and Y.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
bins (int) – Number of bins for the histograms (default: 100).
- Returns:
The computed mutual information value.
- Return type:
- HMB.ImagesComparisonMetrics.MutualInformationColor(image1, image2, bins=100)[source]¶
Compute the mutual information between two color images by averaging over all channels.
\[I_{avg}(X;Y) = \frac{1}{C} \sum_{c=1}^C I(X_c; Y_c)\]- where:
\(C\) is the number of color channels.
\(I(X_c; Y_c)\) is the mutual information for channel c.
- Parameters:
image1 (numpy.ndarray or compatible) – First input color image.
image2 (numpy.ndarray or compatible) – Second input color image.
bins (int) – Number of bins for the histograms (default: 100).
- Returns:
The average mutual information across all color channels.
- Return type:
- HMB.ImagesComparisonMetrics.NormalizedMutualInformation(image1, image2)[source]¶
Compute the normalized mutual information (NMI) between two images.
\[\text{NMI} = \frac{I(X;Y)}{\sqrt{H(X) \cdot H(Y)}}\]- where:
\(I(X;Y)\) is the mutual information between X and Y.
\(H(X)\) and \(H(Y)\) are the entropies of X and Y respectively.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed normalized mutual information value.
- Return type:
- HMB.ImagesComparisonMetrics.StructuralSimilarity(image1, image2, winSize=7)[source]¶
Compute the structural similarity index (SSIM) between two images.
\[\text{SSIM}(x, y) = \frac{(2\mu_x\mu_y + C_1)(2\sigma_{xy} + C_2)}{(\mu_x^2 + \mu_y^2 + C_1)(\sigma_x^2 + \sigma_y^2 + C_2)}\]- where:
\(\mu_x, \mu_y\) are the local means.
\(\sigma_x^2, \sigma_y^2\) are the local variances.
\(\sigma_{xy}\) is the local covariance.
\(C_1, C_2\) are small constants to stabilize the division.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
winSize (int) – Window size for local comparisons (default: 7).
- Returns:
The computed SSIM score.
- Return type:
- HMB.ImagesComparisonMetrics.NormalizedCrossCorrelation(image1, image2)[source]¶
Compute the normalized cross-correlation (NCC) between two images.
\[\text{NCC} = \frac{\sum_i (x_i - \bar{x}) (y_i - \bar{y})}{\sqrt{\sum_i (x_i - \bar{x})^2 \; \sum_i (y_i - \bar{y})^2}}\]- where:
\(\bar{x}\) and \(\bar{y}\) are the mean values of x and y respectively.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed NCC value.
- Return type:
- HMB.ImagesComparisonMetrics.HistogramComparison(image1, image2, bins=256, eps=1e-10)[source]¶
Compute the histogram intersection between two images.
\[\text{HistInter}(H_1, H_2) = \frac{\sum_i \min(H_{1,i}, H_{2,i})}{\sum_i H_{1,i}}\]- where:
\(H_{1,i}\) and \(H_{2,i}\) are the histogram counts for bin i of image1 and image2.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
bins (int) – Number of bins for the histograms (default: 256).
eps (float) – Small constant to avoid division by zero (default: 1e-10).
- Returns:
The computed histogram intersection value (0 to 1).
- Return type:
- HMB.ImagesComparisonMetrics.UniversalQualityIndex(image1, image2)[source]¶
Compute the universal quality index (UQI) between two images.
\[\text{UQI}(x,y) = \frac{4 \, \mathrm{cov}(x,y) \; \mu_x \; \mu_y}{(\sigma_x^2 + \sigma_y^2) (\mu_x^2 + \mu_y^2)}\]- where:
\(\mu_x, \mu_y\) are the means of x and y.
\(\sigma_x^2, \sigma_y^2\) are the variances of x and y.
\(\mathrm{cov}(x,y)\) is the covariance of x and y.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed UQI value.
- Return type:
- HMB.ImagesComparisonMetrics.CosineSimilarityImages(image1, image2)[source]¶
Compute the cosine similarity between two images.
\[\mathrm{cosine}(x,y) = \frac{x \cdot y}{\|x\|_2 \; \|y\|_2}\]where the inputs are treated as flattened vectors.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed cosine similarity value (-1 to 1).
- Return type:
- HMB.ImagesComparisonMetrics.PeakSignalToNoiseRatio(image1, image2, eps=1e-10)[source]¶
Compute the peak signal-to-noise ratio (PSNR) between two images.
\[\begin{split}\mathrm{MSE} = \frac{1}{N} \sum_i (x_i - y_i)^2\\ \mathrm{PSNR} = 20 \log_{10}\left( \frac{\mathrm{MAX}_{I}}{\sqrt{\mathrm{MSE}}} \right)\end{split}\]where \(\mathrm{MAX}_{I}\) is the maximum possible pixel value of the images (e.g. 255 for uint8).
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
eps (float) – Small constant to avoid division by zero (default: 1e-10).
- Returns:
The computed PSNR value in decibels (dB).
- Return type:
- HMB.ImagesComparisonMetrics.FeatureBasedSimilarity(image1, image2)[source]¶
Compute the feature-based similarity between two images using SIFT keypoints and descriptors.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed feature-based similarity score (0 to 1).
- Return type:
- HMB.ImagesComparisonMetrics.MeanSquaredError(image1, image2)[source]¶
Compute the mean squared error (MSE) between two images.
\[\mathrm{MSE}(x,y) = \frac{1}{N} \sum_{i=1}^N (x_i - y_i)^2\]where \(N\) is the number of pixels (or elements) compared.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed MSE value.
- Return type:
- HMB.ImagesComparisonMetrics.NormalizedMeanSquaredError(image1, image2)[source]¶
Compute the normalized mean squared error (NMSE) between two images.
The implementation normalizes each image to zero mean and unit variance before computing the mean squared difference. Formally:
\[\begin{split}\tilde{x} = \frac{x - \mu_x}{\sigma_x}, \quad \tilde{y} = \frac{y - \mu_y}{\sigma_y}\\ \mathrm{NMSE}(x,y) = \frac{1}{N} \sum_{i=1}^N (\tilde{x}_i - \tilde{y}_i)^2\end{split}\]where \(\mu\) and \(\sigma\) denote mean and standard deviation respectively.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed NMSE value.
- Return type:
- HMB.ImagesComparisonMetrics.EarthMoversDistance(image1, image2)[source]¶
Compute the Earth Mover’s Distance (EMD) between two images.
For 1-D distributions the Earth Mover’s / Wasserstein-1 distance can be written as:
\[W_1(P,Q) = \int_{-\infty}^{\infty} |F_P(t) - F_Q(t)| \, dt\]where \(F_P\) and \(F_Q\) are the cumulative distribution functions of the two distributions.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed Earth Mover’s Distance value.
- Return type:
- HMB.ImagesComparisonMetrics.SpectralResidual(image1, image2)[source]¶
Compute the spectral residual similarity (SRS) between two images.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed spectral residual similarity value.
- Return type:
- HMB.ImagesComparisonMetrics.PhaseCongruency(image1, image2)[source]¶
Compute the phase congruency similarity between two images.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed phase congruency similarity value.
- Return type:
- HMB.ImagesComparisonMetrics.NoiseQualityMeasure(image1, image2)[source]¶
Compute the noise quality measure (NQM) between two images.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed noise quality measure value.
- Return type:
- HMB.ImagesComparisonMetrics.HellingerDistance(image1, image2)[source]¶
Compute the Hellinger distance between two images.
\[H(P,Q) = \frac{1}{\sqrt{2}} \left\| \sqrt{P} - \sqrt{Q} \right\|_2\]where \(P\) and \(Q\) are the (normalized) histograms / probability distributions.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed Hellinger distance value.
- Return type:
- HMB.ImagesComparisonMetrics.BhattacharyyaDistance(image1, image2)[source]¶
Compute the Bhattacharyya distance between two images.
\[\begin{split}BC = \sum_i \sqrt{p_i q_i}\\ D_B = -\ln(BC)\end{split}\]where \(p\) and \(q\) are the normalized histograms of the images and \(BC\) is the Bhattacharyya coefficient.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed Bhattacharyya distance value.
- Return type:
- HMB.ImagesComparisonMetrics.PerceptualHash(image1, image2)[source]¶
Compute the perceptual hash (pHash) Hamming distance between two images.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed Hamming distance between the perceptual hashes.
- Return type:
- HMB.ImagesComparisonMetrics.JensenShannonDivergence(image1, image2)[source]¶
Compute the Jensen-Shannon divergence (JSD) between two images.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed Jensen-Shannon divergence value.
- Return type:
- HMB.ImagesComparisonMetrics.KLDivergence(image1, image2, eps=1e-10)[source]¶
Compute the Kullback-Leibler (KL) divergence between two images.
\[D_{\mathrm{KL}}(P \| Q) = \sum_i p_i \log\frac{p_i}{q_i}\]where \(p\) and \(q\) are the normalized histograms (probability distributions) of the two images. A small constant \(\varepsilon\) is added for numerical stability.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
eps (float) – Small constant to avoid division by zero (default: 1e-10).
- Returns:
The computed KL divergence value.
- Return type:
- HMB.ImagesComparisonMetrics.GradientMagnitudeSimilarityDeviation(image1, image2, eps=1e-10)[source]¶
Compute the gradient magnitude similarity deviation (GMSD) between two images.
The GMS map is computed per-pixel as:
\[\mathrm{GMS}(i) = \frac{2\,g_1(i)\,g_2(i)}{g_1(i)^2 + g_2(i)^2 + \varepsilon}\]where \(g_1, g_2\) are the gradient magnitudes of the two images. The GMSD is the standard deviation of the GMS map:
\[\mathrm{GMSD} = \sqrt{\frac{1}{N} \sum_i \big(\mathrm{GMS}(i) - \mu_{\mathrm{GMS}}\big)^2}\]- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
eps (float) – Small constant to avoid division by zero (default: 1e-10).
- Returns:
The computed GMSD value.
- Return type:
- HMB.ImagesComparisonMetrics.SpectralAngleMapper(image1, image2)[source]¶
Compute the spectral angle mapper (SAM) between two images.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
The computed SAM value.
- Return type:
- HMB.ImagesComparisonMetrics.BRISQUE(image)[source]¶
Compute the BRISQUE (Blind/Referenceless Image Spatial Quality Evaluator) score for an image.
- Parameters:
image (numpy.ndarray or compatible) – Input image.
- Returns:
The computed BRISQUE score (lower is better quality).
- Return type:
- HMB.ImagesComparisonMetrics.SummaryTable(image1, image2)[source]¶
Compute a summary table of various similarity and dissimilarity metrics between two images.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
- Returns:
Dictionary mapping metric names to their computed values.
- Return type:
- HMB.ImagesComparisonMetrics.IsSimilarityAccepted(image1, image2, miThreshold=0.35, cosSimThreshold=0.75, pHashThreshold=20, strict=True)[source]¶
Determine if two images are similar based on Mutual Information, Cosine Similarity, and Perceptual Hash.
- Parameters:
image1 (numpy.ndarray or compatible) – First input image.
image2 (numpy.ndarray or compatible) – Second input image.
miThreshold (float) – Threshold for Mutual Information (default: 0.35).
cosSimThreshold (float) – Threshold for Cosine Similarity (default: 0.75).
pHashThreshold (int) – Threshold for Perceptual Hash Hamming distance (default: 20).
strict (bool) – If True, all conditions must be met; if False, at least one condition must be met (default: True).
- Returns:
(bool, str) indicating if the images are similar and a summary string.
- Return type: