图像色彩增强之python实现——MSR,MSRCR,MSRCP,autoMSRCR

转载请注明出处:https://blog.csdn.net/weixin_38285131/article/details/88097771

最近在做街景图象色彩校正方面的工作,对于过暗,过曝光,以及背光等现象,用过一些gamma校正以及其他的方法,最终选择基于Retinex原理的方法对这几种现象都有一定的增强效果。

Retinex理论基于一下假设:
1.真实世界是无颜色的,我们所感知的颜色是光与物质的相互作用的结果。我们见到的水是无色的,但是水膜—肥皂膜却是显现五彩缤纷,那是薄膜表面光干涉的结果。
2.每一颜色区域由给定波长的红、绿、蓝三原色构成的;
3.三原色决定了每个单位区域的颜色。

Retinex理论的基础理论是物体的颜色是由物体对长波(红色)、中波(绿色)、短波(蓝色)光线的反射能力来决定的,而不是由反射光强度的绝对值来决定的,物体的色彩不受光照非均匀性的影响,具有一致性,即retinex是以色感一致性(颜色恒常性)为基础的。不同于传统的线性、非线性的只能增强图像某一类特征的方法,Retinex可以在动态范围压缩、边缘增强和颜色恒常三个方面达到平衡,因此可以对各种不同类型的图像进行自适应的增强。
40多年来,研究人员模仿人类视觉系统发展了Retinex算法,从单尺度Retinex算法,MSR改进成多尺度加权平均的MSR算法,再发展成彩色恢复多尺度MSRCR算法色彩增益加权的AutoMSRCR算法
主要算法公式介绍可以参考如下博客:
https://blog.csdn.net/ajianyingxiaoqinghan/article/details/71435098
我再对这几种方法稍稍总结一下:

一丶单尺度的Retinex——SSR
可以理解为图像分解,一幅图像S(x,y)可以分为他的光照图象 I(x,y)和反射图像R(x,y),反射图象是根据物体本身的反射特性,所以基本不会发生变化,光照图像是根据环境明暗来决定的。

只看公式的话感觉一下就看懵逼了,我感觉就三个步骤:

1)将图像进行log变换
2)然后将log图像进行高斯模糊
3)利用原图和模糊之后的log图像做差分

二丶多尺度的Retinex——MSR
通俗解释:就是再多个单尺度Retinex做平均,区别是在第二步高斯模糊是选择的sigma是不同的

原始图像进行三次SSR
高斯模糊选择15,80,200作为高斯模糊sigma参数
对三次的SSR结果做平均即为MSR图像

三丶彩色恢复多尺度Retinex——MSRCR,MSRCP等

对多尺度MSR结果做了色彩平衡,归一化,增益和偏差线性加权

四丶参数说明

{
“sigma_list”: [15, 80, 200],多尺度高斯模糊sigma值
“G” : 5.0,增益
“b” : 25.0,偏差
“alpha” : 125.0,
“beta” : 46.0,
“low_clip” : 0.01,
“high_clip” : 0.99
}

五丶图像增强结果:

在这里插入图片描述
在这里插入图片描述

六丶代码

retinex.py

import numpy as np
import cv2

def singleScaleRetinex(img, sigma):

    retinex = np.log10(img) - np.log10(cv2.GaussianBlur(img, (0, 0), sigma))

    return retinex

def multiScaleRetinex(img, sigma_list):

    retinex = np.zeros_like(img)
    for sigma in sigma_list:
        retinex += singleScaleRetinex(img, sigma)

    retinex = retinex / len(sigma_list)

    return retinex

def colorRestoration(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 simplestColorBalance(img, low_clip, high_clip):    

    total = img.shape[0] * img.shape[1]
    for i in range(img.shape[2]):
        unique, counts = np.unique(img[:, :, i], return_counts=True)
        current = 0
        for u, c in zip(unique, counts):            
            if float(current) / total < low_clip:
                low_val = u
            if float(current) / total < high_clip:
                high_val = u
            current += c
                
        img[:, :, i] = np.maximum(np.minimum(img[:, :, i], high_val), low_val)

    return img    

def MSRCR(img, sigma_list, G, b, alpha, beta, low_clip, high_clip):

    img = np.float64(img) + 1.0

    img_retinex = multiScaleRetinex(img, sigma_list)

    img_color = colorRestoration(img, alpha, beta)    
    img_msrcr = G * (img_retinex * img_color + b)

    for i in range(img_msrcr.shape[2]):
        img_msrcr[:, :, i] = (img_msrcr[:, :, i] - np.min(img_msrcr[:, :, i])) / \
                             (np.max(img_msrcr[:, :, i]) - np.min(img_msrcr[:, :, i])) * \
                             255
    
    img_msrcr = np.uint8(np.minimum(np.maximum(img_msrcr, 0), 255))
    img_msrcr = simplestColorBalance(img_msrcr, low_clip, high_clip)       

    return img_msrcr

def automatedMSRCR(img, sigma_list):

    img = np.float64(img) + 1.0

    img_retinex = multiScaleRetinex(img, sigma_list)

    for i in range(img_retinex.shape[2]):
        unique, count = np.unique(np.int32(img_retinex[:, :, i] * 100), return_counts=True)
        for u, c in zip(unique, count):
            if u == 0:
                zero_count = c
                break
            
        low_val = unique[0] / 100.0
        high_val = unique[-1] / 100.0
        for u, c in zip(unique, count):
            if u < 0 and c < zero_count * 0.1:
                low_val = u / 100.0
            if u > 0 and c < zero_count * 0.1:
                high_val = u / 100.0
                break
            
        img_retinex[:, :, i] = np.maximum(np.minimum(img_retinex[:, :, i], high_val), low_val)
        
        img_retinex[:, :, i] = (img_retinex[:, :, i] - np.min(img_retinex[:, :, i])) / \
                               (np.max(img_retinex[:, :, i]) - np.min(img_retinex[:, :, i])) \
                               * 255

    img_retinex = np.uint8(img_retinex)
        
    return img_retinex

def MSRCP(img, sigma_list, low_clip, high_clip):

    img = np.float64(img) + 1.0

    intensity = np.sum(img, axis=2) / img.shape[2]    

    retinex = multiScaleRetinex(intensity, sigma_list)

    intensity = np.expand_dims(intensity, 2)
    retinex = np.expand_dims(retinex, 2)

    intensity1 = simplestColorBalance(retinex, low_clip, high_clip)

    intensity1 = (intensity1 - np.min(intensity1)) / \
                 (np.max(intensity1) - np.min(intensity1)) * \
                 255.0 + 1.0

    img_msrcp = np.zeros_like(img)
    
    for y in range(img_msrcp.shape[0]):
        for x in range(img_msrcp.shape[1]):
            B = np.max(img[y, x])
            A = np.minimum(256.0 / B, intensity1[y, x, 0] / intensity[y, x, 0])
            img_msrcp[y, x, 0] = A * img[y, x, 0]
            img_msrcp[y, x, 1] = A * img[y, x, 1]
            img_msrcp[y, x, 2] = A * img[y, x, 2]

    img_msrcp = np.uint8(img_msrcp - 1.0)

    return img_msrcp

run.py

import sys
import os

import cv2
import json

import retinex

data_path = 'data'
img_list = os.listdir(data_path)
if len(img_list) == 0:
    print( 'Data directory is empty.')
    exit()

with open('config.json', 'r') as f:
    config = json.load(f)

for img_name in img_list:
    if img_name == '.gitkeep':
        continue
    
    img = cv2.imread(os.path.join(data_path, img_name))

    print('msrcr processing......')
    img_msrcr = retinex.MSRCR(
        img,
        config['sigma_list'],
        config['G'],
        config['b'],
        config['alpha'],
        config['beta'],
        config['low_clip'],
        config['high_clip']
    )
    cv2.imshow('MSRCR retinex', img_msrcr)
    cv2.imwrite("MSRCR_retinex.tif",img_msrcr);


    print('amsrcr processing......')
    img_amsrcr = retinex.automatedMSRCR(
        img,
        config['sigma_list']
    )
    cv2.imshow('autoMSRCR retinex', img_amsrcr)
    cv2.imwrite('AutomatedMSRCR_retinex.tif', img_amsrcr)


    print('msrcp processing......')
    img_msrcp = retinex.MSRCP(
        img,
        config['sigma_list'],
        config['low_clip'],
        config['high_clip']        
    )    

    shape = img.shape
    cv2.imshow('Image', img)

    cv2.imshow('MSRCP', img_msrcp)
    cv2.imwrite('MSRCP.tif', img_msrcp)
    cv2.waitKey()
  • 62
    点赞
  • 550
    收藏
    觉得还不错? 一键收藏
  • 82
    评论
SSR、MSRMSRCR都是图像增强算法。其中SSR(Single-Scale Retinex)是一种基于多尺度空间的算法,通过对原始图像的亮度和反射成分进行分离,对反射成分进行增强,以提高图像的对比度和细节。引用中提到,SSR的公式部分为r=s-l=logS-logL,其中原始图像为S(x, y),反射图像为R(x, y),亮度图像为L(x, y)。 MSR(Multi-Scale Retinex)也是一种多尺度空间的图像增强算法,其原理与SSR类似,也是通过分离亮度和反射成分来增强图像。不同之处在于,MSR考虑了多个尺度的信息,以更好地适应不同尺度的细节。 MSRCR(Multi-Scale Retinex with Color Restoration)是在MSR算法的基础上进行了改进,主要用于彩色图像增强MSRCR算法通过对图像的亮度和颜色进行分离,对颜色进行修复,以提高彩色图像的质量和视觉效果。引用中提到,MSRCR算法在处理图像后,像素值一般会出现负值,因此需要通过改变增益和偏差来对图像进行修正。 总的来说,SSR、MSRMSRCR都是通过分离图像的亮度和反射成分,对反射成分进行增强,以提高图像的对比度和细节。其中,MSRCR算法还考虑了颜色的修复。这些算法的具体实现细节,包括增益和偏差的取值,取决于算法的软件实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【OpenCV】Retinex图像增强(SSR,MSRMSRCR)](https://blog.csdn.net/Gordon_Wei/article/details/102173309)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Retinex图像增强算法(SSR, MSR, MSRCR)详解](https://blog.csdn.net/Julialove102123/article/details/89312058)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 82
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值