python直方图均衡代码
时间: 2023-08-31 15:07:46 浏览: 255
python直方图均衡化
以下是一个Python实现直方图均衡化的代码示例:
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 直方图均衡化函数
def hist_equal(img, z_max=255):
H, W = img.shape
S = H * W * 1.0 # 总像素数
out = img.copy()
sum_h = 0.0
for i in range(1, 255):
ind = np.where(img == i)
sum_h += len(img\[ind\])
z_prime = z_max / S * sum_h
out\[ind\] = z_prime
out = out.astype(np.uint8)
return out
# 读取图像
img = cv2.imread("../head_g_n.jpg", 0).astype(np.float)
# 直方图均衡化
out = hist_equal(img)
# 显示直方图
plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255))
plt.savefig("out_his.png")
plt.show()
# 保存结果
cv2.imshow("result", out)
cv2.imwrite("out.jpg", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这段代码使用OpenCV和NumPy库实现了直方图均衡化操作。首先读取图像,然后调用`hist_equal`函数进行直方图均衡化处理。最后,使用Matplotlib库显示直方图,并保存结果图像。
#### 引用[.reference_title]
- *1* [图像直方图均衡化算法 python实现](https://blog.csdn.net/Ibelievesunshine/article/details/104922449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [Python实现直方图均衡化](https://blog.csdn.net/m0_58058919/article/details/126925564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文