Anda di halaman 1dari 4

Goa College of Engineering

Experiment No. 7 Date:


Spatial Domain Filtering

AIM: Implement and observe spatial domain filtering

SOFTWARE TOOLS: MATLAB

THEORY:
Filtering is a technique for modifying or enhancing an image. Spatial domain
operation or filtering (the processed value for the current pixel processed value
for the current pixel depends on both itself and surrounding pixels). Hence Filtering
is a neighborhood operation, in which the value of any given pixel in the output
image is determined by applying some algorithm to the values of the pixels in the
neighborhood of the corresponding input pixel. A pixel's neighborhood is some set
of pixels, defined by their locations relative to that pixel.

Linear filtering of an image is accomplished through an operation called


convolution. Convolution is a neighborhood operation in which each output pixel is
the weighted sum of neighboring input pixels. The matrix of weights is called the
convolution kernel, also known as the filter. A convolution kernel is a correlation
kernel that has been rotated 180 degrees.

Following steps are used to compute the output pixel at position (x,y):
1. Rotate the correlation kernel 180 degrees about its center element to create a
convolution kernel.
2. Slide the center element of the convolution kernel so that it lies on top of the
(x,y) element of A.
3. Multiply each weight in the rotated convolution kernel by the pixel of A
underneath.
4. Sum the individual products from step 3.

Types of filters:

Low pass filter:


The basic idea is replace each pixel by the average of the pixels in a square
window surrounding the pixel. This is also called as average filtering or smoothing
operation.

1|Page Dept. of Electronics and Telecommunication


Engineering.
Goa College of Engineering
In statistic and image processing, to smooth a data set is to create an
approximating function that attempts to capture important patterns in the data,
while leaving out noise or other fine-scale structures/rapid phenomena. In
smoothing, the data points of a signal are modified so individual points (presumably
because of noise) are reduced, and points that are lower than the adjacent points
are increased leading to a smoother signal. Smoothing may be used in two
important ways that can aid in data analysis by being able to extract more
information from the data as long as the assumption of smoothing is reasonable by
being able to provide analyses that are both flexible and robust. Variety of
algorithms are used in smoothing.

High Pass Filter:


These are also called as sharpening filters. The main idea of sharpening is
the enhance line structures of other details in an image. Thus, the enhanced image
contains the original image with the line structures and edges in the image
emphasized.

The first order derivative at an image location is approximated using


intensity difference values around a pixel. Examples of operators used for
computing intensity differences are the Robert’s cross gradient, Prewitt, and Sobel
operators. Robert’s cross gradient operators use an even sized mask and hence
lack in symmetry. Prewitt operator uses a 3×3 mask, but performs poorly in the
presence of noise. Commonly used gradient operator is the sobel operator. It
provides for smoothing against noise while performing the differentiation.

Source Code:
Low pass filter

img = imread('cameraman.tif');
imgd = im2double(img); % imgd in [0,1]
f = ones(3,3)/9;
img1 = filter2(f, imgd);
subplot(121); imshow(img);
subplot(122); imshow(img1);

% As mentioned earlier, the low pass filter can be used denoising. Let's test it.
First, to make the
% input a little bit noisy, we spray some pepper and salt on the image, and then
apply the mean

2|Page Dept. of Electronics and Telecommunication


Engineering.
Goa College of Engineering
% filter:
img = imread('cameraman.tif');
imgd = im2double(img); % imgd in [0,1]
imgd = imnoise(imgd,'salt & pepper',0.02);
f = ones(3,3)/9;
img1 = filter2(f, imgd);
subplot(121);
imshow(imgd);
subplot(122);
imshow(img1);

% Extra Snippets
% Program for implementation of smoothing or averaging filter in spatial domain
I=imread('trees.tif');
subplot(2,2,1);
imshow(J);
title('original image');
f=ones(3,3)/9;
h=imfilter(I,f,'circular');
subplot(2,2,2);
imshow(h);
title('averaged image');

The above code has some effect on the salt and pepper noise but not much.
It just made them blurred. We can use Matlab's built-in median filter to improve the
resultant image.

I = imread('cameraman.tif');
J = imnoise(I,'salt & pepper',0.02);
K = medfilt2(J);
subplot(121);
imshow(J);
subplot(122);
imshow(K);

3|Page Dept. of Electronics and Telecommunication


Engineering.
Goa College of Engineering

High Pass filter

( Use the following kernel )

kernel = [-1 -1 -1;-1 8 -1;-1 -1 -1];


filteredImage = imfilter(grayImage, kernel, 'same');

CONCLUSION:

----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------

4|Page Dept. of Electronics and Telecommunication


Engineering.

Anda mungkin juga menyukai