Digital Media Lab 2014-01-28 Applied physics and electronics Ulrik Söderström Image processing in the spatial and frequency domain Fourier transform, Filtering and Image enhancement
Part 1. The Fourier Transform 1.1. Fourier analysis in two dimensions We are going to look at some simple signals in both the spatial- and the frequency domain. In this exercise we are using gray scale images with the size 64 x 64 pixels. Each pixel has a value between 0 and 1. Sometimes you have to rescale the resulting image to come back to values between one and zero. One way to get information about pixel values is the function "hist", which calculates the frequency for each pixel value. Let's start with a "one dimensional" sine wave. In the following code a 64 x 64 pixel image with two periods of a horizontal sine wave is created. The image is shown with "imshow", so we have to rescale the values to lie between 0 and 1 before displaying it. The signal is centred on the x-axis before the Fourier transform is calculated. (fft, fft2, fftshift) clear; %the image will be in format Q x Q Q =64; %number of periods period=2; %orginal image for m=1:1:q for n=1:1:q orig_image(m,n)=0.5+0.5.*sin(2.*pi.*(n./(q./period))); %the "0.5" constants give values between 0 and 1 for use with "imshow" subplot(1,2,1),imshow(orig_image);title('original image'); %delete the added scaling values before FFT, gives values symmetrically around the x- axis A=orig_image-.5; B=abs(fftshift(fft2(A))); subplot(1,2,2),imshow(b);title('magnitude of FFT'); 1.1.1 Try to explain the result! 1.1.2 What happens with the spectrum is the sine wave is vertical? 1.1.3 Create a black image of size 64 x 64 with a 9 x 9 white square in the middle! In this case you might have to rescale the frequency image before you show the result. Try to use a log-transform (log(abs(fftshift(fft2(bild)))+1)), or rescale the image with the formula (bild2=(bildmin(min(bild)))./(max(max(bild))-min(min(bild)))). Is the spectrum looking like you expected? Describe the result. 1.1.4 Repeat the previous experiment but start with an image with zeros all over except one pixel with value 1.
1.2. The importance of object size Go back to problem 1.1.3 but make the square 15 x 15 instead of 9 x 9. 1.2.1 How is spectrum changed? Explain the correspondence between size in spatial and frequency plane. 1.2.2 Fetch a "real" image (you can find smregit.bmp on the home page) and show the corresponding frequency domain image. Try to explain the result! (imread, ind2gray)
Part 2 - Filtering 2.1. Convolution (filtering in the spatial domain) If the filtering function is known and you want to calculate a specific out-signal from the in-signal, you can use two methods. In the spatial domain convolution is used. You apply convolution to the insignal and the impulse response of the filter. Usually the in-signal is a long sequence and the impulse response is a short sequence - usually called the convolution kernel. (conv, conv2) Create an image with a 15 x 15 square in the middle, like the one used in 1.2. Create the following convolution kernel with the code below: 1 1 1 1 1 1 1 1 1 /9 filter1=ones(3, 3)./9; Calculate the output image through convolution. 2.1.1 How is the image changed in the filtering process? Compare the spectra for the images! 2.1.2 Change the size of the filter to size 7 x 7. This is a mean filter, remember to divide each pixel in the filter with the sum of ones in the kernel. Repeat the previous exercise and compare the result! What importance does the size of the kernel have? 2.1.3 The following filter is a Laplace filter: 0 1 0 1-4 1 0 1 0 filter2=[0 1 0; 1-4 1; 0 1 0]; How is the image and its spectrum affected by this kind of filter? 2.2. Filtering in the frequency domain The other method of filtering is filtering in the frequency domain. The transform of the image is multiplied with a filter that attenuates certain frequencies. The filter can either be created directly in the frequency domain or be the transform of a filter created in the spatial domain. In this case the Fourier transform of the image is multiplied with the Fourier transform of the impulse response - the transfer function. Transform both the image and the 3x3 averaging filter to the frequency domain: [M1 N1]=size(bild); [M2 N2]=size(filter); M=M1+M2-1; %The images has to be zero-padded to this size! N=N1+N2-1;
fftbild=fftshift(fft2(bild, M, N)); % calculate the FFT, fftfilter=fftshift(fft2(filter, M, N)); %automatic zero-pad B1=log(abs(fftbild)+1); B1=(B1-min(min(B1)))./(max(max(B1))-min(min(B1))); C1=log(abs(fftfilter)+1); C1=(C1-min(min(C1)))./(max(max(C1))-min(min(C1))); figure subplot(1,2,1),imshow(b1);title('original'); subplot(1,2,2),imshow(c1);title('filter'); A new, filtered, image (in the frequency domain) is created through multiplication: nybild=fftbild.*fftfilter; %element wise multiplication D1=log(abs(nybild)+1); D1=(D1-min(min(D1)))./(max(max(D1))-min(min(D1))); figure subplot(1,2,1),imshow(d1);title('filtered image, magnitude as a function of frequency'); The last step is to do the inverse transform: nybild=fftshift(nybild); %shift back recreate=ifft2(nybild); subplot(1,2,2),imshow(recreate);title('filtered image'); 2.2.1 Describe the function of the filter. How is the image affected? 2.2.2 Compare with the result in 2.1.1 What is the difference? 2.2.3 See also what happens with a 7 x 7 filter. Compare with 2.1.2! 2.2.4 Change the kernel to a Laplace filter and repeat the filtering. Compare with 2.1.3. 2.2.5 Create a filter directly in the frequency domain. For example, you can create an ideal low pass filter: [M, N]=size(fftbild); radius=15; filter=zeros(m, N); for i=1:m for j=1:n if (((i-(m+1)/2).^2+(j-(n+1)/2).^2).^0.5)<=radius filter(i,j)=1; Remember to do the zero-padding (double the size of the image in the transformation process). After the filtering you can go back to the original size with "imcrop". Try the filter above. What happens if the radius of the filter is changed? As you see - ideal filter will produce ripple.
2.2.6 Change the filter to get the "opposite" effect! If you have used the filter above, change it to a highpass filter. Describe the effect of this filter. 2.2.7 Discuss advantages and disadvantages with filtering in spatial and i frequency domain. You can get some hints on page 166 in the book
Part 3 - Image enhancement 3.1.Sharpening of images With a high pass filter it is possible to emphasize the contour of an object. This is usually a useful first step in further automatic manipulations of the image, but the result does not look "better" for the human eye. It is more common that you want to "sharpen" the image by enhancing its contours. This can be done by combining the original image with a high pass filtered version of it. On the home page you can find the image "debbie4.bmp" (imread, ind2gray). A high pass filter can be combined with the original image through the following filter: 0-1 0 0-1 0 0 0 0-1 5-1 = -1 4-1 + 0 1 0 0-1 0 0-1 0 0 0 0 3.1.1 Convolute the image with the filter. Show the result and describe the effect. 3.2. Histogram equalization An ATM machine in Umeå has been busted and a lot of money has been stolen. An image of the thief was captured by the surveillance system, but unfortunately it was damaged when the film was developed. The image seems to see totally gray ("robber.bmp"). A policeman interested in image processing suggests that histogram equalization should help in identifying the thief. Help the police (histeq) 3.2.1 Show the result - is it possible to identify the thief? Report A report should be written and it shall contain: Task description Solution Answer to all the questions and the choices you have made based on these answers Figures Code Reflections on the task