You are on page 1of 5

Applying filtering techniques to image

In following image, apply average filter to image with Gaussian noise.


Code:

I = imread('cameraman.tif');
%% Show current image.
imshow(I);
figure;
G=imnoise(I,'gaussian',0.0005,0.0019);
imshow(G);
figure;
h = fspecial('average', 3);
F=imfilter(G,h);
imshow(F);

***********Sobel

clc;
%% create a new figure to show the image .
newImg = imread('Water lilies.jpg');
figure(1);
%% show the loaded image.
imshow(newImg);

figure(2);
%% convert RGB to gray scale.
I = rgb2gray(newImg)
imshow(I);
%% apply sobel filter.
size(I)
BW = edge(I,'sobel',0.1) ;
imshow(BW);

***********Prewitt

clc;

%% create a new figure to show the image .


newImg = imread('Water lilies.jpg');
figure(1);
%% convert RGB to gray scale.
grayImage= rgb2gray(newImg)
imshow(grayImage);
figure(2);
%% apply prewitt filter.
afterFilter = edge(grayImage,'prewitt')
imshow(afterFilter);
***********Laplacian

clc;

%% create a new figure to show the image .


newImg = imread('Water lilies.jpg');
figure(1);
imshow(newImg);
figure(2);
%% create laplacian filter.
H = fspecial('laplacian');
%% apply laplacian filter.
blurred = imfilter(newImg,H);
imshow(blurred); title('Edge detected Image')

************Roberts filter

%% apply robert filter .


clc;

%% create a new figure to show the image .


newImg = imread('Water lilies.jpg');
figure(1);
%% convert RGB to gray scale.
grayImage= rgb2gray(newImg)
imshow(grayImage);
figure(2);
%% apply roberts filter.
robertsResult = edge(grayImage,'roberts')
imshow(robertsResult);

*************Median filter

%% read the image from the file system to Matrix I


I = imread('cameraman.tif');

%% create a new figure to show the image .


figure(1);
%% show the loaded image.
imshow(I);

%% apply median filter using the internal matlab function medfilt2 .


Y=medfilt2(I,[5 5]);

figure(2);
%% show image after applying the filter
imshow(Y);

%% write the new image to the file system .


imwrite(Y,'newimage.gif','GIF');

***********Weiner filter
%% apply wiener filter .
clc;

newImageRGB = imread('saturn.png');
%% convert image to gray level
grayImage = rgb2gray(newImageRGB);
figure;
imshow(grayImage);

%% adding noise to image .


afferNoise = imnoise(grayImage,'gaussian',0,0.025);
figure;
imshow(afferNoise)
%% applying wiener filter ot image.
afterWiener = wiener2(afferNoise,[6 6]);
figure, imshow(afterWiener)

**********************

Remove Noise by Adaptive Filtering

The wiener2 function applies a Wiener filter (a type of linear filter) to an image adaptively,
tailoring itself to the local image variance. Where the variance is large, wiener2 performs little
smoothing. Where the variance is small, wiener2 performs more smoothing.

This approach often produces better results than linear filtering. The adaptive filter is more
selective than a comparable linear filter, preserving edges and other high-frequency parts of an
image. In addition, there are no design tasks; the wiener2 function handles all preliminary
computations and implements the filter for an input image. wiener2, however, does require more
computation time than linear filtering.

wiener2 works best when the noise is constant-power ("white") additive noise, such as Gaussian
noise. The example below applies wiener2 to an image of Saturn that has had Gaussian noise
added.

1. Read in an image. Because the image is a truecolor image, the example converts it to
grayscale.
2. RGB = imread('saturn.png');
I = rgb2gray(RGB);

3. The example then add Gaussian noise to the image and then displays the image. Because
the image is quite large, the figure only shows a portion of the image.
4. J = imnoise(I,'gaussian',0,0.025);
imshow(J)
Portion of the Image with Added Gaussian Noise

5. Remove the noise, using the wiener2 function. Again, the figure only shows a portion of
the image
6. K = wiener2(J,[5 5]);
figure, imshow(K)

Portion of the Image with Noise Removed by Wiener Filter

***********************

Remove Salt and Pepper Noise from Image

Read image into workspace and display it.

I = imread('eight.tif');
figure, imshow(I)
Add salt and pepper noise.

J = imnoise(I,'salt & pepper',0.02);

Use a median filter to filter out the noise.

K = medfilt2(J);

Display results, side-by-side.

imshowpair(J,K,'montage')

Remove Salt and Pepper Noise from Image Using a GPU

Read the image into a gpuArray.

I = gpuArray(imread('eight.tif'));

Read the image into a gpuArray.

J = imnoise(I,'salt & pepper',0.02);


K = medfilt2(J);
figure, imshow(J), figure, imshow(K)

You might also like