Search code examples
matlabhistogramlocal

Getting all dark output image


If I remove the uint8 when I'm giving the matrix B its size, I get an output that is overshadowed by a red plane. I've also added the [] in the imshow(B, []) to resolve the issue of the all dark output, but it doesn't help.

function myThirdAssignment(I,WindowSize,K0,K1,K2)

%if size(I,3)==1
 

 %[rows, columns, numberOfColorChannels] = size(I);
 
%elseif size(I,3)==3
    
x= im2double(imread(I));
%gray1=rgb2gray(x);
%[r, c, numberOfColorChannels] = size(gray1);
%end


if nargin==1 % If number of arguments of the function are equal to 1 then use 
             % the following default values for K0,K1,K2, & Window size.
    K0= 0.5;
    
    K1= 1;
    
    K2= 0.5;
    
    WindowSize=3;
end

figure(1); imshow(x); title('ORIGINAL IMAGE');
imwrite(x,'OriginalImage.bmp.bmp'); %writing data of original image in to current directory.

% GIVING B THE SAME ROWS AND COLUMS AS THE ORIGINAL IMAGE
[rows, columns, numberOfColorChannels] = size(x);
B = zeros(rows, columns, numberOfColorChannels, 'uint8');


% CALCULATING CEIL & FLOOR VALUES TO MAKE PROGRAM MORE GENERAL
p= ceil((WindowSize / 2)); %3/2= 1.5=2


s= floor((WindowSize / 2)); %3/2=1.5=1



        
        
        
        
           
            B(i,j)= 2*x(i,j);
            
        
        
        else
            B(i,j)= x(i,j);
        end
    
        %--------------------------------------------
        
    end


end

%RGB = cat(3, B, B, B);
figure(2);imshow(B, []); title('IMAGE AFTER LOCAL HISTOGRAM EQUALIZATION');
imwrite(B,'enhancedImage.jpeg.jpeg'); %writing data of enhanced image in to current directory

end

I used this image as input.


Solution

  • There are two issues here: a) the uint8 issue which you have solved already which converts double values into uint8 which rounds everything to 0 resulting in a dark image, and b) the resulting red image which occurs because you're only performing your local histogram equalization on the red channel of your input image x and your green and blue channels in your output image are zeros.

    Change your code to this:

    if avg <= K0*(meanIntensity) && (K1*(stdG) <= std) && (std <= K2*(stdG))
               % only enhance an area of defined window size when its mean/average is 
               % lesser than or equal to the mean of the image by some constant
               % K0 AND its standard deviation is lesser than the value of the
               % standard deviation by a constant K2 and greater than the
               % global standard deviation by a constant K1.
               
                B(i,j,1)= 2*x(i,j,1);
                B(i,j,2)= 2*x(i,j,2);
                B(i,j,3)= 2*x(i,j,3);
            
            
            else
                B(i,j,1)= x(i,j,1);
                B(i,j,2)= x(i,j,2);
                B(i,j,3)= x(i,j,3);
            end