我们来看一个灰度图像,让表示灰度
出现的次数,这样图像中灰度为
的像素的出现概率是
是图像中所有的灰度数,
是图像中所有的像素数,
实际上是图像的直方图,归一化到
。
把 作为对应于
的累计概率函数, 定义为:
是图像的累计归一化直方图。
我们创建一个形式为 的变化,对于原始图像中的每个值它就产生一个
,这样
的累计概率函数就可以在所有值范围内进行线性化,转换公式定义为:
注意 T 将不同的等级映射到 域,为了将这些值映射回它们最初的域,需要在结果上应用下面的简单变换:
上面描述了灰度图像上使用直方图均衡化的方法,但是通过将这种方法分别用于图像RGB颜色值的红色、绿色和蓝色分量,从而也可以对彩色图像进行处理。
-
Python:
cv2.
equalizeHist
(src
[, dst
]
) → dst
-
C:
void
cvEqualizeHist
(const CvArr*
src, CvArr*
dst
)
-
Parameters: - src – Source 8-bit single channel image.
- dst – Destination image of the same size and type as src .
The function equalizes the histogram of the input image using the following algorithm:
Calculate the histogram
for src .
Normalize the histogram so that the sum of histogram bins is 255.
Compute the integral of the histogram:
Transform the image using
as a look-up table:
The algorithm normalizes the brightness and increases the contrast of the image.
# -*- coding: utf-8 -*-
#code:myhaspl@myhaspl.com
import cv2
fn="test1.jpg"
myimg=cv2.imread(fn)
img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)
newimg=cv2.equalizeHist(img)
cv2.imshow('src',img)
cv2.imshow('dst',newimg)
cv2.waitKey()
cv2.destroyAllWindows()
本博客所有内容是原创,如果转载请注明来源
http://blog.youkuaiyun.com/myhaspl/
直方图均衡化通常用来增加许多图像的全局 对比度 ,尤其是当图像的有用数据的对比度相当接近的时候。通过这种方法, 亮度 可以更好地在直方图上分布。这样就可以用于增强局部的对比度而不影响整体的对比度
本博客所有内容是原创,如果转载请注明来源
http://blog.youkuaiyun.com/myhaspl/
# -*- coding: utf-8 -*-
#code:myhaspl@myhaspl.com
#直方图均衡化
import cv2
import numpy as np
fn="test5.jpg"
myimg=cv2.imread(fn)
img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)
h=img.shape[0]
w=img.shape[1]
newimg=np.zeros((h,w),np.uint8)
scount=0.0
#原始图像灰度级
scol={}
#目标图像灰度级
dcol={}
#原始图像频度
Ps={}
#累计概率
Cs={}
#统计原始图像灰度级
for m in xrange(h):
for n in xrange(w):
scol[img[m,n]]=scol.setdefault(img[m,n],0)+1
scount+=1