1.灰度直方图
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
img = np.array(Image.open(r'D:\DigitPictureProcess\hist02.PNG').convert('L'))
plt.figure("lena")
plt.title("Histogram")
plt.xlabel('Bins')
plt.ylabel('P(r)')
arr = img.flatten()
n, bins, patches = plt.hist(arr, bins=256, normed=1, facecolor='black', alpha=0.75)
plt.show()
或者直接调用函数 calcHist
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(r'D:\DigitPictureProcess\hist02.PNG', 0)
plt.imshow(img, 'gray')
# 图像直方图
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.figure()
plt.title("Histogram")
plt.xlabel('Bins')
plt.ylabel('# of Pixels')
plt.plot(hist)
plt.xlim([0, 256])
plt.show()