PYTHON实现直方图均衡化

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值