灰度直方图概括了图像的灰度级信息,简单的来说就是每个灰度级图像中的像素个数以及占有率
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('hand.jpg',1)
ori = img.copy()
color = ['b','g','r']
plt.subplot(221)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(222)
for n ,col in enumerate(color):
histr = cv2.calcHist([img],[n],None,[256],[0,256])
plt.plot(histr, color=col)
plt.xlim([0, 256])
image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
plt.subplot(223)
plt.imshow(image,'gray'),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(224)
hist_full = cv2.calcHist([image],[0],None,[256],[0,256])
plt.plot(hist_full),plt.xlim([0,256])
plt.show()