Skip to content

compareHist(HISTCMP_CORREL) returns -1.0 when comparing a histogram with itself #1257

Description

@CloakedLeader

System Information

  • OpenCV: 5.0.0.93 (opencv-python)
  • Python: 3.13
  • Operating System: Windows

Description

When using cv2.compareHist() with the cv2.HISTCMP_CORREL comparison method, comparing a histogram against itself returns -1.0.

A histogram should have a Pearson correlation coefficient of 1.0 with itself.

This behaviour occurs regardless of whether the histogram is normalized.

Minimal Reproducible Example

import cv2
import numpy as np

print(cv2.__version__)

h = np.random.rand(8, 8, 8).astype(np.float32)

print("Raw:", cv2.compareHist(h, h, cv2.HISTCMP_CORREL))

h1 = cv2.normalize(h, None, alpha=1.0, norm_type=cv2.NORM_L1)
print("L1:", cv2.compareHist(h1, h1, cv2.HISTCMP_CORREL))

h2 = cv2.normalize(h, None, alpha=1.0, norm_type=cv2.NORM_L2)
print("L2:", cv2.compareHist(h2, h2, cv2.HISTCMP_CORREL))

h3 = cv2.normalize(h, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
print("MINMAX:", cv2.compareHist(h3, h3, cv2.HISTCMP_CORREL))

Actual Output

5.0.0
Raw: -1.0
L1: -1.0
L2: -1.0
MINMAX: -1.0

Expected Output

5.0.0
Raw: 1.0
L1: 1.0
L2: 1.0
MINMAX: 1.0

I also observed this while comparing real image histograms. In that case HISTCMP_CORREL sometimes returned values outside the expected range [-1, 1] (e.g. -2.8) or NaN, while the other comparison methods (HISTCMP_BHATTACHARYYA, HISTCMP_CHISQR, etc.) behaved as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions