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 PROGRAM : 2D MEDIAN FILTERING FOR SALT AND PEPPER NOISE WITHOUT USING medfilt2 FUNCTION

MEDIAN FILTER:

In digital Image processing, removing the noise is one of the preprocessing techniques.
The image noise may be termed as random variation of brightness or color information.
There are various types of image noise. Here a matlab program to remove 'salt and pepper noise' using median filtering is given.
 The random occurrence of black and white pixels is ‘salt and pepper noise’.

The procedural steps for 2D median filtering:


Learn how to pad with zeros using MATLAB built_in function padarray.

FLOW CHART:


MATLAB CODE:

%READ AN 2D IMAGE
A=imread('zebra.jpg');
title('IMAGE WITH SALT AND PEPPER NOISE');
figure,imshow(A);

%PAD THE MATRIX WITH ZEROS ON ALL SIDES
modifyA=zeros(size(A)+2);
B=zeros(size(A));

%COPY THE ORIGINAL IMAGE MATRIX TO THE PADDED MATRIX
        for x=1:size(A,1)
            for y=1:size(A,2)
                modifyA(x+1,y+1)=A(x,y);
            end
        end
      %LET THE WINDOW BE AN ARRAY
      %STORE THE 3-by-3 NEIGHBOUR VALUES IN THE ARRAY
      %SORT AND FIND THE MIDDLE ELEMENT
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        window=zeros(9,1);
        inc=1;
        for x=1:3
            for y=1:3
                window(inc)=modifyA(i+x-1,j+y-1);
                inc=inc+1;
            end
        end
       
        med=sort(window);
        %PLACE THE MEDIAN ELEMENT IN THE OUTPUT MATRIX
        B(i,j)=med(5);
       
    end
end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
title('IMAGE AFTER MEDIAN FILTERING');
figure,imshow(B);












Learn how to add 'salt and pepper noise to an image'.Median filtering preserves the image without getting blurred. Median filtering is done on an image matrix by   finding the median of the neighborhood pixels by using a window that slides pixel by pixel.


  


    ALSO MEDIAN FILTER FOR RGB IMAGE
like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com