Skip to content

Instantly share code, notes, and snippets.

@xiaok
Last active February 11, 2022 08:52
Show Gist options
  • Save xiaok/ded2dc90d7227c92999533bd4cb88c9f to your computer and use it in GitHub Desktop.
Save xiaok/ded2dc90d7227c92999533bd4cb88c9f to your computer and use it in GitHub Desktop.
# pip install opencv-python
import cv2
img1 = cv2.imread("a.png", cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread("a_sample.png", cv2.IMREAD_GRAYSCALE)
def calc_hist(img):
hist = cv2.calcHist([img], channels=[0], mask=None, histSize=[256], ranges=[0, 256])
hist = cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
hist = hist.squeeze(axis=-1)
return hist
hist1 = calc_hist(img1)
hist2 = calc_hist(img2)
score = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
# 数字越靠近1越是同一张图
print(score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment