直方图均衡化未调用封装函数python代码
时间: 2024-05-16 17:11:55 浏览: 77
以下是一种直方图均衡化的Python代码实现,未使用封装函数:
```python
import cv2
import numpy as np
def histogram_equalization(img):
# Compute histogram
hist, bins = np.histogram(img.flatten(), 256, [0,256])
# Compute cumulative distribution function
cdf = hist.cumsum()
cdf_normalized = cdf * hist.max() / cdf.max()
# Apply equalization
img_equalized = np.interp(img.flatten(), bins[:-1], cdf_normalized)
img_equalized = img_equalized.reshape(img.shape)
return img_equalized
# Load image
img = cv2.imread('image.jpg', 0)
# Apply histogram equalization
img_equalized = histogram_equalization(img)
# Display images
cv2.imshow('Original Image', img)
cv2.imshow('Equalized Image', img_equalized)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
该代码首先使用`np.histogram`函数计算输入图像的直方图,然后计算累积分布函数,并将其归一化。最后,使用`np.interp`函数将输入图像中的像素值映射到新的像素值上,以实现直方图均衡化。最终结果通过`cv2.imshow`函数显示。
阅读全文