# -*- coding: utf-8 -*-
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
def calcGrayHist(img):
h,w=img.shape
grayHist=np.zeros(256,np.uint64)
for i in range(w):
for j in range(h):
grayHist[img[j][i]]+=1
return grayHist
if __name__=="__main__":
if len(sys.argv)>1 :
img = cv2.imread(sys.argv[1],0)
else:
print('None')
cv2.imshow('img',img)
grayHist=calcGrayHist(img)
x_range=range(256)
#画出灰度直方图
plt.plot(x_range,grayHist,linewidth=2,color='black')
#设置坐标轴范围
y_maxValue=np.max(grayHist)
plt.axis([0,255,0,y_maxValue])
plt.xlabel('gray level')
plt.ylabel('number of pixels')
#显示灰度直方图
plt.show()
h,w=img.shape
pixelSequence=img.reshape([h*w,])
numbins=256 #共多少组条状体
histogram,bins,patch=plt.hist(pixelSequence,numbins,facecolor='red',histtype='bar')
plt.xlabel('gray level')
plt.ylabel('number of pixels')
y_maxValue=np.max(hi
OpenCV学习(9)-灰度直方图
最新推荐文章于 2024-09-09 23:13:46 发布
本文介绍了一种使用Python实现灰度图像直方图绘制的方法。通过读取灰度图像,计算每个灰度级别的像素数量,并利用Matplotlib库进行可视化展示。此外,还展示了如何使用hist函数绘制相同直方图的过程。

最低0.47元/天 解锁文章
1339

被折叠的 条评论
为什么被折叠?



