数字图像处理第三章实验笔记(一)

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()

发布了33 篇原创文章 · 获赞 3 · 访问量 1908

猜你喜欢

转载自blog.csdn.net/FeNGQiHuALOVE/article/details/105274563