Digital Image Processing Mini-Project: Project Title: Image Denoising and Feature Extraction Using Spatial Filters
Digital Image Processing Mini-Project: Project Title: Image Denoising and Feature Extraction Using Spatial Filters
PROJECT TITLE: IMAGE DENOISING AND FEATURE EXTRACTION USING SPATIAL FILTERS
ACKNOWLDGEMENT:
I would like to thank Prof. Thanikaiselvan Sir for constantly guiding me through the course of this project. His class lectures made my concepts clear in image processing and made me familiar with the various image processing techniques and the various operations that can be performed on images. This helped me implement my desired task using MATLAB and so I was able to complete my mini-project on Image denoising and feature extraction using spatial filters successfully.
ABSTRACT:
In image processing, the quality of any image gets badly corrupted by noise whether it be any kind of noise. To combat this problem of noise, we need to improve the overall system quality. We can implement various image denoising techniques to reduce the noise. We are doing image denoising in spatial domain here. In spatial domain, operations are performed on the pixels itself. We have implemented three different kinds of filter: median filter, averaging filter and wiener filter to denoise the image and then have analyzed the results. Also we have implemented 2-D Gaborfilter for feature extractions of an image.
AIM:
To remove various types of noise and also do feature extraction using different types of spatial filters.
OBJECTIVE:
To perform image denoising using three spatial filters- Median filter, averaging filter and Wiener filter. To extract particular features of an image using another kind of spatial filter- Gabor filter and so study the different applications of spatial filters
THEORY:
Image denoising is the first preprocessing step in dealing with image processing where the overall system quality needs to be improved. Generally, the quality of an image gets corrupted by a lot of noise due to the undesired conditions of image acquisition phase or during the transmission. The great challenge of image denoising is how to preserve the edges and all fine details of an image when reducing the noise. Noise occurs during image capture, transmission or processing phases. The noise contaminated in any image could be dependent on or independent of image content. Whatever the classification of the noise as image dependent or independent, the removal of the undesired pattern stay as a great challenge in several vital applications. The noise is characterized by its pattern and by its probabilistic characteristics. There is a wide variety of noise types: Gaussian noise, speckle noise, poisson noise, impulse noise, salt and pepper noise. The denoising filters could be applied as a local window in the neighborhood region or holistic to the whole image. The robustness of image improvement techniques could be achieved by suppressing the noise while preserving the characterizing features of the image. We will be using spatial filters here for removing different kinds of noises. In spatial filtering, filtering operations are performed directly on the pixels of an image. We will be using two types of spatial filters here for removing two different types of noises: 1. MEDIAN filter and AVERAGING filter for removing salt and pepper noise 2. WIENER filter for removing Gaussian noise. Salt and pepper noise is a form of noise which is typically seen on image and it represents itself as randomly occurring white and black pixels. Salt and pepper noise affects the images in situations where quick transients, such as faulty switching, take place. Gaussian noise is statistical noise that has its probability density function equal to that of the normal distribution. When an electrical variation obeys a Gaussian distribution, such as in the case of thermal motion, it is called Gaussian noise, or random noise. It occurs due to signal's amplitude fluctuating randomly.
In the second subsection, a different application of spatial filtering is shown. Here feature extraction is done using GABOR filter. A 2D Gabor filter is a Gaussian kernel function modulated by a sinusoidal plane wave. The Gabor filters are self-similar: all filters can be generated from one mother wavelet by dilation and rotation. Its impulse response is defined by a harmonic function multiplied by a Gaussian function. Because of the multiplication-convolution property, the convolution of the Fourier transform of the harmonic function and the Fourier transform of the Gaussian function gives the Fourier transform of a Gabor filter's impulse response The filter has a real and an imaginary component representing orthogonal directions. A set of Gabor filters with different frequencies and orientations are helpful for extracting useful features from an image.
ALGORITHM:
1. We consider the input image matrix and pad it with zeroes on all the sides.
2. Then we consider a window of size 3*3. The window can be of any size. Starting from matrix A(1,1) ,we place the window.
5. After sorting, the output matrix is placed with a value of 0 at (2,2) pixel position. The value of the output pixel is found using the median of the neighborhood pixels. 6. This procedure is repeated for all values in the I/P matrix by sliding window to next position. The resultant output matrix is:
MATLAB CODE
%READ AN 2D IMAGE A1=imread('zebra.jpg'); A=rgb2gray(A1); figure;imshow(A);title('IMAGE WITH SALT AND PEPPER NOISE');
%PADDING THE MATRIX WITH ZEROS ON ALL SIDES AND CREATING NEW MATRIX newA=zeros(size(A)+2); B=zeros(size(A)); for x=1:size(A,1) for y=1:size(A,2) newA(x+1,y+1)=A(x,y); end end %CREATING A WINDOW ARRAY OF SIZE 3*3 AND THEN SORTING THE ELEMENTS IN THA ARRAY %CALCULATING MEDIAN AND REPLACING EACH PIXEL WITH COMPUTED MEDIAN VALUE
for i= 2:size(newA,1)-1 for j=2:size(newA,2)-1 array=[newA(i,j),newA(i-1,j),newA(i+1,j),newA(i,j-1),newA(i,j+1),newA(i-1,j1),newA(i-1,j+1),newA(i+1,j-1),newA(i+1,j+1)]; array2=sort(array); med=array2(5); B(i-1,j-1)=med; end end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE B=uint8(B); figure,imshow(B);title('IMAGE AFTER MEDIAN FILTERING');
OUTPUT:
When we use averaging filter, the output is simply the average of pixels contained in the neighborhood of the filter mask. By replacing the value of every pixel in an image by the average of gray levels in neighborhood defined by the mask, we get reduced transitions in gray levels which consequently reduce the noise as noise is generally sharp transition in gray level.
ALGORITHM:
1. Load the image and convert it to gray levels. 2. Now use fspecial. h=fspecial (average) creates a two-dimensional filter h of the average type. It returns h as a correlation kernel. 3. Create another 2-D filter OF SIZE 5*5. 4. Now use filter2 function. It performs rotation by 180 degrees on our filter matrix to create convolutional kernel. Then it calls conv2, 2-D convolution function to do filtering operation. 5. Then we observe the output using imshow
MATLAB CODE
I=imread('zebra.jpg'); I=rgb2gray(I); [m n]=size(I); I=im2double(I); figure; imshow(I); title('original'); X=fspecial('average'); Y=fspecial('average',[5,5]); K=filter2(X,I); L=filter2(Y,I); figure;imshow(K) figure; imshow(L); title('new')
original
new
new
newA=zeros(size(A)+2); B=zeros(size(A)); for x=1:size(A,1) for y=1:size(A,2) newA(x+1,y+1)=A(x,y); end end %STORING THE 3*3 NEIGHBOURING PIXEL VALUES IN WINDOW ARRAY %CENTER OF WINDOW TRAVERSING EVERY PIXEL OF INPUT IMAGE %AVERAGING THE VALUES OF ALL THE NEIGHBOURING PIXELS AND SO CONSTRUCTING THE NEW OUTPUT MATRIX. for i= 1:size(newA,1)-2 for j=1:size(newA,2)-2 window=zeros(3); increment=1; sum=0; for x=1:3 for y=1:3 window(increment)=newA(i+x-1,j+y-1); sum=sum+window(increment); increment=increment+1; end end avg=sum/9; B(i,j)=avg; end end %CONVERT OUTPUT MATRIX TO 0-255 RANGE B=uint8(B); figure,imshow(B);title('IMAGE AFTER MEAN FILTERING');
where
is the original signal. is the noise. is the estimated signal. is the Wiener filter's impulse response.
ALGORITHM:
1. Load the image file and then convert it to gray scale using rgb2gray command. 2. Then introduce Gaussian noise into the image having desired mean and variance. 3. Then employ a Wiener filter to remove out the Gaussian noise. (We are using inbuilt matlab wiener function here).
MATLAB CODE
I=imread('cameraman.tif') figure;imshow(I) title('original image') L=imnoise(I,'gaussian',0,0.005); figure;imshow(L); K=wiener2(L,[3,3]); figure;imshow(K); title('image with noise removed')
original image
Real
Imaginary
where
and
Lambda represents the wavelength of the sinusoidal factor. Theta represents the orientation of the normal to the parallel stripes of a Gabor function. Psi is the phase offset. Sigma is the sigma of the Gaussian envelope Gamma is the spatial aspect ratio, and specifies the ellipticity of the support of the Gabor function
ALGORITHM:
1. 2. 3. 4. 5. 6. Read the image and convert it to grayscale using rgb2gray command. Take the size of the input image to be m*n. Enter some value of sigma, psi,gamma. Then enter the number of lambda values we want to take and then enter those values. Then enter the number of theta values we want to take and then enter those values. Then using the different values of lambda and theta, we get the corresponding gabor filter function values.(For doing this, we would need to implement gabor filter function first which is given below the main matlab code.) In this way, we get a set of Gabor filters with different frequencies and orientations which are helpful for extracting desired features from an image. We would get now the output image for all the values of lambda and theta by doing convolution of the input image pixels and corresponding values of the output of gabor filter function.
7. 8.
Here, lambda represents the wavelength of the sinusoidal factor. theta represents the orientation of the normal to the parallel stripes of a Gabor function psi is the phase offset. sigma is the sigma of the Gaussian envelope and Gamma is the spatial aspect ratio, and specifies the ellipticity of the support of the Gabor function.
MATLAB CODE:
clc; clear all; close all; I=imread('zebra.jpg'); I=rgb2gray(I); [m n]=size(I); I=im2double(I); imshow(I);title('original'); % sigma=input('the value of sigma'); % psi=input('the value of psi'); % gamma=input('the value of gamma'); % a=input(' no. of lambda you want to take'); % lambda=input('the different values of lambda'); % b=input(' no. of theta you want to take'); % theta=input('the different values of theta');
sigma=0.1; psi=0.2; gamma=0.1; a=2; lambda=[3 7] b=2; theta=[5 11] figure; k=0; newimage=zeros(m,n); for i=1:a l=lambda(i) for j=1:b t=theta(j); A=gabor_fn(sigma,l,t,psi,gamma); display('value of lambda');display(l); display('value of theta ');display(t); display('output of gabor filter will be');display(A); k=k+1 % subplot(1,b,j); OUTPUT=conv2(I,double(A),'same') for i=1:m for j=1:n if OUTPUT(i,j)>=1 OUTPUT(i,j)=1; end end end subplot(a,b,k) imshow(OUTPUT); newimage=newimage+OUTPUT; end end figure;imshow(newimage/9);
FILTER FUNCTION: function gb=gabor_fn(sigma,theta,lambda,psi,gamma) xsigma = sigma; ysigma = sigma/gamma; % Bounding box nstds=3 y1 = max(abs(nstds*xsigma*sin(theta)),abs(nstds*ysigma*cos(theta))); y1 = ceil(max(1,y1)); x1 = max(abs(nstds*xsigma*cos(theta)),abs(nstds*ysigma*sin(theta))); x1 = ceil(max(1,x1)); x2 = -x1; y2 = -y1; [x,y] = meshgrid(x2:x1,y2:y1); % Rotation xtheta=x*cos(theta)+y*sin(theta); ytheta=-x*sin(theta)+y*cos(theta); gb= 1/(2*pi*xsigma *ysigma) * exp(.5*(xtheta.^2/xsigma^2+ytheta.^2/ysigma^2)).*cos(2*pi/lambda*xtheta+psi);
OUTPUT:
original
INFERENCE:
We used different types of spatial filters based on our purpose. Each and every filter is suited to some specific applications and we cant use single filter for all the applications. Even for noise removal based on the type of noise that has corrupted the input image, the filter employed would differ. Again for some other applications like feature extraction and image segmentation, the filter employed would be quite different. 1. We used median filter for removing salt and pepper noise as it utilizes median of its neighborhood pixels and removes the extreme black or white pixel values corresponding to 255 and 0. So the image got smoothened to quite a large extent. 2. Also we used another type of filter, an averaging filter to remove salt and pepper noise. It utilized average of its neighborhood pixels. It reduced the sharp transitions in gray levels. But there was an added disadvantage that there was smearing effect in output image. As we are replacing each and every pixel by the average of all its neighboring pixels, so blurring effect came into picture. 3. Then for removing another type of noise which is Gaussian noise, we used Wiener filter which is based on a statistical approach. Wiener filter is also a type of linear time invariant filter as its inputs are fixed. Here the spectral properties of the original signal and the noise were already known. It effectively removed Gaussian noise. 4. Then we designed Gabor filter which is widely used for many applications like feature extraction, image segmentation and edge detection also. We designed it for extracting particular features of our input image. We wished to highlight just the black stripes of our input zebra image and we achieved it by adjusting the values of lambda and theta. It was seen that depending on the values of lambda and theta we are taking, our filter function and consequently the output image was changing. Theta and lambda parameters which we took while defining filter function govern the output image.
REFERENCES:
1. Digital Image Processing by Gonzalez 2. An introduction to Digital Image Processing with Matlab Notes for SCM2511 Image Processsing1 by Alasdair McAndrew 3. Wiener Filtering and Image Processing. www.clear.rice.edu/elec431/projects95/lords/wiener.html 4. bmia.bmt.tue.nl/education/courses/fev/course/.../Gabor_functions.pdf