智能图像分析
PYTHON实现直方图均衡化
在不调用其他python库函数的情况下完成直方图均衡化的操作并输出图像
代码
// An highlighted block
import cv2
import numpy as np
img = cv2.imread('operationbefore.png')#读图
# 转化为灰度图并获取灰度值直方图列表
# 转为灰色图片
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imshow("img_gray",img_gray)
cv2.waitKey()
gray_store = [0.0]*256
pr_store = [0]*256
# 获取图像尺寸并记录直方图列表
sp = img_gray.shape
height = sp[0]
width = sp[1]
# print(height, width)
for i in range(height):
for j in range(width):
gray_store[img_gray[i][j]] += 1
# 生成频度列表及累加列表
for i in range(len(gray_store)):
if i == 0:
pr_store[i] = (gray_store[i] * 255) / (height * width)
else:
pr_store[i] = (gray_store[i] * 255) / (height * width) + pr_store[i-1]
# 均衡化操作
for i in range(len(pr_store)):
k = 0
while pr_store[i] - k > 0:
k += 1
chazhi = k - pr_store[i]
if chazhi <= 0.5:
pr_store[i] = k
else:
pr_store[i] = k-1
# 根据直方图改变图像
for i in range(height):
for j in range(width):
img_gray[i][j] = pr_store[img_gray[i][j]]
# 显示图片
cv2.imshow("img_gray",img_gray)
cv2.waitKey()
相关基础知识链接
https://blog.csdn.net/weixin_41059269/article/details/97028518
165

被折叠的 条评论
为什么被折叠?



