灰度图像直方图(源码实现)

原理:统计每个像素灰度出现的概率
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('D:/pythonob/imageinpaint/img/flower.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
count = np.zeros(256,np.float)
for i in range(0,height):
for j in range(0,width):
pixel = gray[i,j]
index = int(pixel)
count[index] = count[index]+1
for i in range(0,256):
count[i] = count[i]/(height*width)
x = np.linspace(0,255,256)
y = count
plt.bar(x,y,0.9,alpha = 1 ,color = 'b')
plt.show()
cv2.waitKey(0)
效果图:

猜你喜欢

转载自www.cnblogs.com/cxxBoo/p/11480206.html
今日推荐