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

Min Filter - MATLAB CODE

MIN FILTER
  •      To find the darkest points in an image.
  •       Finds the minimum value in the area encompassed by the filter.
  •       Reduces the salt noise as a result of the min operation.
  •       The 0th percentile filter is min filter.

MATLAB CODE:

%READ AN IMAGE
A = imread('board.tif');
A = rgb2gray(A(1:300,1:300,:));
figure,imshow(A),title('ORIGINAL IMAGE');

%PREALLOCATE THE OUTPUT MATRIX
B=zeros(size(A));

%PAD THE MATRIX A WITH ZEROS
modifyA=padarray(A,[1 1]);

        x=[1:3]';
        y=[1:3]';
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
      
       %VECTORIZED METHOD 
       window=reshape(modifyA(i+x-1,j+y-1),[],1);

       %FIND THE MINIMUM VALUE IN THE SELECTED WINDOW
        B(i,j)=min(window);

    end
end

%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MIN FILTERING');












like button Like "IMAGE PROCESSING" page

Max Filter - MATLAB CODE

  • To find the brightest points in an image.
  •  Finds the maximum value in the area encompassed by the filter.
  •  Reduces the pepper noise as a result of the max operation.
  •  The 100th percentile filter is max filter. Check the 50th percentile filter i.e the median filter.



MATLAB CODE:

%READ AN IMAGE
A = imread('board.tif');
A = rgb2gray(A(1:300,1:300,:));
figure,imshow(A),title('ORIGINAL IMAGE');

%PREALLOCATE THE OUTPUT MATRIX
B=zeros(size(A));

%PAD THE MATRIX A WITH ZEROS
modifyA=padarray(A,[1 1]);

        x=[1:3]';
        y=[1:3]';
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
      
       %VECTORIZED METHOD
       window=reshape(modifyA(i+x-1,j+y-1),[],1);

       %FIND THE MAXIMUM VALUE IN THE SELECTED WINDOW
        
       B(i,j)=max(window);
   
    end
end

%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MAX FILTERING');














like button Like "IMAGE PROCESSING" page

Adaptive filtering-local noise filter


Adaptive filter is performed on the degraded image that contains original image and noise. The mean and variance are the two statistical measures that a local adaptive filter depends with a defined mxn window region. 






























A = imread('saturn.png');
B = rgb2gray(A);
sz = size(B,1)*size(B,2);


%Add gaussian noise with mean 0 and variance 0.005
B = imnoise(B,'gaussian',0,0.005);
figure,imshow(B); title('Image with gaussian noise');













B = double(B);

%Define the window size mxn
M = 5;
N = 5;

%Pad the matrix with zeros on all sides
C = padarray(B,[floor(M/2),floor(N/2)]);


lvar = zeros([size(B,1) size(B,2)]);
lmean = zeros([size(B,1) size(B,2)]);
temp = zeros([size(B,1) size(B,2)]);
NewImg = zeros([size(B,1) size(B,2)]);

for i = 1:size(C,1)-(M-1)
    for j = 1:size(C,2)-(N-1)
        
        
        temp = C(i:i+(M-1),j:j+(N-1));
        tmp =  temp(:);
             %Find the local mean and local variance for the local region        
        lmean(i,j) = mean(tmp);
        lvar(i,j) = mean(tmp.^2)-mean(tmp).^2;
        
    end
end

%Noise variance and average of the local variance
nvar = sum(lvar(:))/sz;

%If noise_variance > local_variance then local_variance=noise_variance
 lvar = max(lvar,nvar);     

 %Final_Image = B- (noise variance/local variance)*(B-local_mean);
 NewImg = nvar./lvar;
 NewImg = NewImg.*(B-lmean);
 NewImg = B-NewImg;

 %Convert the image to uint8 format.
 NewImg = uint8(NewImg);
figure,imshow(NewImg);title('Restored Image using Adaptive Local filter');



like button Like "IMAGE PROCESSING" page

Add salt and pepper noise to image

How to add salt and pepper noise to an image


          To obtain an image with ‘speckle’ or ‘salt and pepper’ noise we need to add white and black pixels randomly in the image matrix.



First convert the RGB image into grayscale image.
Then generate random values for the size of the matrix.
Here I used MATLAB function ‘randint’.





This function will generate random values for the given matrix size within the specified range.
For instance, consider an image matrix of size 4X3


Imgmatrix =
   237   107   166
   234    95   162
   239   116   169
   56   126    89


Generate random values for a 4X3 matrix with range 0 to 10.
randint(4,3,[0,10])

ans =
     4    10     4
     7     8     4
    10     0     5
     3    10     9

    

Now we can replace with pixel value zero (black) in the image matrix if there is ‘0’ value in the random matrix.

Now the image matrix will have black pixels.

Imgmatrix =
   237   107   166
   234    95   162
   239   0   169
   56   126    89


 
Similarly, replace the image matrix pixel value with ‘255’ if there is value ‘10’ in the random matrix.
The white pixels are now added.

Imgmatrix =
   237   255   166
   234    95   162
   255   0   169
   56   126    89

Black=0 white=255

 

MATLAB CODE:


A=imread('taj.jpg');
B=rgb2gray(A);


black=3;
white=253;
%Adjust the values in 'black' and 'white' to increase the noise.

NoiseImg = B;
    Rmatrix = randint(size(B,1),size(B,2),[0,255]);
    NoiseImg(Rmatrix <= black) = 0;
    NoiseImg(Rmatrix >=white) = 255;
    RImg=medfilt2(NoiseImg);
    figure,subplot(1,2,1),imshow(NoiseImg),title('Add ''Salt and Pepper'' Noise');
    subplot(1,2,2),imshow(RImg),title('After Noise Removal');






 I used the MATLAB function 'medfilt2' to remove noise. 


like button Like "IMAGE PROCESSING" page

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
Previous Post Next Post Home
Google ping Hypersmash.com