Filters

Lesson Objectives

  • What a filter does.
  • Dealing with edges.
  • Linear and Non-linear fitlering

A filter is essentially an array that moves across another array performing computations on each pixel based on the value of a surrounding pixel. In this section we will look at a linear filter. Linear filters perform the same linear operation on all the surrounding pixels in order to determine the value of a pixel.

A filter can be any array. For example, let's create a 3 x 3 array h

h=randi(3,3); % the function randi creates random integers
% Create an array of ones as a dummy image
image_ones= ones(10, 10);

Let us apply the filter h to image_test using the imfilter function.

filtered_array= imfilter(image_ones, h);

Open arrays image_ones and filtered_array. What has changed?

Matlab has an inbuilt function to make the filter. We can do this using the fspecial function like this.

  h= fspecial('average', [3,3]); % create the filter array.

If we open the array h now, we can see that the average filter has given equal weighting to each of the pixels. Let us apply h to image_ones.

filtered_array= imfilter(array_1, h);% apply filter.
imshow(filtered_array) % Display image

Challenge 1

% Apply the average filter h to the image cameraman and display the original image and the filtered image side by side.
% Load in the inbuilt Matlab image cameraman.tif
% Apply the filter h to cameraman
% Initiate figure and the first subplot
% Display cameraman and give it the title 'original image'
% Initiate second subplot
% Display the filtered image and give it a title

Using a filter to remove random noise

A lot of us deal with images with some random noise in it. This noise is usually introduced in the data acquisition step and we will need to remove the noise. We can use image filtering to remove the noise. But before we do this, let us make our own noisy image. The image processing toolbox has its own noise making function imnoise.

% Read in cameraman.tif
cameraman= imread('cameraman.tif');
cameraman_noise= imnoise(cameraman); % Make cameraman noisy
% Display the original image and the noisy image side by side. Leave extra panels for the filtered images later.

subplot(2,2,1)
imshow(cameraman); colormap gray; title('Original Image');
subplot(2,2,2)
imshow(cameraman_noise); title('noisy image');

Let us now apply the average filter we used earlier to cameraman_noise to try and filter out the high spatial frequency noise that we have introduced.

h= fspecial('average', [3,3]); % make sure h is in our workspace
cameraman_filter1= imfilter(cameraman_noise, h); %applying the filter to cameraman_noise
subplot(2,2,3)
imshow(cameraman_filter1); title('filtered image(average 3x3)');

Let us apply a larger filter to the image.

h= fspecial('average', [15,15]); % make sure h is in our workspace
cameraman_filter2= imfilter(cameraman_noise, h); %applying the filter to cameraman_noise
subplot(2,2,4)
imshow(cameraman_filter2); title('filtered image(average 15x15)');

Challenge 2

Create a gaussian filter h with 8 rows and 8 columns and a standard deviation of 2 using the fspecial function. Use the fspecial help documentation. Plot h (Hint: Use imagesc). What does it look like?

Apply the gaussian filter you just created to cameraman_noise. Display cameraman and the filtered image next to one another.

What happens if you change the size of the filter? What happens if you change the size of standard deviation? Why do you have black edges around the sides?

Different types of noise need different filters

The noise that we saw in the previous section was random noise. For the next part we are going to look at a different kind of noise. Different kinds of noise need different kinds of filters.

Let us use our noisy friend imnoise to introduce salt and pepper noise in our image.

%% Introducing noise into an image

cameraman_snp= imnoise(cameraman,'salt & pepper', 0.02); % Applying salt and pepper noise to cameraman.
figure; imshow(cameraman_snp); % Display the cameraman image with noise

When there is salt and peper noise in an image, some pixels have the maximum value of 255 and some others have the minimum of 0.

Challenge 3:

Apply an average filter to the image with salt and pepper noise. What happens?

Median Filter

We used fspecial to create a filter before and apply it to the image. Matlab has different types of filters built in to it.

Median filter is a non=linear filter. This means it does not perform the same action to every pixel. It takes the median of all the pixels in the grid specified.

filt_cameraman= medfilt2(cameraman_noise, [3,3]); %applying the median filter
imshow(filt_cameraman);

results matching ""

    No results matching ""