比較方法如下:
代碼:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import time
def create_rgb_hist(image):
h,w,ch=image.shape
rgbHist=np.zeros([16*16*16,1],np.float32)
bsize=16
for row in range(h):
for col in range(w):
b = image[row, col, 0]
g = image[row, col, 1]
r = image[row, col, 2]
index = np.int(b/bsize)*16 * 16 + np.int(g/bsize)*16 + np.int(r/bsize) #下降成16個bin,由256*256*256降維4096
print('index:',index)
rgbHist[np.int(index),0]+=1
return rgbHist
def hist_compare(image1,image2):
hist1 = create_rgb_hist(image1)
hist2 = create_rgb_hist(image2)
match1 = cv.compareHist(hist1, hist2, cv.HISTCMP_BHATTACHARYYA)#越小越相似
match2 = cv.compareHist(hist1, hist2, cv.HISTCMP_CORREL)#越大越相似
match3 = cv.compareHist(hist1, hist2, cv.HISTCMP_CHISQR) #越小越相似
print('BHATTACHARYYA:',match1,'\n','CORREL:',match2,'\n','CHISQR:',match3)
image1=cv.imread('D:/pycharm/pycharmproject/test.jpg')
image2=cv.imread('D:/pycharm/pycharmproject/test.jpg')
cv.imshow('src',image1)
hist_compare(image1,image2)
cv.waitKey(0)
cv.destroyAllWindows()