1. MATLAB
University of Lincoln School of Computer Science CMP3108M – Image Processing
Week-5: Spatial Filtering
In each section, there are a few new MATLAB built-in functions being used. To know more about these functions, you can Google the function name together with the keyword MATLAB to find the relevant MathWorks documentation, and read the instructions. For example: ‘rgb2gray MATLAB’.
2. Neighbourhood Operations
We saw last week that an image can be modified by applying a particular function to each pixel value. Neighbourhood processing may be considered as an extension of this, where a function is applied to a neighbourhood of each pixel. The idea is to move a ‘kernel’ or ‘mask’ (a rectangle usually with sides of odd length) over the given image. As we do this, we create a new image whose pixels have grey values calculated from the grey values under the mask. A diagram illustrating the process for performing a neighbourhood operation is given in the figure below.
It requires three steps:
1. position the kernel over the current pixel,
2. form all products of kernel elements with the corresponding elements of the neighbourhood,
3. add up all the products.
This must be repeated for every pixel in the image.
It is convenient to describe a mask simply in terms of the coefficients of all the values within the mask. This can be written as a matrix. The averaging mask above, for example, could be described as:
2.1 Task
One important neighbourhood operation is to use a 3×3 mask and take the average of all nine values within the mask. This value becomes the grey value of the corresponding pixel in the new image. This operation may be described as follows:
Here, e is grey value of the current pixel in the original image, and the average is the grey value of the corresponding pixel in the new image. To apply this to an image, consider the 5×5 image obtained by:
Consider the top left 3×3 neighbourhood of our image x:
Now we take the average of all these values by using a nested ‘for loop‘ such as:
x = 255*im2double(x);
sum = 0;
for row = 1:3
for col = 1:3
sum = sum + (1/9)*x(row,col);
end end
The answer is sum = 111.1111 which can be rounded to 111. Alternatively, you can use mean2(x(1:3,1:3)) instead of the nested ‘for loop‘.
Now we can move to the second neighbourhood:
sum = 0;
for row = 1:3
for col = 2:4
sum = sum + (1/9)*x(row,col);
end end
The answer is sum = 108.8889 which can be rounded to 109. If we continue in this manner, the following output is obtained:
This array is the result of 3×3 neighbourhood averaging operation. new MATLAB functions used in this section: mean2, magic
2.2 Task
There is an obvious problem in applying a neighbourhood operation: what happens at the edge of the image, where the mask partly falls outside the image? In such a case, as illustrated in figure below, there will be a lack of grey values to use in the operation.
There are a number of different approaches to dealing with this problem:
Crop (ignore the edges): That is, the mask is only applied to those pixels in the image for with the mask will lie fully within the image. This means all pixels except for the edges, and results in an output image which is smaller than the original. If the mask is very large, a significant amount of information may be lost by this method. We applied this method in our example above.
Pad with zeros: We assume that all necessary values outside the image are zero. This gives us all values to work with, and will return an output image of the same size as the original, but may have the effect of introducing unwanted artefacts (for example, edges) around the image.
You can use y = padarray(x,padsize) to perform the padding in MATLAB such as: y = padarray(x,[1 1])
Figure below illustrates the results of zero-padding of the image to make it ready for the neighbourhood operation using masks of different sizes:
Original Image zero-padded for a 3×3 mask zero-padded for a 5×5 mask
The output image after applying a 3×3 averaging mask would be:
Using pen and paper or a calculator, compute the two pixels at the border and corner (highlighted by red rectangles) by applying a 3×3 neighbourhood for averaging, and see if you get the same values.
new MATLAB functions used in this section: padarray
3. Smoothing Spatial Filters
Smoothing spatial filters are applied in order to reduce noise and/or to prepare images for further processing such as segmentation. We have 2 types of spatial filtering: linear and non-linear.
Linear filtering of an image is accomplished through a neighbourhood operation in which each output pixel is the weighted sum of neighbouring input pixels. The matrix of weights is called the kernel or mask. One good example of linear smoothing filtering is the uniform averaging or mean filter. This is when the output image is based on a local averaging of the input image where all of the values within the filter mask have the same weight. The neighbourhood operation performed in task 2.1 was an example of the averaging filter.
A non-linear filter is a filter whose output is not a linear function of its input. A good example is the median filter. In the following, we will look at examples of both types of smoothing filtering.
3.1 Task
A 4×4 grey-scale image is shown below. Using pen and paper or calculator, compute the mean filtered image values of the pixels highlighted by the red question marks. Apply zero-padding of the input image.
3.2 Task
We can create our filter masks by using the fspecial function; this has many options which makes for easy creation of many different filters. We shall use the average option, which produces averaging filters of given size:
h = fspecial(‘average’, 3)
This will return an averaging filter mask of size 3×3.
In MATLAB, filtering is implemented by the imfilter function. For instance:
B = imfilter (A,h)
This filters the input image A with using the filter mask h. By default, the imfilter function fills in off- the-edge image pixels by assuming that they are 0 (zero-padding).
Try filtering the above image using fspecial and imfilter functions. You can obtain the input image by: A = uint8 ( [3 6 7 4 ; 0 6 9 8 ; 1 45 8 18 ; 2 1 7 9] )
Download the “SampleImages” zip file, un-compress the file, and save the files in your working folder. Now write a MATLAB script to load the image ‘Face.jpg’ or ‘Test.tif’ and convert it to grey- scale if necessary. Then apply a uniform mean filtering of different sizes and display the filtered image together with the original input image. The solution is provided below, but try writing the script yourself before looking at the solution.
InputImage = imread(‘Test.tif’);
if ( length(size(InputImage))> 2 )
InputImage = rgb2gray(InputImage);
end
figure;
subplot(2,2,1);
imshow(InputImage);
title(‘Input Image’);
h = fspecial(‘average’, 3); FilteredImage = imfilter(InputImage,h); subplot(2,2,2);
imshow(FilteredImage);
title(‘Filtered Image 3×3’);
h = fspecial(‘average’, 5); FilteredImage = imfilter(InputImage,h); subplot(2,2,3);
imshow(FilteredImage);
title(‘Filtered Image 5×5’);
h = fspecial(‘average’, 7); FilteredImage = imfilter(InputImage,h); subplot(2,2,4);
imshow(FilteredImage);
title(‘Filtered Image 7×7’);
new MATLAB functions used in this section: fspecial, imfilter 3.3 Task
Median filtering seems almost tailor-made for removal of salt and pepper noise. Recall that the median of a set is the middle value when they are sorted. A median filter is an example of a non-linear spatial filter.
Using a 3×3 mask, the output value is the median of the values in the mask. For example:
The operation of obtaining the median means that very large or very small values (noisy values) will end up at the top or bottom of the sorted list. Thus the median will in general replace a noisy value with one closer to its surroundings.
A 4×4 grey-scale image is shown below. Using pen and paper, compute the median filtered image values of the pixels highlighted by the red question marks. Apply zero-padding of the input image.
3.4 Task
In MATLAB, median filtering is implemented by the medfilt2 function. For instance: B = medfilt2 (A, [m n], padopt )
This performs median filtering, where each output pixel contains the median value in the m-by-n neighbourhood around the corresponding pixel in the input image. If you do not specify the neighbourhood size, a mask of 3×3 will be considered by default. The parameter padopt controls how medfilt2 handles the image boundaries (default padding option is zeros).
Try filtering the above image using the medfilt2 function. You can obtain the input image by: A = uint8 ( [3 6 7 4 ; 0 6 9 8 ; 1 45 8 18 ; 2 1 7 9] )
Now write a MATLAB script to load the image ‘LincolnSP.jpg’ or ‘Test.tif’ and convert it to grey-scale if necessary. Then apply a median filtering of 3×3 and display the filtered image side-by-side the original input image.
new MATLAB functions used in this section: figure, subplot, medfilt2, title
4. Sharpening Spatial Filters
To be added next week.