在图像处理中,直方图是非常重要,也是非常有用的一个处理要素。
在skimage库中对直方图的处理,是放在exposure这个模块中。
1、计算直方图
函数:skimage.exposure.histogramimage,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)
输出:
分成两个bin,每个bin的统计量是一样的,但numpy返回的是每个bin的两端的范围值,而skimage返回的是每个bin的中间值
2、绘制直方图
绘图都可以调用matplotlib.pyplot库来进行,其中的hist函数可以直接绘制直方图。
调用方式:
代码如下:
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='b