700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python 图像处理OpenCV:直方图均衡化(笔记)

Python 图像处理OpenCV:直方图均衡化(笔记)

时间:2022-08-15 04:12:06

相关推荐

Python 图像处理OpenCV:直方图均衡化(笔记)

进行了直方图的均衡化和限制对比度的直方图均衡化。

代码如下:

import cv2 as cvimport matplotlib.pyplot as plt# 直方图均衡化def img_histogram_balance(img):img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)# cv.equalizeHist(src) 用于实现图像的直方图均衡化,其输入是灰度图像,输出的是直方图均衡化的图像。result = cv.equalizeHist(img_gray)plt.title("Origin")plt.subplot(121)plt.imshow(img_gray)# 绘制原始直方图plt.subplot(122)plt.hist(img_gray.ravel(), 256)plt.show()plt.title("equalize")plt.subplot(121)plt.imshow(result)# 绘制均衡化直方图plt.subplot(122)plt.hist(result.ravel(), 256)plt.show()# 限制对比度的直方图均衡化def limit_histogram_balance(img):img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)'''cv.createCLAHE(clipLimit,tileGridSize) 限制对比度的自适应直方图均衡化clipLimit:颜色对比度的阈值tileGridSize:均衡化的网格大小,即在多少网格下进行直方图的均衡化操作,常用大小是8×8的矩阵。'''clahe = cv.createCLAHE(clipLimit=30, tileGridSize=(8, 8))result = clahe.apply(img_gray)plt.title("limit_equalize_img")plt.subplot(121)plt.imshow(result)plt.subplot(122)plt.hist(result.ravel(), 256)plt.show()if __name__ == '__main__':img = cv.imread("./image/fengjing.jpg")img_histogram_balance(img)limit_histogram_balance(img)

运行结果:

上述进行了直方图的均衡化和限制对比度的直方图均衡化。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。