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()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值