opencv算法,直方图均衡化
import sys
import cv2
import numpy as np
加载彩色图像,日落照片为例
input_file = “sunset.jpg”
img = cv2.imread(input_file)
转化为灰度图像
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow(‘Input grayscale image’, img_gray)
灰度图像直方图均衡
img_gray_histeq = cv2.equalizeHist(img_gray)
cv2.imshow(‘Histogram equalized - grayscale’, img_gray_histeq)
将彩色图转换到YUV色彩空间
img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
均衡Y通道
img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])
转回BGR图像
img_histeq = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
cv2.imshow(‘Input color image’, img)
cv2.imshow(‘Histogram equalized - color’, img_histeq)
cv2.waitKey()