Image Processing: Week 5: Image...

3
1 Image Processing: Week 5: Image Filtering Instructors: Denis Tomè ([email protected]), Martin Rünz ([email protected]), Adrian Penate-Sanchez ([email protected]), James Hennessey ([email protected]) In the last lecture you learned about linear image filtering operations like correlation and convolution and how these can be applied directly on the image or the Fourier transform of the image. These kind of filtering operations have many uses, ranging from smoothing and noise reduction, to feature enhancement and sharpening, calculating image derivatives, edge detection and even template matching. Your task today is to code your own linear filtering functions operating on the image domain and to study the Fourier transform. Task 1: Convolution Convolution is a fundamental operation for applying linear filters to images. It takes as input an image (greyscale) I and a filter kernel K and returns the result I’ of convolving the image with this filter. Definition: Given a greyscale image I and a filter K of size (2N+1) x (2M + 1) (we use odd dimension filters like 3 x 3, 5 x 5 etc.), convolution is defined as: Your task is to write your own convolution function (do not use the conv2() function) and test it with different filters. How will you handle the behaviour of the filter in the edges of the image (pad with zeros, copy the edge etc.)? Smoothing filters that reduce noise in images are quite useful in image processing. Use a simple mean filter like: This filter replaces each pixel by the average of its neighbourhood. Now if we want something better, we can try weighing the contributions of the neighbouring pixels by their proximity to the centre pixel. Try a Gaussian filter, which is defined as:

Transcript of Image Processing: Week 5: Image...

Page 1: Image Processing: Week 5: Image Filteringweb4.cs.ucl.ac.uk/teaching/GV12/res/2017/week_05/week_05.pdf · 2017. 11. 13. · Image Processing: Week 5: Image Filtering Instructors: Denis

1

Image Processing: Week 5: Image Filtering Instructors:

Denis Tomè ([email protected]), Martin Rünz ([email protected]), Adrian Penate-Sanchez ([email protected]), James Hennessey ([email protected])

In the last lecture you learned about linear image filtering operations like correlation and convolution and how these can be applied directly on the image or the Fourier transform of the image. These kind of filtering operations have many uses, ranging from smoothing and noise reduction, to feature enhancement and sharpening, calculating image derivatives, edge detection and even template matching. Your task today is to code your own linear filtering functions operating on the image domain and to study the Fourier transform. Task 1: Convolution Convolution is a fundamental operation for applying linear filters to images. It takes as input an image (greyscale) I and a filter kernel K and returns the result I’ of convolving the image with this filter.

Definition: Given a greyscale image I and a filter K of size (2N+1) x (2M + 1) (we use odd dimension filters like 3 x 3, 5 x 5 etc.), convolution is defined as:

Your task is to write your own convolution function (do not use the conv2() function) and test it with different filters. How will you handle the behaviour of the filter in the edges of the image (pad with zeros, copy the edge etc.)?

Smoothing filters that reduce noise in images are quite useful in image processing. Use a simple mean filter like:

This filter replaces each pixel by the average of its neighbourhood. Now if we want something better, we can try weighing the contributions of the neighbouring pixels by their proximity to the centre pixel. Try a Gaussian filter, which is defined as:

Page 2: Image Processing: Week 5: Image Filteringweb4.cs.ucl.ac.uk/teaching/GV12/res/2017/week_05/week_05.pdf · 2017. 11. 13. · Image Processing: Week 5: Image Filtering Instructors: Denis

2

where σ is thestandarddeviationof theGaussian.This isacontinuous function,so foryour filteryouwillneedtosampleitinbothxandydirections(usemeshgrid()),keepinginmindthevalueforσthatyouchose.Trydifferentfiltersizesanddifferentvaluesforσandcomparetheresults.

Herearesometestresults:

Task 2: Fourier Transform

The second task for today is related to Fourier transforms. As you have learned in class, the Fourier transform is a very useful way to do image filtering and other image operations. In this task, you will study the magnitude and phase of the Fourier transform. The task is simple, you just need to follow these steps:

1. Load two greyscale images 2. Convert them to the Fourier domain (use fft2()) 3. Plot the magnitude and phase of the two Fourier transforms (use abs() and angle()) 4. Now try switching the phase between the two Fourier transforms and reconstruct the

original images. If you have the magnitude, say |C| and the phase, say θ, of the Fourier complex numbers, you can use this formula to convert back to the original

Page 3: Image Processing: Week 5: Image Filteringweb4.cs.ucl.ac.uk/teaching/GV12/res/2017/week_05/week_05.pdf · 2017. 11. 13. · Image Processing: Week 5: Image Filtering Instructors: Denis

3

complex numbers. So you can replace the phase of one image with the phase of the other image, and then invert the Fourier transform to see the result (use ifft2()).

What did you observe? What does this tell you about the image information carried in the magnitude and in the phase of the Fourier transform?

Here are some test results:

Figure 2.1

Notes:

1. Matlab’s function fft2 computes the 2-D Fourier transform of the image, where the zero-frequency component is not in the center as shown in figure 2.1, but rather in the top-left corner. In order to have the same results, a shift operation is required. fftshift function shifts the zero-frequency component to the center of the output.

2. When displaying the magnitude, the range of values is NOT between [0,1] as expected by Matlab, but instead much larger. The solution is to display the log value of the magnitude, and to define inside the plot function the minimum and maximum intensities. E.g. imshow(logC, [min(logC(:)) max(logC(:))]);