retinex图像增强算法

clear all;%clear the values
clc;%clear the command window
strhead = '2005';%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);

%% Gamma-correction
% dim = size(img);
% for i = 1:dim(1)
%      for j = 1:dim(2)
%          for c=1:3
%          img(i,j,c) = img(i,j,c)^(1/3);
%          end
%      end
%  end
%         retinex_img(:,:,1) = retinex_img(:,:,1).*(img(:,:,1));
%         retinex_img(:,:,2) = retinex_img(:,:,2).*(img(:,:,2));
%         retinex_img(:,:,3) = retinex_img(:,:,3).*(img(:,:,3));

subplot(1,3,3);imshow(retinex_img);title('the retinex image');
string=strcat(strhead,'_retinex.bmp');
imwrite(retinex_img,string,'bmp');

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

%% display colorspace transformation
img = hsv2rgb(img);
retimg = hsv2rgb(retimg);

function retimg = bialteral2(img ,N ,sigma)
%% 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,'reflecting retinex algorithm……');
set(h,'Name','Reflecting 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);%extract the local region
       
        d = L-img(i,j);%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

        norm_F = sum(F(:));
        B(i,j) = sum(sum(F.*L))/norm_F;
       
        retimg(i,j) = B(i,j);
    end
    waitbar(i/dim(1));
end
close(h);%close the bar

你可能感兴趣的:(算法,image,distance,transformation,domain,algorithm)