李欣睿
python实现直⽅图均衡化
直⽅图均衡化
1.⼤致思路
张九龄故居1. ⾸先求出原图⽚的直⽅图,即图⽚中每个灰度值的具体像素点数量,具体函数为cv
袖手旁观的意思2.calcHist([img],[0],None,[256],[0,255]),再除以该图⽚
的总像素点(h*w)求出其概率,并将结果放置hist_img数组。
2. 利⽤累积分布函数,设置⼀个新的数组sum_hist,求出从0到i的所有灰度值所对应的像素点数的概率,即 sum_hist[i] =
sum(hist[0:i+1])。
3. 对于新建⽴的sum_hist,要对其乘上(L-1),并且由于灰度值是整数,所以要对结果进⾏四舍五⼊。注意此时的数组存放的键值公鸡晚上打鸣是怎么回事
对,是对于每个原始图⽚的灰度值->处理之后的图⽚灰度值。
推荐菜谱
4. 最后新建图⽚equal_img,存放结果数据。
2. 具体代码实现缩写作文
加西亚的信import matplotlib .pyplot as plt import cv2import numpy as np img = "part-00264-919.jpg"def def_equalizehist (img ,L =256): img = cv2.imread (img ,0) cv2.imshow ("ori",img ) h , w = img .shape # 计算图像的直⽅图,即存在的每个灰度值的像素点数量 hist = cv2.calcHist ([img ],[0],None ,[256],[0,255]) # 计算灰度值的像素点的概率,除以所有像素点个数,即归⼀化 hist [0:255] = hist [0:255] / (h *w ) # 设置Si sum_hist = np .zeros (hist .shape ) #开始计算Si 的⼀部分值,注意i 每增⼤,Si 都是对前(i-1)的灰度值分布概率进⾏累加 for i in range (256): sum_hist [i ] = sum (hist [0:i +1]) equal_hist = np .zeros (sum_hist .shape ) #Si 再乘上灰度级,再四舍五⼊ for i in range (256): equal_hist [i ] = int (((L - 1) - 0) * sum_hist [i ] + 0.5) equal_img = img .copy () #新图⽚的创建 for i in range (h ): for j in range (w ): equal_img [i , j ] = equal_hist [img [i , j ]] # mask = np.zeros(equal_img.shape, np.uint8) # mask[100:200, 50:150]=255 # equal_hist = cv2.calcHist([equal_img], [0], mask, [256], [0, 256]) equal_hist = cv2.calcHist ([equal_img ], [0], None , [256], [0, 256]) equal_hist [0:255] = equal_hist [0:255] / (h * w ) cv2.imshow ("inver", equal_img ) # 显⽰最初的直⽅图 #plt.figure("原始图像直⽅图") plt .plot (hist , color ='b') plt .show () #plt.figure("直⽅均衡化后图像直⽅图") plt .plot (
equal_hist , color ='r') plt .show () cv2.waitKey () #return equal_hist return [equal_img , equal_hist ]def_equalizehist (img )cv2.imshow ('ss',ss )12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
英文日记格式