Vous êtes sur la page 1sur 5

UNIVERSITY OF EAST ANGLIA

School of Computing Sciences


CMPC3I16 | Computer Vision LABORATORY SHEET 2 | Image Pre-processing for Computer Vision

The aim of this laboratory session is to give you experience of basic image processing operations, and in particular image processing in the Matlab working environment. Many high-level computer vision algorithms require some form of pre-processing of the image(s) to ensure robust and reliable operation. To reenforce the material covered in the lectures, during this laboratory session we will use some of the built-in Matlab functions for image processing. The goal is to understand basic image processing operations, and to see the e ect of changing the lter properties.
1 Getting Started

Matlab is started by double-clicking the desktop icon, if it is available, or by selecting the icon from relevant program-group in the start menu. Matlab may take several moments to fully initialise. Initialisation is complete when a blinking cursor is presented at the command prompt, or `Ready' is displayed in the bottom left-hand corner of the Matlab window. Use the available help if you are having problems. If you want to locate a command, but are not sure of the exact name, use the lookfor command.
>> lookfor keyword

will search the help and return a list of functions that contain a given keyword, and
>> help command

displays the help for the named command.


2 Reading and Displaying Images

An image can be read from a le and into the Matlab workspace as a variable, which can then be displayed in a gure window using:
>> img = imread(`cameraman.tiff'); % Reads the image from file. >> imshow(img); % Displays the image in a figure window.

The string argument to imread is the name of an image | in this case it is an image installed with Matlab. This can be replaced with any lename (and where appropriate le path). Looking at the top left region of the Camera Man image (in the sky), the image appears reasonably at (approximately the same intensity). Consequently, if we were to sample the image along a pro le vertically through the image, we might expect only a relatively small in intensity. However, if we were to do the following:
CMPC3I16 Laboratory Sheet 2

Spring 2010

>> x = improfile; >> figure; plot(x, `k');

% Sample intensities along a profile in the image. % Plot the intensities in a new figure window.

We can see the intensities as raw numbers to get an idea of how much variation there is these relatively at regions. The pro le and the sampled intensities are shown in Figure 1.

Figure 1: Camera man image (left), with a vertical pro le marked in red, and the corresponding images intensities sampled along that pro le plotted as numbers (right). Although we perceive the region to be at, the image intensities vary up to almost one tenth the entire range (0{255) along the pro le. The performance of automated vision systems can degrade due to noise such as this.
3 Linear Image Filters

Recall, the operation of a 2D image lter is given by the discrete convolution:


G x; y

XX )=
a b

m= a n= b

f x

m; y

n h m; n ;

) (

(1)

where the size of the kernel is 2a + 1 2b + 1. The kernel is centred at a pixel, the sumof-products between the kernel coecients and the image intensities beneath it is computed, and the resulting value is copied to the pixel location in the output image. The kernel is then centred over the next pixel and the process repeated for all pixels in the image. This de nes the behaviour of all of the linear lters we will look at, what di ers is the coecients (values) in the kernel | these de ne the type/operation of the lter, the ltering process itself is the same. To convince you of this, you could write your own function that implements the above and see that the same underlying ltering code produces a di erent operation depending on the kernel. Matlab, in conjunction with the Image Processing Toolbox has many functions to support both linear and non-linear image processing operations. Two functions we are particularly interested in here are fspecial, which creates a lter kernel for a named lter type with the speci ed properties, and imfilter, which lters an image using a given kernel.
CMPC3I16 Laboratory Sheet 2

Spring 2010

Box Filters

A box lter is a crude lter used for smoothing an image | the pixel intensities in the neighbourhood (beneath the kernel) are averaged, and the mean value used to represent the pixel in the ltered image. In this instance all kernel coecients are the same (equal to the reciprocal of the number of coecients in the lter). For example, to construct the kernel for a 3 3 box lter we could do:
>> h = (1/9) * ones(3,3);

or we could used the supplied fspecial function, as follows1 :


>> h = fspecial(`average');

Implement both of these operations to convince you they are the same. Given the camera man image and the lter kernel, the next step is to apply the lter. This can be done using the imfilter command, as follows:
>> img_box = imfilter(img, h); >> imshow(img_box);

What is the e ect of averaging the pixel intensities in the image? What would be the e ect of increasing the lter size? Try it and see. The default behaviour of imfilter is to assume pixels outside of the image are black (0). You should notice an of e ect of increasing the size of the lter kernel is strange border artefacts at the edge of the image. The black border appears to increase with the size of the kernel. How might you overcome this problem of the black border? Look at the input parameters for imfilter and try them out.
Gaussian Filters

A linear lter with a Gaussian shaped kernel is generally a better option than a box lter. Rather than all pixels having equal weight in computing the average, a Gaussian down-weights the in uence of pixels proportionally with distance from the centre pixel. There are two properties of the Gaussian kernel that must be de ned. The rst is the size of the kernel itself and the second is the standard deviation (width of the Gaussian). Recall the area under Gaussian curve is xed, therefore a lter with a larger width has a lower overall height at the centre. You can easily verify this looking at the shape of the lters you create. For example:
>> >> >> >> >>
1

h1 = fspecial(`gaussian', [17 17], 0.5); h2 = fspecial(`gaussian', [17 17], 2); [X,Y] = meshgrid(1:size(h1,1)); figure; surf(X,Y,h1); figure; surf(X,Y,h2);

fspecial will compute the kernel for a host of lter types | see the documentation for more information on the types of lter and the particular parameters for each.

CMPC3I16 Laboratory Sheet 2

Spring 2010

and notice the height of the peak in both cases. Construct a number of lters, changing both the size of the kernel and the width of the Gaussian, and notice the e ect of the lter that results from changing the parameters.
3.1 Laplacian Images

In the lectures we saw that the Laplacian (magnitude of the second derivative in one direction) of an image can be computed using the following kernel: 0 1 0 1 -4 1 0 1 0 Using dIx = I (x + 1) I (x) as an approximation of the rst derivative, show the above kernel dx is correct. Compute the Laplacian of the camera man image (if you use fspecial, be sure you have generated the kernel above!), and notice the many false edges in the result2 . It was proposed in the lectures that combing both smoothing and gradient based approaches can make edge detectors more robust to noise, even though the e ect of smoothing is to reduce the strength of edges in the image. Verify this using the lters you have looked at in this session | does smoothing the original image, then computing the Laplacian of the smoothed image give more reliable edges?
4 Non-linear Filters

There are numerous non-linear lters we might apply to an image | max, min, and median lters for local brightening, darkening and smoothing respectively, and the erosion, dilation, opening, and closing operators to smooth contours in the images, to name but a few.
4.1 Noise Removal

For certain types of noise, non-linear lters are much more sensible than linear lters. This can easily be demonstrated with a simple example. Firstly, load the coins image:
>> img = imread(`coins.png');

Next add noise to the image. For this example we will use the salt and pepper noise model. See the help for the imnoise function for alternatives.
>> imgn = imnoise(img,`salt & pepper', 0.1); >> figure; imshow(imgn);
2

imshow(imfilter(img,h)>25);.

These spurious edges might be more obvious if you threshold the ltered image,

e.g.,

CMPC3I16 Laboratory Sheet 2

Spring 2010

What does the parameter 0.1 specify in the example above? Firstly attempt to restore the image using a low-pass (Guassian) lter. Does this work? To restore the grey background, what can you deduce about the required kernel size? What does this do to the foreground objects? Is this approach suitable? Rather than a linear lter, use a 2-dimensional median lter to attenuate the noise (medfilt2). Try with di erent size lters | does this work better? What happens as the lter size increases? For the lter size you deduce as optimal, is the restored image exactly the same as the original? If not, can you determine how close they are?
4.2 Morphological Filters

Morphological lters are useful for smoothing contours and removing artefacts in binary, greyscale, and colour images | we will consider only binary images. We might wish to do this, for example, to clean up a crude image segmentation. From the coins image used previously, we can see the intensity of the pixels representing the coins are relatively bright compared with those representing the background. A simple segmentation of the image might then just look for bright pixels:
>> M = img > 128; >> figure; imshow(M);

This, mostly, does a reasonable job of segmenting the coins from the background. However, it isn't perfect. Some of the foreground pixel intensities fall below the decision threshold (128), and thus are classi ed as belonging to the background. A median lter will do a reasonable job in most of the cases, but one coin in particular is problematic. Four morphological operators were introduced in the lectures: namely erosion, dilation, opening and closing. These are implemented in Matlab as imerode, imdilate, imopen and imclose, respectively. In conjunction with these a structuring element must be de ned, which is done using the strel function. Filter the binary mask computed from the coins image with the various morphological operators to investigate which is best suited for this particular case. Also, which shape of structuring element works best here? (hint: think of the shape of the object you are attempting to clean). What happens as the structuring element increases/descreases in size?

Dr B.J. Theobald & Dr M. Mackiewicz Jan. 2010


CMPC3I16 Laboratory Sheet 2

Spring 2010

Vous aimerez peut-être aussi