【图像处理】Retinex算法用于图像亮度增强

基于Retinex 算法实现图像增强

Retinex 算法通过对图像进行对数变换和高斯模糊来估计光照分量。然后,将原始图像的对数与光照分量的对数相减,得到反射分量的估计。最后,通过对反射分量进行适当的增强和调整,可以实现图像的亮度增强。 Retinex 算法的图像亮度增强能改善图像质量,有效去除阴影和光照不均匀现象,使亮度更均匀、对比度更高,同时增强细节和纹理,让图像更清晰锐利。该算法还能增强视觉效果,通过调整亮度和对比度,使图像更生动逼真,尤其能让低光照下暗淡模糊的图像变得明亮清晰,提高视觉舒适度。此外,它能较好地保持物体颜色不变,确保图像的真实性和准确性。

dart 复制代码
'''
用于对图像进行亮度增强
'''
import cv2
import numpy as np
import argparse
# 对图像进行单尺度 Retinex 处理
def single_scale_retinex(img, sigma):
    retinex = np.log10(img) - np.log10(cv2.GaussianBlur(img, (0, 0), sigma))
    return retinex
# 对图像进行多尺度 Retinex 处理
def multi_scale_retinex(img, sigma_list):
    retinex = np.zeros_like(img)
    for sigma in sigma_list:
        retinex += single_scale_retinex(img, sigma)
    retinex = retinex / len(sigma_list)
    return retinex
# 进行颜色恢复
def color_restoration(img, alpha, beta):
    img_sum = np.sum(img, axis=2, keepdims=True)
    color_restoration = beta * (np.log10(alpha * img) - np.log10(img_sum))
    return color_restoration
# 图像增强主函数,包括图像增强和颜色恢复
def retinex_process(img, sigma_list, G, b, alpha, beta):
    img = np.float64(img) + 1.0
    img_retinex = multi_scale_retinex(img, sigma_list)
    img_color = color_restoration(img, alpha, beta)
    img_retinex = G * (img_retinex * img_color + b)
    # 将像素值限制在范围内
    for i in range(img_retinex.shape[2]):
        img_retinex[:, :, i] = np.clip(img_retinex[:, :, i], 0, 255)
    img_retinex = np.uint8(img_retinex)
    return img_retinex

def retinex(img_path,out_path):
    # 读取图像
    img = cv2.imread(img_path)
    # 尺度列表
    sigma_list = [15, 80, 250]
    # 增益参数
    G = 5.0
    # 偏置参数
    b = 25.0
    # 颜色恢复参数
    alpha = 125.0
    # 颜色恢复参数
    beta = 46.0
    # 进行图像增强
    img_retinex = retinex_process(img, sigma_list, G, b, alpha, beta)
    cv2.imwrite(out_path, img_retinex)
def getargs():
    parse = argparse.ArgumentParser()
    parse.add_argument('--img_path',default=r"E:\0-hky\mmagic-main\data\test4.png", type = str, help='input img path')
    parse.add_argument('--out_path', default=r'E:\0-hky\mmagic-main\output_quwu\test4_light.jpg',type = str, help='output img path')
    return parse.parse_args()
if __name__ == "__main__":
    opts = getargs()
    img_path = opts.img_path
    out_path = opts.out_path
    retinex(img_path,out_path)

部分测试图像和结果:



相关推荐
脚踏实地的大梦想家17 分钟前
【Go】P2 Golang 常量与变量
开发语言·后端·golang
张璐月17 分钟前
go docker-compose启动前后端分离项目 踩坑之旅
开发语言·docker·golang
.鱼子酱18 分钟前
机器学习 - 使用 ID3 算法从原理到实际举例理解决策树
算法·决策树·机器学习
和小胖112221 分钟前
第一讲 Vscode+Python+anaconda 安装
python
和小胖112225 分钟前
第二讲 Vscode+Python+anaconda 高阶环境配置
ide·vscode·python
禹曦a43 分钟前
JavaScript性能优化实战指南
开发语言·javascript·性能优化
Swift社区1 小时前
Swift 解法详解:LeetCode 371《两整数之和》
开发语言·leetcode·swift
Swift社区1 小时前
Swift 解法详解 LeetCode 362:敲击计数器,让数据统计更高效
开发语言·leetcode·swift
ytttr8731 小时前
PHP中各种超全局变量使用的过程
开发语言·php
Q741_1471 小时前
C++ 前缀和 高频笔试考点 实用技巧 牛客 DP34 [模板] 前缀和 题解 每日一题
开发语言·c++·算法·前缀和·牛客网