import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
‘’’
def plot_demo(image): #直方图
plt.hist(image.ravel(), 256, [0, 256])
plt.show()
def image_hist(image): #每一个通道的直方图
color = (‘blue’, ‘green’, ‘red’)
for i, color in enumerate(color):
hist = cv.calcHist([image], [i], None, [256], [0, 56])
plt.plot(hist, color=color)
plt.xlim([0, 256])
plt.show()
def equalHist_demo(image): #直方图均衡化,只能对灰度图进行
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
dst = cv.equalizeHist(gray)
cv.imshow(“equalHist_demo”, dst)
‘’’
def calhe_demo(image): #局部均衡化
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
clahe = cv.createCLAHE(clipLimit=5.0, tileGridSize=(8,8))
dst = clahe.apply(gray)
cv.imshow(“calhe_demo”, dst)
def create_rbg_hist(image):
h, w, c = image.shape
rgbHist = np.zeros([16
16
16, 1], np.float32)
bsize = 256 / 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)
rgbHist[np.int(index), 0] = rgbHist[np.int(index), 0] + 1
return rgbHist
def hist_compare(image1, image2):
hist1 = create_rbg_hist(image1)
hist2 = create_rbg_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(“巴氏距离 : %s, 相关性 : %s, 卡方 : %s”%(match1, match2, match3))
print("-----------helli python--------------")
src = cv.imread(“E:/image/lena1.tiff”)
cv.namedWindow(“input image”, cv.WINDOW_AUTOSIZE)
#cv.imshow(“input image”, src)
#equalHist_demo(src)
#calhe_demo(src)
image1 = cv.imread(“E:/image/lena1.jpg”)
image2 = cv.imread(“E:/image/pepper1.tiff”)
cv.imshow(“image1”, image1)
cv.imshow(“image2”, image2)
create_rbg_hist(image1)
hist_compare(image1, image2)
cv.waitKey(0)
cv.destroyAllWindows()