Follow by Email

Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

COLOR HISTOGRAM EQUALIZATION - MATLAB CODE


                          Histogram Equalization can be considered as redistribution of the intensity of the image. Color histogram equalization can be achieved by converting a color image into HSV/HSI image and enhancing the Intensity while preserving hue and saturation components. 

                          However, performing histogram equalization on components of R,G and B independently will not  enhance the image. At the end of this post, check the histogram of before and after histogram equalization of an image which is obtained by performing histogram equalization on the components(R,G and B) independently.

Steps to be performed:
2.      Obtain the ‘Intensity Matrix’ from the HSI Image matrix
3.      Perform Histogram Equalization on the intensity Matrix
http://angeljohnsy.blogspot.com/2011/04/matlab-code-histogram-equalization.html
4.      Update the Intensity Matrix from the HSI Image matrix with the histogram equalized Intensity matrix
        
MATLAB CODE:
%COLOR HISTOGRAM EQUALIZATION

%READ THE INPUT IMAGE
I = imread('football.jpg');

%CONVERT THE RGB IMAGE INTO HSV IMAGE FORMAT
HSV = rgb2hsv(I);


%PERFORM HISTOGRAM EQUALIZATION ON INTENSITY COMPONENT
Heq = histeq(HSV(:,:,3));

HSV_mod = HSV;
HSV_mod(:,:,3) = Heq;

RGB = hsv2rgb(HSV_mod);

figure,subplot(1,2,1),imshow(I);title('Before Histogram Equalization');

       subplot(1,2,2),imshow(RGB);title('After Histogram Equalization');


EXPLANATION:

RGB image matrix is converted into HSI(Hue ,Saturation and Intensity) format and histogram equalization is applied only on the Intensity matrix . The Hue and Saturation matrix remains the same. The updated HSI image matrix is converted back to RGB image matrix.


%DISPLAY THE HISTOGRAM OF THE ORIGINAL AND THE EQUALIZED IMAGE

HIST_IN = zeros([256 3]);
HIST_OUT = zeros([256 3]);


%http://angeljohnsy.blogspot.com/2011/06/histogram-of-image.html
%HISTOGRAM OF THE RED,GREEN AND BLUE COMPONENTS

HIST_IN(:,1) = imhist(I(:,:,1),256); %RED
HIST_IN(:,2) = imhist(I(:,:,2),256); %GREEN
HIST_IN(:,3) = imhist(I(:,:,3),256); %BLUE

HIST_OUT(:,1) = imhist(RGB(:,:,1),256); %RED
HIST_OUT(:,2) = imhist(RGB(:,:,2),256); %GREEN
HIST_OUT(:,3) = imhist(RGB(:,:,3),256); %BLUE

mymap=[1 0 0; 0.2 1 0; 0 0.2 1];

figure,subplot(1,2,1),bar(HIST_IN);colormap(mymap);legend('RED CHANNEL','GREEN CHANNEL','BLUE CHANNEL');title('Before Applying Histogram Equalization');
       subplot(1,2,2),bar(HIST_OUT);colormap(mymap);legend('RED CHANNEL','GREEN CHANNEL','BLUE CHANNEL');title('After Applying Histogram Equalization');

EXPLANATION:
Obtain the histogram of each component (Red,Green and Blue) independently.
Define the colormap ‘mymap’ with three colors namely Red, Green and Blue.
Display the histograms of the components before and after histogram equalization.

 NOTE:
Histogram of the above image by processing the components independently gives bad result.
 
Histogram Equalization applied on individual components
like button Like "IMAGE PROCESSING" page

MATLAB CODE:Local Histogram equalization


For every pixel, based on the neighbor hood value the histogram equalization is done. Here I used 3 by 3 window matrix for explanation. By changing the window matrix size, the histogram equalization can be enhanced. By changing the values of M and N the window size can be changed in the code given below.

Steps to be performed:





MATLAB CODE:


A=imread('tire.tif');
figure,imshow(A);
Img=A;
     
  
%WINDOW SIZE
M=10;
N=20;


mid_val=round((M*N)/2);

%FIND THE NUMBER OF ROWS AND COLUMNS TO BE PADDED WITH ZERO
in=0;
for i=1:M
    for j=1:N
        in=in+1;
        if(in==mid_val)
            PadM=i-1;
            PadN=j-1;
            break;
        end
    end
end
%PADDING THE IMAGE WITH ZERO ON ALL SIDES
B=padarray(A,[PadM,PadN]);

for i= 1:size(B,1)-((PadM*2)+1)
    
    for j=1:size(B,2)-((PadN*2)+1)
        cdf=zeros(256,1);
        inc=1;
        for x=1:M
            for y=1:N
  %FIND THE MIDDLE ELEMENT IN THE WINDOW          
                if(inc==mid_val)
                    ele=B(i+x-1,j+y-1)+1;
                end
                    pos=B(i+x-1,j+y-1)+1;
                    cdf(pos)=cdf(pos)+1;
                   inc=inc+1;
            end
        end
                      
        %COMPUTE THE CDF FOR THE VALUES IN THE WINDOW
        for l=2:256
            cdf(l)=cdf(l)+cdf(l-1);
        end
            Img(i,j)=round(cdf(ele)/(M*N)*255);
     end
end
figure,imshow(Img);
figure,
subplot(2,1,1);title('Before Local Histogram Equalization'); imhist(A);
subplot(2,1,2);title('After Local Histogram Equalization'); imhist(Img);



















After Local Histogram Equalization

Histogram equalization of an Image:
 http://angeljohnsy.blogspot.com/2011/04/matlab-code-histogram-equalization.html

like button Like "IMAGE PROCESSING" page

Matlab code: Histogram equalization without using histeq function


              It is the re-distribution of gray level values uniformly. Let’s consider a 2 dimensional image which has values ranging between 0 and 255.




MATLAB CODE:

GIm=imread('tire.tif');
numofpixels=size(GIm,1)*size(GIm,2);
figure,imshow(GIm);
title('Original Image');


HIm=uint8(zeros(size(GIm,1),size(GIm,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
%freq counts the occurrence of each pixel value.
%The probability of each occurrence is calculated by probf.
for i=1:size(GIm,1)
    for j=1:size(GIm,2)
        value=GIm(i,j);
        freq(value+1)=freq(value+1)+1;
        probf(value+1)=freq(value+1)/numofpixels;
    end
end
sum=0;
no_bins=255;
%The cumulative distribution probability is calculated. 
for i=1:size(probf)
   sum=sum+freq(i);
   cum(i)=sum;
   probc(i)=cum(i)/numofpixels;
   output(i)=round(probc(i)*no_bins);
end
for i=1:size(GIm,1)
    for j=1:size(GIm,2)
            HIm(i,j)=output(GIm(i,j)+1);
    end
end
figure,imshow(HIm);
title('Histogram equalization');



             




%The result is shown in the form of a table
figure('Position',get(0,'screensize'));
dat=cell(256,6);
for i=1:256
dat(i,:)={i,freq(i),probf(i),cum(i),probc(i),output(i)};   
end
   columnname =   {'Bin''Histogram''Probability''Cumulative histogram','CDF','Output'};
columnformat = {'numeric''numeric''numeric''numeric''numeric','numeric'};
columneditable =  [false false false false false false];
t = uitable('Units','normalized','Position',...
            [0.1 0.1 0.4 0.9], 'Data', dat,...
            'ColumnName', columnname,...
            'ColumnFormat', columnformat,...
            'ColumnEditable', columneditable,...
            'RowName',[]); 
    subplot(2,2,2); bar(GIm);
    title('Before Histogram equalization');
    subplot(2,2,4); bar(HIm);
    title('After Histogram equalization');




                              

Here is a simple Version of Histogram Equalization MATLAB CODE:

%Read a grayscale Image or a matrix mxn
A=imread('tire.tif');
figure,imshow(A);
%Specify the bin range[0 255]
bin=255;
%Find the histogram of the image.
Val=reshape(A,[],1);
Val=double(Val);
I=hist(Val,0:bin);
%Divide the result by number of pixels
Output=I/numel(A);
%Calculate the Cumlative sum
CSum=cumsum(Output);
%Perform the transformation S=T(R) where S and R in the range [ 0 1]
HIm=CSum(A+1);
%Convert the image into uint8
HIm=uint8(HIm*bin);
figure,imshow(HIm);



                          
                                 
like button Like "IMAGE PROCESSING" page
Next Post Home

Link

Related Posts Plugin for WordPress, Blogger...
Google ping Hypersmash.com