Lab 5 Numerical Methods TNCG17 Digital Image and Fourier Transform Sasan Gooran (Autumn 2009)
Before starting this lab you are supposed to do the preparation assignments of this lab. All functions and files you might need during this lab can be found under: www.itn.liu.se/~sasgo/tncg17/files/lab5 1. Variable types: Input image Campus.tif using imread and name it myimage1. Display the image by imshow. Normalize the image by means of division by 255 and name the result myimage2. Why 255? Display myimage2 using imshow(myimage2) and imshow(double(myimage2)) in two different windows. What happens? Run the following commands in Matlab and contemplate what the commands do. >> myimage=imread( Campus.tif ); >> myimage=double(myimage)/255; >> myimage1=myimage(1:2:end,1:2:end); >> imshow(myimage1) >> imshow(myimage1(:)) >> myimage2=myimage; >> myimage2(100:200,200:400)=1; >> imshow(myimage2) >> myimage3=myimage(1:3:end,1:4:end); >> imshow(myimage3) 2. Downsample and Upsample Input the image Campus.tif. Convert the image to double and normalize it. Downsample the image by taking every second row and column. Upsample
the downsampled image to its original size by imresize. Use all three options for interpolation and discuss the differences in results. 3. Rotation Input the image Campus.tif. Convert the image to double and normalize it. Function imrotate can be used to rotate the image. Check the size of the rotated image and compare it with the size of the original image. Is your calculation in preparation assignment correct? Use different interpolation methods and discuss differences in results. 4. Convolution 4.1 Input the image Campus.tif. Convert the image to double and normalize it. Use function conv2, which performs the two dimensional convolution, and filter the image by following filter. filter1=ones(5)/25. (this is an averaging filter). What does this filter do? 4.2 Input the image Campus.tif and normalize and name it c. Add some white noise to this image by: >> cr = c + (rand(size(c)) 0.5)*0.25; Try to understand what the above command does. Display cr. You can clearly see that cr is a noisy image. Reduce the noise by filtering cr by the filter in assignment 4.1. Any improvement?
Change the size of the filter (both decrease and increase) and see how these changes affect the results. Your comments: 5. Fourier Transform Function fft2 finds the two dimensional discrete Fourier transform of an image. The result is a matrix the same size as the digital image. The result at frequency (0, 0) is put at position (1, 1). In order to move this frequency to the center of the Fourier spectrum you can use fftshift. Functions abs and angle can be used to find the magnitude and the phase of a matrix with complex elements. The two dimensional inverse Fourier transform is performed by ifft2. 5.1 Frequency In order to understand one of the common uses of Fourier transform, which is to find the frequency components of a signal buried in noisy spatial domain signal, run the following commands in Matlab. Create a two dimensional mesh by: >> [X,Y]=meshgrid([0:0.01:1],[0:0.01:1]); Both X and Y are now 101 x 101 matrices with elements varying from 0 to 1 with a step of 0.01. Here we use the function surf to plot a number of simple two dimensional signals. >> surf(x,y,ones(101)) >> surf(x,y,rand(101)) >> surf(x,y,sin(2*pi*x)) >> surf(x,y,sin(2*pi*y)) >> surf(x,y,sin(2*pi*(x+y))) >> surf(x,y,sin(2*pi*(x))+sin(2*pi*y)) Make sure that you understand the commands above and continue!
We now create a sinusoid signal containing two main frequencies, one with an amplitude of 0.8 and the other one with 1, >> z=sin(2*pi*(4*(x)))+0.8*cos(2*pi*(10*x)); >> surf(x,y,z) By plotting the magnitude of the two dimensional Fourier transform of this signal, named Z, you can clearly see the main frequencies that the signal contains, >> surf(x,y,abs(z)) Please have a look at the amplitude. How can you find the right amplitude of each frequency from the Fourier plot? Now we corrupt the signal with a zero-mean random noise. >> zr=z+2*(rand(size(x))-0.5); >> surf(x,y,zr) As you can see it is really hard to see the main frequencies of the original signal here. We now go to the Fourier domain, >> Zr=fftshift(fft2(zr)); >> surf(x,y,abs(zr)) As you see the two frequency components are easy to find in the Fourier domain. You can also find their amplitudes. Your explanation on what you learned from these exercises:.
5.2 Bandwidth Define a signal equal to one in Matlab and calculate its Fourier transform by, >> [X,Y]=meshgrid([0:0.01:1],[0:0.01:1]); >> o1=ones(101); >> O1=fftshift(fft2(o1)); Plot both the signal and its Fourier transform by: >> surf(x,y,o1) >> figure >> surf(x,y,abs(o1)) As you might have noticed the signal in the spatial domain is wide and its Fourier transform on the other hand is narrow. That is always true that the wider the signal the narrower its Fourier transform, and vice versa. Make sure that you understand this fact by studying o2 and o3 below and their respective Fourier transform. >> o2=zeros(101); >> o2(41:61,41:61)=1; >> o3=zeros(101); >> o3(21:81,21:81)=1; Which signal above is wider? Which Fourier transform is narrower? 5.3 Mean Value In the preparation assignments you showed that the mean (or the sum) of the elements in the digital image equals its Fourier transform at frequency (0, 0). Verify that by an example. (You can verify that by using an image, or simply create your own matrix that doesn t necessarily need to be large)
5.4 Real Digital Images In the preparation assignments you showed that the magnitude and the phase of a real image is symmetric and antisymmetric around frequency (0, 0), respectively. Verify that with at least one real matrix (image). 5.5 Convolution Convolution in the spatial domain is equivalent with multiplication in the Fourier domain. Verify that by performing the filtering in assignment 4.1 in the Fourier domain. You must show that your result obtained here are exactly equal to that obtained previously in assignment 4.1.