""" Contrast Limited Adaptive Histogram Equalization,CLAHE 对比度受限自适应直方图均衡 """ import cv2 # import numpy as np import matplotlib.pyplot as plt def show_img_with_matplotlib(color_img, title, pos): img_rgb = color_img[:, :, ::-1] plt.subplot(2, 5, pos) plt.imshow(img_rgb) plt.title(title, fontsize=8) plt.axis('off') def equalize_clahe_color_hsv(img): cla = cv2.createCLAHE(clipLimit=4.0) H, S, V = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) eq_V = cla.apply(V) eq_image = cv2.cvtColor(cv2.merge([H, S, eq_V]), cv2.COLOR_HSV2BGR) return eq_image def equalize_clahe_color_lab(img): cla = cv2.createCLAHE(clipLimit=4.0) L, a, b = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2Lab)) eq_L = cla.apply(L) eq_image = cv2.cvtColor(cv2.merge([eq_L, a, b]), cv2.COLOR_Lab2BGR) return eq_image def equalize_clahe_color_yuv(img): cla = cv2.createCLAHE(clipLimit=4.0) Y, U, V = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2YUV)) eq_Y = cla.apply(Y) eq_image = cv2.cvtColor(cv2.merge([eq_Y, U, V]), cv2.COLOR_YUV2BGR) return eq_image def equalize_clahe_color(img): cla = cv2.createCLAHE(clipLimit=4.0) channels = cv2.split(img) eq_channels = [] for ch in channels: eq_channels.append(cla.apply(ch)) eq_image = cv2.merge(eq_channels) return eq_image # 加载图像 image = cv2.imread('D:/Documents/python/OpenCV/image/008.jpg') gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 灰度图像应用 CLAHE clahe = cv2.createCLAHE(clipLimit=2.0) gray_image_clahe = clahe.apply(gray_image) # 使用不同 clipLimit 值 clahe.setClipLimit(5.0) gray_image_clahe_2 = clahe.apply(gray_image) clahe.setClipLimit(10.0) gray_image_clahe_3 = clahe.apply(gray_image) clahe.setClipLimit(20.0) gray_image_clahe_4 = clahe.apply(gray_image) # 彩色图像应用 CLAHE image_clahe_color = equalize_clahe_color(image) image_clahe_color_lab = equalize_clahe_color_lab(image) image_clahe_color_hsv = equalize_clahe_color_hsv(image) image_clahe_color_yuv = equalize_clahe_color_yuv(image) # 标题 plt.figure(figsize=(10, 4)) plt.suptitle("Color histogram equalization with cv2.equalizedHist() - not a good approach", fontsize=9, fontweight='bold') # 可视化 show_img_with_matplotlib(cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR), "gray", 1) show_img_with_matplotlib(cv2.cvtColor(gray_image_clahe, cv2.COLOR_GRAY2BGR), "gray CLAHE clipLimit=2.0", 2) show_img_with_matplotlib(cv2.cvtColor(gray_image_clahe_2, cv2.COLOR_GRAY2BGR), "gray CLAHE clipLimit=5.0", 3) show_img_with_matplotlib(cv2.cvtColor(gray_image_clahe_3, cv2.COLOR_GRAY2BGR), "gray CLAHE clipLimit=10.0", 4) show_img_with_matplotlib(cv2.cvtColor(gray_image_clahe_4, cv2.COLOR_GRAY2BGR), "gray CLAHE clipLimit=20.0", 5) show_img_with_matplotlib(image, "color", 6) show_img_with_matplotlib(image_clahe_color, "clahe on each channel(BGR)", 7) show_img_with_matplotlib(image_clahe_color_lab, "clahe on each channel(LAB)", 8) show_img_with_matplotlib(image_clahe_color_hsv, "clahe on each channel(HSV)", 9) show_img_with_matplotlib(image_clahe_color_yuv, "clahe on each channel(YUV)", 10) plt.show()
时间: 2023-06-04 16:06:43 浏览: 214
CLAHE,即对比度受限自适应直方图均衡化,是一种用于增强图像对比度的方法。在计算图像直方图均衡化的过程中,CLAHE会先将图像分成许多小块,并对每个小块进行直方图均衡化。由于小块内的像素值范围较小,采取均衡化的结果会使得低对比度的区域增强,同时避免出现像素值过饱和的情况。CLAHE能够在保持图像整体视觉质量的同时,突出图像细节。在OpenCV库中,可以通过cv2.createCLAHE()函数来调用CLAHE算法。
阅读全文