An adaptive gamma correction for image enhancement 低照度图像自适应gamma矫正(附python代码)

An adaptive gamma correction for image enhancement 低照度图像自适应gamma矫正
找代码没找到,自己用matlab写了一个,算是成功。看图对比,我判断是否是高对比度还是低对比度图像时,一开始用了用了4*sigma<=1/3 %低对比度;如图1,但是越弄越暗。倒换了一下,
if 4*sigma>=1/3 %低对比度,这样就对了,如图2。

**论文里是标准差,博主用的是方差。用标准差就正确了。
if 4 * sigma <= 1 / 3: # 低对比度
附带python代码
图1
在这里插入图片描述

流程图如下,流程图并没有细分,明亮和偏暗。
流程图
明亮和暗的分类
![明亮和暗的分类](https://i-blog.csdnimg.cn/direct/00c72a8c7914450383493c45ee743406.png

参考:An adaptive gamma correction for image enhancement 低照度图像自适应gamma矫正
文章:An adaptive gamma correction for image enhancement

参考python代码:

import cv2
import numpy as np
import os

def heaviside_custom(x):
    return np.where(x > 0, 1, 0)

# 读取RGB图像
rgb_image = cv2.imread('ACE/1/1.jpg')  # 替换为你的图像文件路径

# 将RGB图像转换为HSV
hsv_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2HSV)

# 提取V通道
v_channel = hsv_image[:, :, 2]

# 将V通道转换为双精度类型
Iin = v_channel / 255.0

# 获取 Iin 的大小
m, n = Iin.shape

# 打印矩阵的形状
print(f'Iin 的形状是 {m}{n} 列')

# 创建与 Iin 大小相同的全为1的矩阵
one_matrix = np.ones((m, n))

# 计算V通道的均值和方差
mu = np.mean(Iin)  # 均值 μ, 判断亮暗
sigma = np.std(Iin)  # 方差

# 显示结果
print(f'V通道的均值: {mu}')
print(f'V通道的标准差: {sigma}')

# 判断对比度并计算 Iout
if 4 * sigma <= 1 / 3:  # 低对比度
    print("低对比度")
    gamma = -np.log2(sigma)
    k = Iin**gamma + (one_matrix - Iin**gamma) * mu**gamma  # 保持按元素运算
    c = 1.0 / (1 + heaviside_custom(0.5 - mu) * (k - 1))  # 使用均值来简化
    if mu >= 0.5:
        print("亮图像")
        Iout = c*Iin**gamma  # 保持按元素运算
    else:
        print("暗图像")
        Iout = c*(Iin**gamma) / k  # 使用均值来简化
else:  # 高对比度
    print("高对比度")
    gamma = np.exp((1 - (mu + sigma)) / 2)
    k = Iin**gamma + (one_matrix - Iin**gamma) * mu**gamma  # 保持按元素运算
    c = 1.0 / (1 + heaviside_custom(0.5 - mu) * (k - 1))  # 使用均值来简化
    Iout = c*Iin**gamma  # 保持按元素运算

Iout1 = (Iout * 255).astype(np.uint8)

# 合并处理后的 V 通道与原始的 H 和 S 通道
hsv_image[:, :, 2] = Iout1

# 将HSV图像转换回RGB
rgb_output = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)

# 显示和保存结果图像
cv2.imshow('原始图像', rgb_image)
cv2.imshow('结果图像', rgb_output)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存输出图像
os.makedirs('ACE/2', exist_ok=True)  # 确保保存路径存在
cv2.imwrite('ACE/2/Iout.jpg', rgb_output)  # 替换为你的保存路径


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

b可为

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值