Python+OpenCV:直方图均衡化(Histogram Equalization)
####################################################################################################
# 图像直方图均衡化(Image Histogram Equalization)
def lmc_cv_image_histogram_equalization():
"""
函数功能: 图像直方图均衡化(Image Histogram Equalization)。
"""
stacking_images = []
# 灰色图像直方图均衡化
image = lmc_cv.imread('D:/99-Research/Python/Image/CountryRoad.jpg')
image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2GRAY)
equalize_image = lmc_cv.equalizeHist(image)
# stacking images side-by-side
stacking_image = np.hstack((image, equalize_image))
stacking_images.append(stacking_image)
# 彩色图像直方图均衡化
image_file_name = ['D:/99-Research/Python/Image/hist_low.jpg', 'D:/99-Research/Python/Image/hist_high.jpg',
'D:/99-Research/Python/Image/Lena.jpg']
for i in range(len(image_file_name)):
# 读取图像
image = lmc_cv.imread(image_file_name[i])
ycrcb = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2YCR_CB)
channels = lmc_cv.split(ycrcb)
channels[0] = lmc_cv.equalizeHist(channels[0])
ycrcb = lmc_cv.merge(channels)
equalize_image = lmc_cv.cvtColor(ycrcb, lmc_cv.COLOR_YCR_CB2BGR)
image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)
equalize_image = lmc_cv.cvtColor(equalize_image, lmc_cv.COLOR_BGR2RGB)
# stacking images side-by-side
stacking_image = np.hstack((image, equalize_image))
stacking_images.append(stacking_image)
# 显示图像
pyplot.figure('Image Display')
titles = ['Gray Image Histogram Equalization', 'Low Histogram Image Histogram Equalization',
'High Histogram Image Histogram Equalization', 'Nomal Histogram Image Histogram Equalization']
for i in range(len(stacking_images)):
pyplot.subplot(2, 2, i + 1)
pyplot.imshow(stacking_images[i], 'gray')
pyplot.title(titles[i])
pyplot.xticks([])
pyplot.yticks([])
pyplot.show()
# 根据用户输入保存图像
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# 销毁窗口
pyplot.close()
return