加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 10:47:22 所属栏目:Python 来源:网络整理
导读:原理:统计每个像素灰度出现的概率 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(i
原理:统计每个像素灰度出现的概率
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)
效果图:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读