conv_cover.jpeg
符号说明输入图像 I 输出图像为 O 宽为 W 高为 H 其中
表示 I 的第 r 行第 c 列的像素(灰度)值
灰度直方图
灰度直方图是一种以计算代价概括一幅图像灰度级的信息,通过统计图像中在每一个灰度值 (0-255) 出现次数,然后以直方图形式显示图像灰度信息。
def calc_gray_hist(img):
rows,cols = img.shape
gray_hist = np.zeros([256],np.uint64)
for r in range(rows):
for c in range(cols):
gray_hist[img[r][c]] += 1
return gray_hist
这部分代码没有什么需要解释,我们就是讲创建了 256 维数组,然后我们根据像素的灰度值放到他对应的位置上,将图像的像素值按其值划分为 256 类别然后根据像素灰度值进行统计,统计每个灰度值上像素的个数。
kiwi.jpeg
def show_img_gray_hist(path):
gray = cv2.imread(path,0)
# print gray
gray_hist = calc_gray_hist(gray)
x_range = range(256)
plt.plot(x_range,gray_hist,'r',linewidth=2,c='black')
y_maxValue = np.max(gray_hist)
plt.axis([0,255,0,y_maxValue])
plt.xlabel('gray level')
plt.ylabel('number of pixels')
plt.show()
kiwi_gray_hist.png
def show_hist_plt(path):
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rows,cols = gray.shape
pixelSequence = gray.reshape([rows*cols])
numberBins = 256
histogram, bins, patch = plt.hist(pixelSequence,numberBins,facecolor='black',histtype='bar')
plt.xlabel(u"gray Level")
plt.ylabel(u"number of pixels")
y_maxValue = np.max(histogram)
plt.axis([0,255,0,y_maxValue])
plt.show()
naruto.jpeg
naruto_hist.png