2.编程实现:读取图像,绘制图像的直方图,并对图像进行直方图均衡化,并显示均衡化之后的图像。
时间: 2024-10-16 11:14:26 浏览: 68
在编程中,特别是使用Python的PIL(Pillow)库和OpenCV等工具包处理图像时,可以按照以下步骤实现图像的直方图读取、绘制以及直方图均衡化:
1. **读取图像**:
```python
from PIL import Image
img = Image.open('image.jpg')
```
2. **计算直方图**:
```python
hist, bins = np.histogram(img.histogram(), bins=range(256))
```
3. **绘制直方图**:
```python
plt.plot(bins[:-1], hist, 'r', linewidth=2)
plt.show()
```
4. **直方图均衡化**:
```python
from skimage import exposure
eq_img = exposure.equalize_hist(img)
```
5. **显示原图像和均衡化的图像**:
```python
fig, axs = plt.subplots(nrows=1, ncols=2)
axs[0].imshow(img)
axs[1].imshow(eq_img)
axs[0].set_title('Original Image')
axs[1].set_title('Histogram Equalized Image')
plt.show()
```
注意,这里假设已经导入了必要的库如`numpy`, `matplotlib`以及`skimage.exposure`。如果是在OpenCV环境中,直方图均衡化可能会稍微有所不同。
阅读全文
相关推荐


















