(六)直方图均衡化

直方图均衡化

一、定义及作用

定义:直方图均衡化是将原图像通过某种变换,得到一幅灰度直方图为均匀分布的新图像的方法。

作用:直方图均衡化方法的基本思想是对在图像中像素个数多的灰度级进行展宽,而对像素个数少的灰度级进行缩减。从而达到清晰图像的目的。

二、理论原理

1、映射函数的条件

为了将原图像的亮度范围进行扩展,需要一个映射函数,将原图像的像素值均衡映射到新直 方图中,根据直方图的定义,这个映射函数有两个条件:
1. 为了不打乱原有的顺序,映射后亮、暗的大小关系不能改变,
2. 映射后必须在原有的范围内,比如(0-255)

2、映射函数的选取

1. 对于输入图像的任意一个像素p, p∈[0,255], 总能在输出图像里有对应的像素q, q∈[0,255] 使得下面等式成
(输入和输出的像素总量相等)
在这里插入图片描述
其中、hist(k)即灰度级为k的像素个数

2. 其中,输出图像每个灰度级的个数(我们所希望的映射输出):
在这里插入图片描述

3.代入累加直方图公式,反求出对应关系: 在这里插入图片描述

计算实例

在这里插入图片描述

python实现

import cv2
import matplotlib.pyplot as plt

def histogram(img_gray):
    '''
    求灰度图像的直方图
    :param img_gray: 灰度图像
    :return:
    '''
    histogram = [0 for x in range(256)]
    h,w = img_gray.shape
    for i in range(h):
        for j in range(w):
            histogram[img_gray[i][j]] = histogram[img_gray[i][j]]+1
    return histogram

def histogram_equalization(img_gray):
    '''
    灰度图直方图均衡化
    :param img_gray: 灰度图像
    :return: 灰度图直方图均衡化后的图像
    '''
    histogram_data = histogram(img_gray)
    Pi = [0 for x in range(256)]
    SumPi = []
    sum = 0
    h, w = img_gray.shape
    total_pixels = h*w
    for i in range(len(histogram_data)):
        Pi[i] = histogram_data[i] / total_pixels
        sum = sum + Pi[i]
        SumPi.append(sum)
    equalization = {}
    for i in range(len(histogram_data)):
        equalization[i] = round(SumPi[i] * 256-1 )
    # return equalization
    for i in range(h):
        for j in range(w):
            img_gray[i][j] = equalization[img_gray[i][j]]
    return img_gray


# 设置matplotlib正常显示中文和负号
plt.rcParams['font.sans-serif']=['SimHei']   # 用黑体显示中文
plt.rcParams['axes.unicode_minus']=False     # 正常显示负号

img = cv2.imread('lenna.png',1)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
plt.subplot(221)
plt.imshow(img_gray,cmap = "gray")
plt.title("灰度图")

plt.subplot(222)
hist_x = [x for x in range(256)]
plt.xticks(range(0,255,25))
hist = histogram(img_gray)
plt.bar(hist_x,hist,width=1)
plt.title("灰度直方图")


plt.subplot(223)
img_equal = histogram_equalization(img_gray)
plt.imshow(img_equal,cmap = "gray")
plt.title("均衡化")

plt.subplot(224)
hist_equal = histogram(img_equal)
plt.bar(hist_x,hist_equal,width=1)
plt.title("均衡化后的直方图")
plt.show()

def RGB_histogram(img):
    '''
    求彩色图像的直方图
    :param img: BGR图像或者RGB图像
    :return:
    '''
    Bhistogram = histogram(img[:,:,0])
    Ghistogram = histogram(img[:,:,1])
    Rhistogram = histogram(img[:,:,2])
    return Bhistogram,Ghistogram,Rhistogram

def RGB_histogram_equalization(img):
    img[:, :, 0] = histogram_equalization(img[:,:,0])
    img[:, :, 1] = histogram_equalization(img[:,:,1])
    img[:, :, 2] = histogram_equalization(img[:,:,2])
    return img
plt.subplot(221)
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img1)
plt.title("彩色图")

plt.subplot(222)
BGRhistogram = RGB_histogram(img)
plt.bar(hist_x,BGRhistogram[0],color="blue",width=1)
plt.bar(hist_x,BGRhistogram[1],color="green",width=1)
plt.bar(hist_x,BGRhistogram[2],color="red",width=1)
plt.title("彩色图的直方图")

plt.subplot(223)
RGB_img_equal = RGB_histogram_equalization(img)
RGB_img_equal1 = cv2.cvtColor(RGB_img_equal,cv2.COLOR_BGR2RGB)
plt.imshow(RGB_img_equal1)
plt.title("彩色图均衡化")

plt.subplot(224)
RGB_hist_equal = RGB_histogram(RGB_img_equal)
plt.bar(hist_x,RGB_hist_equal[0],color="blue",width=1)
plt.bar(hist_x,RGB_hist_equal[1],color="green",width=1)
plt.bar(hist_x,RGB_hist_equal[2],color="red",width=1)
plt.title("彩色图均衡化后的直方图")
plt.show()
cv2.imshow("RGB_img_equal",RGB_img_equal)
cv2.waitKey(0)

在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值