【Retinex】【Frankle-McCann Retinex】matlab代码注释

function Test()
    clear all
    rgb=imread('c:\222.jpg');%需要处理的图片
    m=size(rgb,1);           %图像的行数,也就是高度, 行数对应高
    n=size(rgb,2);           %图像的列数,也就是宽度,列数对应宽
    rr=zeros(m,n);           %定义三个同样大小的变量存放图片
    gg=zeros(m,n);           %同上
    bb=zeros(m,n);           %同上
    for i=1:m
        for j=1:n
            rr(i,j)=logm(double(rgb(i,j,1))+eps);
            gg(i,j)=logm(double(rgb(i,j,2))+eps);
            bb(i,j)=logm(double(rgb(i,j,3))+eps);
        end
    end
    rr=rr/max(max(rr(:)));
    gg=gg/max(max(gg(:)));
    bb=bb/max(max(bb(:)));
    rrr= retinex_frankle_mccann(rr, 4);
    ggg= retinex_frankle_mccann(gg, 4);
    bbb= retinex_frankle_mccann(bb, 4);
    for i=1:m
        for j=1:n
            rrr(i,j)=round(exp(rrr(i,j)*5.54));
            ggg(i,j)=round(exp(ggg(i,j)*5.54));
            bbb(i,j)=round(exp(bbb(i,j)*5.54));
        end
    end
    rgb=cat(3,uint8(rrr),uint8(ggg),uint8(bbb));
    rgb=max(min(rgb,255),0);
    imshow(rgb);
end
%也就是说进行Frankle_mccann算法处理彩色图像时,需要对三个颜色通道图像分别进行处理,然后在进行合并

function [ Retinex ] = retinex_frankle_mccann( L, nIterations )
    global RR IP OP NP Maximum
    RR = L;
    Maximum = max(L(:));                                 % maximum color value in the image
    [nrows, ncols] = size(L);
    shift = 2^(fix(log2(min(nrows, ncols)))-1);          % initial shift
    OP = Maximum*ones(nrows, ncols);                     % initialize Old Product
    while (abs(shift) >= 1)
        for i = 1:nIterations
            CompareWith(0, shift);                         % horizontal step
            CompareWith(shift, 0);                         % vertical step
        end
        shift = -shift/2;                                 % update the shift
    end
    Retinex = NP;
end

function CompareWith(s_row, s_col)
    global RR IP OP NP Maximum
    IP = OP;
    if (s_row + s_col > 0)
        IP((s_row+1):end, (s_col+1):end) = OP(1:(end-s_row), 1:(end-s_col)) + ...
            RR((s_row+1):end, (s_col+1):end) - RR(1:(end-s_row), 1:(end-s_col));
    else
        IP(1:(end+s_row), 1:(end+s_col)) = OP((1-s_row):end, (1-s_col):end) + ...
            RR(1:(end+s_row),1:(end+s_col)) - RR((1-s_row):end, (1-s_col):end);
    end
    IP(IP > Maximum) = Maximum;                          % The Reset operation
    NP = (IP + OP)/2;                                    % average with the previous Old Product
    OP = NP;                                             % get ready for the next comparison
end


 

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Retinex算法可以有效地去除水下图像中的背景光和噪声,提高图像的对比度和清晰度。以下是一个基于Retinex算法的MATLAB代码示例,用于实现水下图像的增强函数: ```matlab function out = retinex_underwater(I, alpha, beta, gamma, sigma) % I: 输入的水下图像 % alpha: 水的吸收系数 % beta: 水的散射系数 % gamma: 水下环境的反射系数 % sigma: 高斯核宽度 % 将图像转换为双精度浮点类型 I = im2double(I); % 计算水下图像的退化模型 J = double(I); for i = 1:size(J,1) for j = 1:size(J,2) J(i,j,:) = J(i,j,:) * exp(-alpha*(i+j)) + beta*255*(1-exp(-alpha*(i+j))); end end % 估计背景光 bg = imopen(J, strel('disk', 8)); % 分离背景光 out = J - bg; % 进行Retinex增强 G = fspecial('gaussian', 11, sigma); out_log = log(out + 1); out_log_conv = zeros(size(out_log)); for i = 1:3 out_log_conv(:,:,i) = conv2(out_log(:,:,i), G, 'same'); end out_res = exp(out_log - out_log_conv); % 调整亮度和对比度 out_res = (out_res - min(out_res(:))) / (max(out_res(:)) - min(out_res(:))); out_res = out_res.^gamma; % 将输出图像转换为uint8类型 out = uint8(out_res * 255); end ``` 这个函数实现了对水下图像的Retinex增强,包括以下步骤: 1. 根据输入的水下图像和物理模型参数(吸收系数、散射系数、反射系数),计算水下图像的退化模型,估计并分离背景光。 2. 利用高斯滤波器和Retinex算法,对分离后的图像进行增强,得到去除背景光的增强图像。 3. 对增强后的图像进行亮度和对比度的调整,最终输出uint8类型的图像。 需要注意的是,在实际应用中,需要根据具体的水下环境和图像特征,调整物理模型参数和Retinex算法中的其他参数,以达到最佳的增强效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无敌三角猫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值