美文网首页
04灰度直方图_源码

04灰度直方图_源码

作者: 犬夜叉写作业 | 来源:发表于2019-07-18 20:21 被阅读0次
# 1 0-255 2 概率 
# 本质:统计每个像素灰度 出现的概率 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,255):
    count[i] = count[i]/(height*width)

#使用numpy进行绘图
x = np.linspace(0,255,256)     #x轴
y = count
plt.bar(x,y,0.9,alpha=1,color='b')  #0.9表示每一个bar所占的百分比,此处是百分之90
plt.show()
cv2.waitKey(0)

image.png

相关文章

网友评论

      本文标题:04灰度直方图_源码

      本文链接:https://www.haomeiwen.com/subject/vqurlctx.html