numpy和matplotlib绘制直方图

 使用 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], [i], None, [256], [0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.title("Matplotlib color Method")
plt.show()

结果图: 
这里写图片描述

原文地址:https://www.cnblogs.com/zhaopengcheng/p/7399147.html