You are on page 1of 4

High-Level Computer Vision

Summer Semester 2013 Prof. Bernt Schiele, Dr. Mario Fritz <{schiele,mfritz}@mpi-inf.mpg.de> Dr. Fabio Galasso, Siyu Tang <{galasso,tang}@mpi-inf-mpg.de>

Exercise 1: Introduction to Matlab and Image Filtering


In this exercise you will rst familiarise yourself with the essentials of the Matlab programming and then implement several basic image ltering routines which will later be used for more complex image processing tasks such as edge detection. Download the le exercise1.tar.gz from the course web page and unpack it in a separate directory. This le contains a placeholder for each function you will have to implement in this exercise, and your task is to ll the missing code corresponding to each subproblem. The le also includes two images: graf.png and gantrycrane.png, which we will use for testing purposes. The script ex1.m sequentially executes the code corresponding to each subproblem and visualizes the results. Ideally, after you have implemented all the missing code you should be able to execute it without errors.

Question 1: Matlab Tutorial


a) Create your working directory. Run Matlab by clicking on the Matlab icon or by typing matlab in the command shell. Change the directory to your working directory. b) Watch the video Getting Started with Matlab available at http://www.mathworks.de/videos/matlab/ getting-started-with-matlab.html. c) Read the introductory parts of the Matlab guide available at www.mathworks.com/help/pdf_doc/matlab/ getstart.pdf. The chapters you should read are Matrices, Graphics and Flow Control. d) Walk through the following Matlab demos (www.mathworks.com/products/matlab/demos.html): Mathematics Basic Matrix Operations Matrix Manipulation Graphics 2-D Plots 3-D Plots Images and Matrices Programming Manipulating Multidimensional Arrays Function Functions

Question 2: Basic Image Processing


Download the le exercise1.tar.gz from the class web page and uncompress it in your working directory. This le contains two example images, which you will manipulate in the following. For the rst exercises, we provide example code. Try it out and shortly explain what happens. For the later exercises, we only provide a code framework which you should complete yourself. a) Use Matlab documentation to learn about the following commands which we will use in this exercise. imread, image, imshow, imagesc, colormap, imrotate. To obtain information about a particular command you can type help followed by the command name in the Matlab command prompt. b) Read the image graf.png and display it in a window with the command imshow. graf=imread(graf.png); imshow(graf); Convert the color image to gray values and display it with dierent Matlab commands.

graf_gray=rgb2gray(graf) close all; image(graf_gray); imagesc(graf_gray); imshow(graf_gray); c) Look at the colormap. Create and set a new colormap which varies from 1 to 0, 0 to 1, and several random maps. Display the image. Explain what happens. map=colormap; map1=map(end:-1:1,:); map2=rand(64,3); close all; image(graf_gray); colormap(map1); colormap(map2); colormap(default); colormap(gray); d) Brighten and darken the image. bgraf=graf*2; image(bgraf); dgraf=graf/2; image(dgraf); e) Assign row 200 in the image to a variable. Display it with a plot. Repeat that for column 300. row200=graf_gray(200,:); plot(row200); column300=graf_gray(:,300); plot(column300); f) Remove a rectangular region from the image and display the region. Crop a rectangular region from the image containing only the head of the karate man and display it as a new image. close all; graf1=graf; graf1(130:260,240:450, :)=0; imshow(graf1); graf2=graf(130:260,240:450,:); imshow(graf2); g) Crop the head of the chicken and rotate it by 45 degrees. Try dierent methods (nearest, bilinear, bicubic). Explain the observations. close all; subplot(1, 3, 1); graf4=imrotate(graf2,45,nearest); imshow(graf4); subplot(1, 3, 2); graf4=imrotate(graf2,45,bilinear); imshow(graf4); subplot(1, 3, 3); graf4=imrotate(graf2,45,bicubic); imshow(graf4); h) Sample the image (reduce the size). Use dierent sampling step sizes (2, 4, 8). Explain what happens. close all; subplot(1, 3, 1); graf5=graf_gray(1:2:end,1:2:end); imshow(graf5); subplot(1, 3, 2); graf5=graf_gray(1:4:end,1:4:end); imshow(graf5); subplot(1, 3, 3); graf5=graf_gray(1:8:end,1:8:end); imshow(graf5); i) Create a mirror image. Concatenate 2 or 4 images. graf6=graf_gray(:,end:-1:1); imshow(graf6);

graf7=[graf_gray,graf_gray;graf_gray,graf_gray]; imshow(graf7); j) Assign row 200 in the image to a variable. Display it with a plot. Create a lter which is a vector of 10 equal elements summing to 1. Convolve the row with the lter and plot the result. Explain what happens. row200=graf_gray(200,:); plot(row200); filt1=ones(1,10)/10; newrow=conv(double(row200), filt1); hold on; plot(newrow, r-); k) Create a 2D blur lter of size 10 10 which sums to 1. Convolve it with the image and display the new image. Create a 1D lter of size 10 which sums to 1. Convolve it with the lines of the image. Transpose the lter and convolve it with rows of the ltered image. Explain what happens. filtersize=10; filt2D=ones(filtersize,filtersize)/(filtersize*filtersize); graf8=conv2(double(graf_gray),filt2D); imagesc(graf8); filt1D=ones(1,filtersize)/filtersize; graf9=conv2(double(graf_gray),filt1D); graf10=conv2(graf9,filt1D); imagesc(graf10); max(max(graf10 - graf8)) l) Create a function called basicimproc which takes as arguments an image inimg and ve numbers x, y, the size of a square, rotationangle and filtersize. The function should crop a square region out of the image, rotate it by the desired angle, blur the cropped region, and then return it as outimg. It should also display the intermediate result images. Create a new le called basicimproc.m with your preferred editor and begin the script: function outimg=basicimproc(inimg, x, y, size, rotationangle, filtersize) ... ... end

Question 3: Image Filtering (15 points)


a) Implement a method which computes the values of a Gaussian for a given variance and a number of samples. G= x2 1 exp( 2 ) 2 2 (1)

where is the standard deviation and x is a vector of integer values. Open the le gauss.m with your preferred editor and begin the script: function G=gauss(sigma) ... ... end b) Implement a 2D Gaussian lter in gaussianfilter.m. The function should take an image as an input and return the result of convolution of this image with 2D Gaussian kernel. See Fig. 1 for illustration of Gaussian ltering. Width and height of the lter should be 2 (3 sigma) + 1. You can take advantage of the Matlabs conv2 function if you dont want to implement convolution yourself. Open the le called gaussianfilter.m with your preferred editor and begin the script: function outimg=gaussianfilter(inimg,sigma) ... ... end

(a)

(b)

Figure 1: (a) Original image (b) Image after applying a Gaussian lter with = 4.0. Hint: use the fact that 2D Gaussian lter is separable to speed up computations. c) Implement a function gaussdx.m for creating a Gaussian derivative lter in 1D according to the following equation d G = dx = x2 d 1 exp( 2 ) dx 2 2 1 x2 x exp( 2 ) 2 2 3

(2) (3)

The eect of applying a lter can be studied by observing its so-called impulse response. For this, create a test image in which only the central pixel has a non-zero value: imgImp = zeros(25,25); imgImp(13,13) = 1.0; Now, create the following 1D lter kernels G and D. sigma = 6.0; G = gauss(sigma); D = gaussdx(sigma); What happens when you apply the following lter combinations? (For best visualization, display the result image with the imagesc command). 1. 2. 3. 4. 5. rst rst rst rst rst G, then G. G, then D. D, then G. G, then D. D, then G.

d) Use the functions gauss and gaussdx in order to implement a function gaussderiv that returns the 2D Gaussian derivatives of an input image in x and y direction. Try the function on the given example images. Please turn in your solution by sending an email to Fabio Galasso <galasso@ mpi-inf. mpg. de> including all relevant m-les before Wednesday, May 1st, 23:59.

You might also like