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

MATLAB code for Linear filtering without using imfilter function

  Linear Filter :          
      Linear filtering technique is used for reducing random noise, sharpening the edges and correcting unequal illuminations.           
                                                                          
  The procedure is carried out by filtering the image by correlation with an appropriate filter kernel.  The value of output pixel is calculated as a weighted sum of neighboring pixels.
                                                                                                                                             




MATLAB CODE:


 A=imread('eight.tif');
 figure,imshow(A);
 title('Original Image');

corr=[0 0.5 0.5;-1 0.5 0.2; 0.4 0.2 0;];
%corr=[0.5
   %    0.4
   %    0.1];
      
%corr=ones(5,5)/25;
      
%To pad the input image with zeros based on the kernel size.
Array padding can also be done using matlab built_in function padarray.


Example:


Let M=[4 5 6; 1 1 4; 7 8 8;];


M= [ 4     5     6
     1     1     4
     7     8     8]
                                                            
                                                 
M1=padarray(M,[1 1])
%Pad with zeros on all sides
M1 =


     0     0     0     0     0
     0     4     5     6     0
     0     1     1     4     0
     0     7     8     8     0
     0     0     0     0     0
                                                             
                                                                   
                                                                    
  

                                                               

>> M2=padarray(M,[2 2])
%pad with two rows and columns of zeros on all sides              
                                                         


M2 =


     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     4     5     6     0     0
     0     0     1     1     4     0     0
     0     0     7     8     8     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0


>> M3=padarray(M,[1 2])
%Pad 1 row and 2 columns with zeros on all sides
M3 =


     0     0     0     0     0     0     0
     0     0     4     5     6     0     0
     0     0     1     1     4     0     0
     0     0     7     8     8     0     0
     0     0     0     0     0     0     0

   
pad1=size(corr,1)-1;
pad2=size(corr,2)-1;
output=uint8(zeros(size(A)));
if(size(corr,1)==1)
   
 B=zeros(size(A,1),size(A,2)+pad2);
 m=0;
 n=floor(size(corr,2)/2);
 sz1=size(B,1);
 sz2=size(B,2)-pad2;
elseif(size(corr,2)==1)
    B=zeros(size(A,1)+pad1,size(A,2));
    m=floor(size(corr,1)/2);
    n=0;
    sz1=size(B,1)-pad1;
   sz2=size(B,2);
else
    B=zeros(size(A,1)+pad1,size(A,2)+pad2);
    m=floor(size(corr,1)/2);
    n=floor(size(corr,2)/2);
   
    sz1=size(B,1)-pad1;
 sz2=size(B,2)-pad2;
end
 for i=1:size(A,1)
     for j=1:size(A,2)
         B(i+m,j+n)=A(i,j);
     end
 end
 szcorr1=size(corr,1);
 szcorr2=size(corr,2);
for i=1:sz1
    for j=1:sz2
        sum=0;
        m=i;
        n=j;
        for x=1:szcorr1
          for y=1:szcorr2
       %The weighted sum of the neighborhood pixels is calculated.
               sum=sum+(B(m,n)*corr(x,y));
               n =n+1;                    
           end
             n=j;
            m=m+1;
       end
        output(i,j)= sum;
    end
end
    figure,imshow(output);
    title('After linear filtering');



%For the correlation kernel ones(5,5)/25;




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

YIQ Image to RGB Image

MATLAB code:
%YIQ to RGB

%R=Y+0.956*I+0.621*Q
%G=Y-0.272*I-0.647*Q
%B=Y-1.106*I+1.703*Q
RGB=uint8(zeros(size(YIQ)));
for i=1:size(YIQ,1)
    for j=1:size(YIQ,2)
          RGB(i,j,1)=YIQ(i,j,1)+0.956*YIQ(i,j,2)+0.621*YIQ(i,j,3);
          RGB(i,j,2)=YIQ(i,j,1)-0.272*YIQ(i,j,2)-0.647*YIQ(i,j,3);
          RGB(i,j,3)=YIQ(i,j,1)-1.106*YIQ(i,j,2)+1.703*YIQ(i,j,3);
    end
end

figure,imshow(RGB);
title('RGB Image');

like button Like "IMAGE PROCESSING" page

RGB Image to YIQ Image

YIQ  builds the basis for the NTSC (National Television System Commitee) format.
The Component division of YIQ :
Y=0.30R+0.59G+0.11B
I=0.60R-0.28G-0.32B
Q=0.21R-0.52G+0.31B


MATLAB code:
Im=imread('peppers.png');


figure,imshow(Im);
title('Original Image')

%y=0.2989 * R + 0.5870 * G + 0.1140 * B 

%I=0.60*R - 0.28*G-0.32*B
%Q=0.21*R -0.52*G+0.31*B
YIQ=uint8(zeros(size(Im)));
for i=1:size(Im,1)
    for j=1:size(Im,2)
        YIQ(i,j,1)=0.2989*Im(i,j,1)+0.5870*Im(i,j,2)+0.1140*Im(i,j,3);
        YIQ(i,j,2)=0.596*Im(i,j,1)-0.274*Im(i,j,2)-0.322*Im(i,j,3);
        YIQ(i,j,3)=0.211*Im(i,j,1)-0.523*Im(i,j,2)+0.312*Im(i,j,3);
    end
end
figure,imshow(YIQ);
title('YIQ Image');

like button Like "IMAGE PROCESSING" page

RGB Image to Grayscale Image without using rgb2gray function

A gray-scale image is composed of different shades of grey color.
A true color image can be converted to a gray scale image by preserving the luminance(brightness) of the image.

Here the RGB image is a combination of RED, BLUE AND GREEN colors.
The RGB image is 3 dimensional. In an image ,
at a particular position say  ( i,j)
Image(i,j,1) gives the value of RED pixel.
Image(i,j,2) gives the value of BLUE pixel.
Image(i,j,3) gives the value of GREEN pixel.
The combination of these primary colors are normalized with R+G+B=1;
This gives the neutral white color.

The grayscale image is obtained from the RGB image by combining 30% of RED , 60% of GREEN and 11% of BLUE.
This gives the brightness information of the image. The resulting image will be two dimensional. The value 0 represents black and the value 255 represents white. The range will be between black and white values.


MATLAB CODE:

Im=imread('peppers.png');

figure,imshow(Im);
title('Original Image');

%0.2989 * R + 0.5870 * G + 0.1140 * B
GIm=uint8(zeros(size(Im,1),size(Im,2)));
for i=1:size(Im,1)
      for j=1:size(Im,2)
          GIm(i,j)=0.2989*Im(i,j,1)+0.5870*Im(i,j,2)+0.1140*Im(i,j,3);
      end
end

%Without using for loop:
%GIm=0.2989*Im(:,:,1)+0.5870*Im(:,:,2)+0.1140*Im(:,:,3);


figure,imshow(GIm);
title('Grayscale Image');











Check out : Partially colored gray scale image - MATLAB CODE
like button Like "IMAGE PROCESSING" page

Simple Image Thresholding using GUI component Slider

Thresholding is one of the steps performed on the image during image conversion. 
Also Learn: Otsu's thresholding method
It is used for separating the background from the foreground.
The user can drag the slider to adjust the Thresholding. The slider value ranges between 0 and 255.

MATLAB CODE:


function f=mainthresh()
%The window size of the image is adjusted based on the screen size.


global T1;
ssz = get(0,'ScreenSize');
figure('Visible','on','Name','IMAGE THRESHOLDING','NumberTitle','off','Position',ssz);

axes('units','pixels','Position',[ssz(3)/45 ssz(4)/4 ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/3)]);

in=imread('coins.png');
imshow(in);
set(gca,'xtick',[],'ytick',[])

%The Slider is one of the GUI component in MATLAB. The Value of the slider is between 0 and 255.
%These values can be changed based on the image type.


slid=uicontrol('Style','Slider','Visible','on','Value',1,'Max',255,'Min',0,'Sliderstep',[1 1],'Position',[ssz(3)/35 ssz(4)/5 ssz(3)-(ssz(3)/3) 20],'Callback', @threshing_image);
ent=uicontrol('Style','pushbutton','Visible','on','String','THRESHOLD VALUE','Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/8) 105 30]);
ed=uicontrol('Style','edit','Visible','on','String','0','Value',1,'Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/6) 90 20]);


%When the slider is moved the corresponding position value is passed to the function 'threshing_image' .

%The original image is passed to another function 'Imthreshold'.



      function threshing_image(object,~)
        val=get(object,'value');
        in=imread('coins.png');
        T1=Imthreshold(in,val);
        imshow(T1);
        set(gca,'xtick',[],'ytick',[])
       
        set(ed,'String',val);
      end

    

%Based on the value passed, the image background is separated from the foreground.

%In this example, the background of the coins image contains pixel values less than 80.
%By adjusting the slider to value 80 the background area gets value zero and the coins appear as it is.




function Im=Imthreshold(Image,Tvalue)

%Replace the pixel value either with 0 or 255.


Image( find ( Image(:,:,1) < Tvalue ) )=255;

Im=Image;
end
f=T1;
end















Thresholded Image



Here is another example , This Image is RGB. So we have to consider the RED, GREEN and BLUE pixel values.



like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com