赞
踩
目录
直方图均匀化是通过拉伸像素强度的分布范围,使得在0~255灰阶上的分布更加均匀,提高图像的对比度。达到改善图像主管视觉效果的目的。对比度较低的图像适合使用直方图均衡化的方法来增强图像细节。
原理
最初的像素点都在0-7之间,最后我们将其规划到0~255中间。
使用API---eqyalizeHist(src[, dst)
示例代码如下:
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- cv2.namedWindow("img", cv2.WINDOW_NORMAL)
- cv2.resizeWindow("img", 1290, 480)
- lena = cv2.imread("beautiful women.png")
- gray = cv2.cvtColor(lena, cv2.COLOR_BGR2GRAY)
-
- # lena变黑
- gray_dark = gray - 40
- # lena变亮
- gray_bright = gray + 40
- cv2.imshow("img", np.hstack((gray, gray_dark, gray_bright)))
- # 查看各自的直方图
- hist_gray = cv2.calcHist([gray], [0], None, [256], [0, 255])
- hist_dark = cv2.calcHist([gray_dark], [0], None, [256], [0, 255])
- hist_bright = cv2.calcHist([gray_bright], [0], None, [256], [0, 255])
- # 画出直方图
- plt.plot(hist_gray, label = "gray")
- plt.plot(hist_dark, label = "dark")
- plt.plot(hist_bright, label = "bright")
- plt.legend()
- plt.show()
- # 进行均衡化处理
- dark_equ = cv2.equalizeHist(gray_dark)
- bright_equ = cv2.equalizeHist(gray_bright)
- # 查看均衡化的直方图
- hist_dark_equ = cv2.calcHist([dark_equ], [0], None, [256], [0, 255])
- hist_bright_equ = cv2.calcHist([bright_equ], [0], None, [256], [0, 255])
- plt.plot(hist_dark_equ, label = "dark_equ")
- plt.plot(hist_bright_equ, label = "bright_equ")
- plt.legend()
- plt.show()
- cv2.imshow("gray_dark", np.hstack((gray_dark, dark_equ)))
- cv2.imshow("gray_dark", np.hstack((gray_bright, bright_equ)))
-
- cv2.waitKey(0)
- cv2.destroyAllWindows()
输出结果如下:
图像均衡化之前:
图像均衡化之后:
直方图均衡化之前:
直方图均衡化之后:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。