OpenCV+Python实现彩色图像直方图全局均衡化和自适应均衡化

https://blog.csdn.net/Ibelievesunshine/article/details/95220075

# 彩色图像全局直方图均衡化
def hisEqulColor1(img):
    # 将RGB图像转换到HSV空间中
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    # 将hsv图像通道分离
    channels = cv2.split(hsv)
    # 对第2个通道即亮度通道进行全局直方图均衡化并保存
    cv2.equalizeHist(channels[2], channels[2])
    # 将处理后的通道和没有处理的两个通道合并,命名为hsv
    cv2.merge(channels, hsv)
    # 将hsv图像转换回RGB图像
    cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB, img)
    return img


# 彩色图像进行自适应直方图均衡化,代码同上的地方不再添加注释
def hisEqulColor2(img):
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    channels = cv2.split(hsv)

    # 以下代码详细注释见官网:
    # https://docs.opencv.org/4.1.0/d5/daf/tutorial_py_histogram_equalization.html
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    clahe.apply(channels[2], channels[2])

    cv2.merge(channels, hsv)
    cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB, img)
    return img

 

你可能感兴趣的:(Python)