代码如下:
#本质:统计每个像素灰度出现的概率 横坐标是0-255 纵坐标是出现的概率P
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('image0.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)
显示如下: