python opencv——直方图应用
def equaliHist_demo(image):
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
dst = cv.equalizeHist(gray) #直方图均衡化,用于提高图像的质量,该函数处理的图片一定是灰度图才可以
cv.imshow('equaliHist_demo',dst)
直方图均衡化
直方图均衡化是通过拉伸像素强度分布范围来增强图像对比度的一种方法
def clahe_demo(image): gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) clahe = cv.createCLAHE(clipLimit=2.0,tileGridSize=(8,8)) #createCLAHE自适应均衡化图像 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) #巴氏距离 越接近0越相似 match2 = cv.compareHist(hist1,hist2,cv.HISTCMP_CORREL) #相关性 越接近1越相似 match3 = cv.compareHist(hist1,hist2,cv.HISTCMP_CHISQR) #卡方 越小越相似 print('巴氏距离:%s,相关性:%s,卡方:%s'%(match1,match2,match3))