python 查看一张图片的直方图

image = cv2.imread('E:/CTAData/ALLDataINLabPCD/SourceImagesOnlyPlaque/A001916875/061.png')
cv2.imshow("Original",image)

#图像直方图
hist = cv2.calcHist([image],[0],None,[256],[0,256])

plt.figure()#新建一个图像
plt.title("Grayscale Histogram")#图像的标题
plt.xlabel("Bins")#X轴标签
plt.ylabel("# of Pixels")#Y轴标签
plt.plot(hist)#画图
plt.xlim([0,256])#设置x坐标轴范围
plt.show()#显示图像
更加方便使用和完整的code
from matplotlib import pyplot as plt
tmp = imgs_train[1,::,::,0]
tmp = tmp*255
tmp = tmp.astype(np.uint8)
#图像直方图
hist = cv2.calcHist([tmp],[0],None,[256],[0,256])
plt.figure()#新建一个图像
plt.subplot(1,2,1)
plt.title("Grayscale Histogram")#图像的标题
plt.xlabel("Bins")#X轴标签
plt.ylabel("# of Pixels")#Y轴标签
plt.plot(hist)#画图

plt.subplot(1,2,2)
plt.imshow(tmp)

 

posted @ 2019-10-08 15:52  bH1pJ  阅读(37)  评论(0编辑  收藏  举报