怎么对图片直方图进行均衡化处理?

2025-10-22 09:30:41

1、获取第一个通道的像素值统计数据:

import cv2

import numpy as np

from matplotlib import pyplot as plt

img = cv2.imread('b.png',0)

h,bins = np.histogram(img.flatten(),256,[0,256])

怎么对图片直方图进行均衡化处理?

2、bins就是相应的亮度(像素值)。

怎么对图片直方图进行均衡化处理?

3、绘制直方图:

plt.plot(h,color = 'g')

这是第一个通道,但是使用绿色。

怎么对图片直方图进行均衡化处理?

4、累计小于某个像素值的像素数目:

cdf = h.cumsum()

嗯,cdf里面的数字只会递增或持平,不会下降。

怎么对图片直方图进行均衡化处理?

5、把这个累计图画出来:

plt.plot(cdf,color = 'g')

plt.show()

怎么对图片直方图进行均衡化处理?

6、把累计图压扁:

cdf_normalized = cdf*h.max()/cdf.max()

把压扁的累计图和直方图画到一起:

plt.plot(h, color = 'g')

plt.plot(cdf_normalized, color = 'b')

怎么对图片直方图进行均衡化处理?

怎么对图片直方图进行均衡化处理?

7、用orange来填充直方图:

plt.plot(h, color = 'g')

plt.plot(cdf_normalized, color = 'b')

plt.hist(img.flatten(),256,[0,256], color = 'orange')

怎么对图片直方图进行均衡化处理?

8、增高原图的对比度:

cdf_m = np.ma.masked_equal(cdf,0)

cdf_m = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())

cdf = np.ma.filled(cdf_m,0).astype('uint8')

img2 = cdf[img]

cv2.imwrite('g.png',img2)

怎么对图片直方图进行均衡化处理?

9、我们看看原图“b.png”的直方图。

怎么对图片直方图进行均衡化处理?

10、在看看g.png的直方图。

怎么对图片直方图进行均衡化处理?

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢