import cv2
import numpy as np
from matplotlib import pyplot as plt
__author__ = "zxsuperstar"
__email__ = "zxsuperstar@163.com"
"""
图像直方图:一维与多维
"""
def plt_demo(image):
plt.hist(image.ravel(),256,[0,256])
plt.show("直方图")
def image_hist(image):
color = ("blue","green","red")
for i ,color in enumerate(color):
hist = cv2.calcHist([image],[i],None,[256],[0,256])
plt.plot(hist,color=color)
plt.xlim([0,256])
plt.show()
if __name__ == "__main__":
src = cv2.imread("t.jpg") #blue green red
# cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("image",src)
# plt_demo(src)
image_hist(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
1.numpy的ravel函数功能是将多维数组降为一维数组。
2.matplotlib.pyplot.hist函数主要是计算直方图。
hist函数原型:hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vert