python数字图像处理实现直方图与均衡化数字图像处理实现直方图与均衡化
在图像处理中,直方图是非常重要,也是非常有用的一个处理要素。这篇文章主要介绍了python数字图像处理
实现直方图与均衡化,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在图像处理中,直方图是非常重要,也是非常有用的一个处理要素。
在skimage库中对直方图的处理,是放在exposure这个模块中。
1、计算直方图、计算直方图
函数:skimage.exposure.histogram(image,nbins=256)
在numpy包中,也提供了一个计算直方图的函数histogram(),两者大同小义。
返回一个tuple(hist, bins_center), 前一个数组是直方图的统计量,后一个数组是每个bin的中间值
import numpy as np
from skimage import exposure,data
image =data.camera()*1.0
hist1=np.histogram(image, bins=2) #用numpy包计算直方图
hist2=exposure.histogram(image, nbins=2) #用skimage计算直方图
print(hist1)
print(hist2)
输出:
(array([107432, 154712], dtype=int64), array([ 0. , 127.5, 255. ]))
(array([107432, 154712], dtype=int64), array([ 63.75, 191.25]))
分成两个bin,每个bin的统计量是一样的,但numpy返回的是每个bin的两端的范围值,而skimage返回的是每个bin的中间值
2、绘制直方图、绘制直方图
绘图都可以调用matplotlib.pyplot库来进行,其中的hist函数可以直接绘制直方图。
调用方式:
复制代码 代码如下:
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='black',alpha=1,histtype='bar')
hist的参数非常多,但常用的就这六个,只有第一个是必须的,后面四个可选
arr: 需要计算直方图的一维数组
bins: 直方图的柱数,可选项,默认为10
normed: 是否将得到的直方图向量归一化。默认为0
facecolor: 直方图颜色
edgecolor: 直方图边框颜色
alpha: 透明度
histtype: 直方图类型,‘bar', ‘barstacked', ‘step', ‘stepfilled'
返回值 :
n: 直方图向量,是否归一化由参数normed设定
bins: 返回各个bin的区间范围
patches: 返回每个bin里面包含的数据,是一个list
from skimage import data
import matplotlib.pyplot as plt
img=data.camera()
plt.figure("hist")
arr=img.flatten()
n, bins, patches = plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red')
plt.show()
评论5