在python中展示图像的直方图
时间: 2024-05-06 18:18:41 浏览: 71
在Python中展示图像的直方图可以使用Matplotlib库中的hist()函数。以下是一个简单的例子:
```python
import cv2
import matplotlib.pyplot as plt
# 读取图像
img = cv2.imread('image.jpg', 0)
# 计算直方图
hist, bins = plt.hist(img.ravel(), 256, [0, 256])
# 显示直方图
plt.title('Histogram')
plt.xlabel('Intensity')
plt.ylabel('Frequency')
plt.show()
```
这个例子中,首先使用OpenCV库中的imread()函数读取一张灰度图像。然后,使用Matplotlib库中的hist()函数计算图像的直方图。最后,使用Matplotlib库中的plot()函数显示直方图。
阅读全文