直方图均衡化
直方图比较
调整对比度:1、整体;2、局部
都是对灰色图像
from cv2 import cv2 as cv
import numpy as np
# 直方图均衡化都是针对灰色图像
# 整体
def equalHist_demo(image):
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
dst = cv.equalizeHist(gray)
cv.imshow('equalHist_demo',dst)
# 局部
def clahe_demo(image):
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
clahe = cv.createCLAHE(clipLimit=5.0,tileGridSize=(8,8)) # #clipLimit是对比度的大小,tileGridSize是每次处理块的大小
dst = clahe.apply(gray)
cv.imshow('clahe_demo',dst)
def create_rgb_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_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('巴氏距离:%s, 相关性:%s, 卡方:%s'%(match1,match2,match3))
if __name__ == "__main__":
filepath1 = "C:\\pictures\\6.jpg"
img1 = cv.imread(filepath1) # blue green red
filepath2 = "C:\\pictures\\10.jpg"
img2 = cv.imread(filepath2)
# cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image1",img1)
cv.imshow("input image2",img2)
# equalHist_demo(img)
# clahe_demo(img)
hist_compare(img1,img2)
cv.waitKey(0)
cv.destroyAllWindows()
两个图的比较