www.gusucode.com > Diabetic retinopathy > color_enhancement_main.m

    
sprintf('Iterative Color Image Enhancement with composite Y,Cb,Cr');
clear
tic
 

 method_set={'AR','MCE','MCEDRC','TW-CES','DRC-CES','SF-CES','TW-CES-BLK','DRC-CES-BLK','SF-CES-BLK'}; 

 [mr mc]=size(method_set);
 
 jpqm=zeros(1,mc);
 cef=zeros(1,mc);
 
 


image='image18';


lamda=1.95;  % used in contrast method     
alpha=0.98;   % used in ALPHA ROOTING

gamma_JM=1.95;  %used Dynamic Range Compression

%for SFunction
pivotx=0.5;
pivoty=0.5;
power1=1.8;
power2=0.8;


% For retinex_theory based implementation
% gamma=1.95; % Also used for Dynamic Range compression
%  thresh1=0.10;
%  thresh2=1.95;
 
 gamma=1.95; 
 thresh1=0.10;
 thresh2=gamma;

betaDC=1.0; % used in DCT scaling using twicing function
sigma_thresh=15;% used for block decomposition 

% betaAC=2.5;
% Qlt=100;
% sf=0;% Sharpening factor
 nIteration=1;
 
 fsave=1; %File save parameter

   % org_I 	              = imread( [sprintf(image) '.bmp'],'bmp');
  org_I	              = imread( [sprintf(image) '.jpg'],'jpg');
  
 [height width depth]=size(org_I);
figure(1); imshow(uint8(org_I)); title('Original Image');
 CI_ycbcr=RGB_YCbCr(org_I);
 Y=CI_ycbcr(:,:,1);
 maxDC=double(max(max(Y)));
 Cb=CI_ycbcr(:,:,2);
 Cr=CI_ycbcr(:,:,3);

 
  
       
       
    dct_Y=blkproc(Y,[8,8],@dct2);
    dct_Cb=blkproc(Cb,[8 8],@dct2);
    dct_Cr=blkproc(Cr,[8 8],@dct2);
     org_dct_CI_ycbcr(:,:,1)=dct_Y;
     org_dct_CI_ycbcr(:,:,2)=dct_Cb;
     org_dct_CI_ycbcr(:,:,3)=dct_Cr;
     
 
  %M is the index for method
% for M=1:mc

M = 4;
 
dct_CI_ycbcr=org_dct_CI_ycbcr;
 
 method=char(method_set(M));
 
 sprintf('Using %s',char(method))   
 ofname=['result/'  sprintf(image) '_' sprintf(method)];

%      sprintf('Output file= %s ',ofname)
      for i=1:nIteration

   gamma_ret=ones(height/8+2,width/8+2);  
   for l=1:height/8+2
    for m=1:width/8+2
        gamma_ret(l+1,m+1)=gamma;
    end
   end
          
    for l=0:height/8-1
    for m=0:width/8-1
        
   switch method
       case 'TW-CES'
       Z=enhance_composite_dc_sigma(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),betaDC,maxDC,1);
        case 'TW-CES-BLK'
       Z=enhance_composite_dc_blk(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),betaDC,maxDC,1,sigma_thresh);
        case 'SF-CES'
       Z=enhance_composite_SFunction_sigma(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),maxDC,pivotx,pivoty,power1,power2,1);
         case 'SF-CES-BLK'
       Z=enhance_composite_SFunction_blk(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),maxDC,pivotx,pivoty,power1,power2,1,sigma_thresh);
     case 'AR'
      Z=enhance_composite_alpha_rooting(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),alpha,maxDC);
      case  'MCE'
      Z=enhance_composite_contrast_method(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),lamda);
       case 'DRC-CES'
      Z=enhance_composite_dyn_sigma(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),gamma_JM,maxDC,1);
        case 'DRC-CES-BLK'
      Z=enhance_composite_dyn_blk(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),gamma_JM,maxDC,1,sigma_thresh);
       case 'MCEDRC'
               g1=gamma_ret(l+1,m+1);
               g2=gamma_ret(l+1,m+2);
               g3=gamma_ret(l+1,m+3); 
               g4=gamma_ret(l+2,m+1);
              [Z gamma_ret(l+2,m+2)]=enhance_composite_retinex_theory_smoothing(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),maxDC,gamma,thresh1,thresh2,g1,g2,g3,g4);
       otherwise
           method='TW-CES';
       Z=enhance_composite_dc_sigma(dct_CI_ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:),betaDC,maxDC,1);
   end;
   
           
       enh_dct_Ycbcr(l*8+1:l*8+8,m*8+1:m*8+8,:)=Z;
    end
    end
    
    dct_CI_ycbcr=enh_dct_Ycbcr;
      end  
    
    enh_Y=blkproc(enh_dct_Ycbcr(:,:,1),[8,8],@idct2);
    enh_Cb=blkproc(enh_dct_Ycbcr(:,:,2),[8,8],@idct2);
    enh_Cr=blkproc(enh_dct_Ycbcr(:,:,3),[8,8],@idct2);   
     Y=enh_Y;
     Cb=enh_Cb;
     Cr=enh_Cr;
   
    
   % sprintf('JPEG Quality index=%f',jpeg_quality_score(uint8(Y)))
     jpqm(M)=jpeg_quality_score(uint8(Y));
    
    
    
    [h w]=size(enh_Y);
    %sprintf('Size of enhanced Image: %d x %d ',h,w)
    [h w]=size(enh_Cb);
    %sprintf('Size of Cb: %d x %d ',h,w)
    [h w]=size(enh_Cr);
   % sprintf('Size of Cr: %d x %d ',h,w)
    
    enh_CI=zeros(h,w,3);
    enh_CI(:,:,1)=enh_Y(1:h,1:w);
    enh_CI(:,:,2)=enh_Cb(1:h,1:w);
    enh_CI(:,:,3)=enh_Cr(1:h,1:w);
    out=YCbCr_RGB(enh_CI);
    figure(M+2); imshow(uint8(out)); title(sprintf('%s',method));
%     sprintf('Colourfulness Metric=%f',colourfulness_metric(uint8(out)))
   cef(M)=colourfulness_metric(uint8(out))/colourfulness_metric(uint8(org_I));
   
   
%   saving enhanced images
  if(fsave==1)
   imwrite(uint8(out),[ofname,'.BMP'],'BMP');
    sprintf('Image saved in %s ', ofname)
  end
 %end % Method
 

   %Saving results in a file
 
   if(fsave==1)
   ofrname=['result/' sprintf(image) '_measures'];
  ofid=fopen(ofrname,'w');
   
   fprintf(ofid,'METHOD        JPQM     CEF   \n');
   sprintf('METHOD        JPQM     CEF   \n')
  for M=1:mc
      fprintf(ofid,'%s         %4.2f   %4.2f   \n',char(method_set(M)),jpqm(M),cef(M));
      sprintf('%s         %4.2f   %4.2f    \n',char(method_set(M)),jpqm(M),cef(M))
  end
  st=fclose(ofid);
  sprintf('Results saved in %s',ofrname)
   
   else
       for M=1:mc
           sprintf('%s         %4.2f   %4.2f    \n',char(method_set(M)),jpqm(M),cef(M))
       end
   end %if fsave

 
   toc