一、简介

将原始影像从RGB色彩空间转换到符合人眼视觉特性的HSV色彩空间进行增强处理的主要目的是将颜色分量与亮度分量及饱和度分量分离,保证颜色分量不变,调整亮度分量和饱和度分量。这样能够避免传统方法在RGB色彩空间中分别对三个颜色通道进行调整而导致的颜色失真问题,使增强后的影像更加符合人眼视觉特性。RGB色彩空间向HSV色彩空间的转换是从基于笛卡尔直角坐标系的单位立方体向基于圆柱极坐标双锥体的转换,其转换的基本原则是分离出RGB中的亮度分量,并将色度分解为色调和饱和度两个分量,用角向量表示色调。
传统的Retinex算法采用高斯函数作为其环绕函数,将MSR算法中的高斯函数用双边滤波函数代替,双边滤波与高斯滤波相比,可以同时增强空域和时域的平滑性,避免了高斯滤波的误判问题,可以更好地保持图像的边缘信息,使得处理后的图像边缘更加光滑连续,还能在一定程度上解决图像的光晕问题。其中sigma_s取值为[15 70 110],其中sigma_r取值为[0.05 0.10 0.15]。
为了使图像的色彩更加自然,对经过双边滤波处理后的入射分量进行伽马变换,其中C =0.77;Gamma =[0.05 0.1 0.3]。

二、源代码

clear all;%clear the values
clc;%clear the command window
tic
strhead = \'5\';%the name of file
strtail = \'.bmp\' ;% the format of the file
str = strcat(strhead,strtail);
img = im2double(imread(str));%read the image and convert to double.
N = 15;%the size of filter
sigma = [100 , 0.3];%the parameters of bilateral filter 
retimg = bialteral(img , N , sigma );%get the illumination image

subplot(1,3,1);imshow(img);title(\'the original image\');
subplot(1,3,2);imshow(retimg);title(\'the illumination image\');

%% s-l
img_copy = rgb2hsv(img);
img_copy3 = log(img_copy(:,:,3));
retimg_copy = rgb2hsv(retimg);
retimg_copy3 = log(retimg_copy(:,:,3));%only to v layer
r_img = img_copy3 - retimg_copy3;
r_img = exp(r_img);
N = 4;
sigma = [100,0.3];
retinex_img(:,:,3) = bialteral2(r_img,N,sigma);%get reflect image

dim = size(img);
for i = 1:dim(1)
     for j = 1:dim(2)        
         img_copy(i,j,3) = img_copy(i,j,3)^(1/3);         
     end
end
retinex_img(:,:,3) = retinex_img(:,:,3).*(img_copy(:,:,3));
 
retinex_img(:,:,1) = img_copy(:,:,1);
retinex_img(:,:,2) = img_copy(:,:,2);
retinex_img = hsv2rgb(retinex_img);
function retimg = bialteral(img ,N ,sigma)
%% colorspace transformation
   img = rgb2hsv(img);%convert rgb to hsv colorspace in order to process

%% pre-computer domain filtering
sigma_d = sigma(1);
sigma_r = sigma(2);
[X,Y] = meshgrid(-N:N,-N:N);%generate two matrix 
D = exp(-(X.^2+Y.^2)/(2*sigma_d^2));%domain weights with Euclidean distance

%% create waitbar
h = waitbar(0,\'illumination retinex algorithm……\');
set(h,\'Name\',\'Illumination Retinex\');

%% rang filtering in v layer
dim = size(img);%dim=[height,length,3]
B = zeros(dim);%create an image B with the same size and dimension with the zero value.
for i = 1:dim(1)
    for j = 1:dim(2)
        iMin = max(i-N,1);
        iMax = min(i+N,dim(1));
        jMin = max(j-N,1);
        jMax = min(j+N,dim(2));
        L = img(iMin:iMax,jMin:jMax,3);%extract the local region
        
        d = L-img(i,j,3);%the dissimilarity between the surroud and center
          
        R = exp(-(d.^2)/(2*sigma_r^2));%range filter weights
                
        F = R.*D((iMin:iMax)-i+N+1,(jMin:jMax)-j+N+1);%its row is from iMin-i+N+1 to iMax-i+N+1,and so as line
        for m = 1:iMax-iMin+1
            for n = 1:jMax-jMin+1
                if d(m,n) < 0
                    F(m,n) = 0;
                end
            end
        end
        norm_F = sum(F(:));
        B(i,j,3) = sum(sum(F.*L))/norm_F;

        retimg(i,j,1) = img(i,j,1);
        retimg(i,j,2) = img(i,j,2);
        retimg(i,j,3) = B(i,j,3);
    end
    waitbar(i/dim(1));
end
close(h);%close the bar

三、运行结果

在这里插入图片描述

四、备注

版本:2014a

版权声明:本文为matlabxiao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/matlabxiao/p/15052345.html