您的位置:首页 > 编程语言 > Python开发

numpy和matplotlib绘制直方图

2017-08-20 10:30 871 查看
使用 Matplotlib Matplotlib 中有直方图绘制函数:matplotlib.pyplot.hist()它可以直接统计并绘制直方图。你应该使用函数 calcHist() 或 np.histogram()统计直方图。

1 使用pyplot.hist() 显示灰度图像直方图,代码如下:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('image/lufei.jpeg',0)
plt.hist(img.ravel(),255,[0,256]);
plt.title("Matplotlib Method")
plt.show()






2 使用使用函数 calcHist() 分别统计BGR 颜色通道直方图

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('image/lufei.jpeg')
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv2.calcHist(

结果图:

[img]http://img.blog.csdn.net/20170421164430773?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDY4MjM3NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" border="0" >
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: