OpenCV直方图均值化方法

直方图均衡化是指修改图像的像素以增强图像的对比强度的过程。
以OpenCV在python应用为例:
import sys
import cv2
import numpy as np
class Histogram_equlizate: #让较暗图片转换为明亮图片。
    def __init__(self,input_file='sunrise.jpg'):
        self.input_file=input_file
        self.img=cv2.imread(input_file)
    def Convert_to_grayscale(self):
        # Convert it to grayscale
        self.img_gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
        cv2.imshow('convert grayscale image',self.img_gray)
    def Equalize_the_histogram(self):
        # Equalize the histogram
        img_gray_histeq = cv2.equalizeHist(self.img_gray)
        cv2.imshow('Histogram equalized - grayscale', img_gray_histeq)
    def Histogram_equlizate_color_image(self):
        # Histogram equalization of color images
        img_yuv = cv2.cvtColor(self.img, cv2.COLOR_BGR2YUV)
        img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0]) #选择Y通道均值化
        img_histeq = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
        cv2.imshow('Input color image', self.img)
        cv2.imshow('Histogram equalized -Y通道', img_histeq)
        img_yuv[:,:,1] = cv2.equalizeHist(img_yuv[:,:,1]) #选择U通道均值化
        img_histeq = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
        cv2.imshow('Input color image', self.img)
        cv2.imshow('Histogram equalized - U通道', img_histeq)
        img_yuv[:,:,2] = cv2.equalizeHist(img_yuv[:,:,2]) #选择V通道均值化
        img_histeq = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
        cv2.imshow('Input color image', self.img)
        cv2.imshow('Histogram equalized - V通道', img_histeq)
        cv2.waitKey()
test1=Histogram_equlizate()
test1.Convert_to_grayscale()
test1.Equalize_the_histogram()
test1.Histogram_equlizate_color_image()

你可能感兴趣的:(机器之巅,专家之道,直方图均值化)