Digital Image Processing Chapter II - Digital Image Fundamentals (Sections 2.3.4, 2.4 and 2.5) Exercises Introduction on Matlab: 1. For an introduction on Matlab we strongly recommend the student to read the system’s on-line help, particularly the sections MATLAB and subsections Development Environment, Mathematics, Programming and Data Types, and for information about Matlab’s graphical resources, the student should read the subsections Graphics, 3-D Visualization and Creating Graphical User Interfaces. 2. For an introduction on Matlab’s image processing functions we recommend the exercises on this list as well as the reading of the section Image Processing Toolbox and of the subsections Getting Started, Introduction and Displaying and Printing Images of the online help. Exercises: 1. Data Types (type “help datatypes”). 2. Image Types: RGB: Create 3 matrixes (Red, Green and Blue) with the same dimensions, containing values between 0 and 1. Example: (3 matrixes 3x3) RED=[0 1 0;1 0 1;1 0 0.5]; GREEN=[0 0 1;1 0 0;1 1 0.5]; 1 BLUE=[0 0 0;0 1 1;1 1 0.5]; To see the contents of the matrixes, type their names and then ENTER. Notice that if you place the character “;” at the end of a line, the content of the variable is not shown on the screen. And it is very important to notice that Matlab is case sensitive. Concatenate the three matrixes to create a single three dimensional matrix (RGB) using the function “cat” (use the help to learn about the function). Example: RGB=cat( 3, RED, GREEN, BLUE); Visualize the content of the matrix RGB. The two first indices of the elements of the matrix define, respectively, their line and column numbers. The third index refers now to one of the three original matrixes. If we now consider matrix RGB as an image, the first two indices define the coordinates of a pixel, and the third, one of the RGB components of the pixel. Visualize the resulting matrix with the functions: imshow(RGB, ‘notruesize’) – for Matlab 6, or imshow(RGB, ‘Initial Magnification’, ‘fit’) – for Matlab 7. Indexed Image: Create a matrix with the name “map”, containing 3 lines and three columns, and values between 0 and 1, to represent a color map. Create a two dimensional matrix X, with integer values between 1 and 3. Visualize the indexed image using the functions: imshow(X, map, ‘notruesize’) – for Matlab 6, or imshow(X, map, ‘Initial Magnification’, ‘fit’) – for Matlab 7. 2 Gray Level Image: Create a matrix X with values between 0 and 1. Visualize the matrix as a gray level (intensity) image with the functions (where N is the number of gray levels to be shown): imshow(X, N, ‘notruesize’) – for Matlab 6, or imshow(X, gray(N), ‘Initial Magnification’, ‘fit’) – for Matlab 7. Binary Image: Repeat the previous exercise using exclusively the values 0 and 1 for the elements of X (binary image). 3. Reading and Writing Images: Image Reading: Study the function “imread” using Matlab’s help on-line. Create an image using the program Paint (Windows), and save the image in two different formats: 256 colors and 24 bits. Read both images using the function “imread”. Try to find out, with the aid of the function “whos” if the images are RGB or indexed images (to learn about the function type “help whos”). Visualize the images using the function “imshow”. Image Writing: Save one of the images created in the previous exercise with the “bmp” format using the function “imwrite”. 3 Open the saved image with the Paint program. Experiment: Read the image “forest.tif” and visualize it. What sort of image is it? 4. Image Conversion: With the aid of the help on-line, learn to use the functions “who”, “whos” and “clear”. Check out the variables in your workspace. Clear all the variables from your workspace. Learn to use the functions “ind2gray”, “gray2ind”, “rgb2ind”, “ind2rgb”, “rgb2gray” and “gray2rgb”. Notice the options “dither/nodither”, and the use of a preexisting color map, for the xxx2ind functions. Notice the double data type (not uint8) for the elements of the resulting matrixes, for the xxx2rgb functions. Transform the images created in the previous exercises into different image types and visualize the resulting images. 5. Visualization: Learn to use the function “figure”. Show two different images in different windows. Learn to use the functions “subplot” and “subimage”. Show the two images of the previous exercise in a single window using the functions. Learn to use the function “title”. Place titles on the windows of the previous exercises. 4 6. Matrixes’ Basics: Create the matrix A using the following command: A = [16 3 2 13 19; 5 10 11 8 3; 9 6 7 12 8; 4 15 14 1 13; 1 2 3 4 5] Observe what happens when the character “;” is placed at the end of the command. Calculate the sum of the four corner cells of the matrix, referencing the respective indices. 7. Check the output of the following commands: A A(1,3) + A(3,1) A(1:3,3) A(1:4,2:4) A(:,3) A(1:3,:) Create a 5x3 matrix B, and apply the command A(:,[3 5 2])=B(:,1:3) A(:) A’ A(:,[1 2 2 3 3 3 4 4 4 4]) A([1 2 2 3 3 3 4 4 4 4],:) A(A>10)=0 8. Concatenation: Create a 2x2 matrix A. Use the following commands and check their outputs: B=[A 2*A] B=[A A-1; 2*A A/2] 9. Study the commands “zero”, “ones” , “eye” e “size”. 5 10. The colon “:” operator: Try the following commands: X=[2.5:0.4:4.0] X=[2.5:4.5] 11. Arithmetic Operators: Type “help arith” and learn about the operators: “+”, “-”, “*”, “.*”, “/”, “./”, “^”, “.^”. Experiment with the commands “*” and “.*”, and with “/” and “./”. 12. Scripts and Functions (with the aid of the instructor). Create a script that calculates the length of edge c of a triangle, given the length of the other two edges a and b, and the angle between edges a and b. Use the formula: c2 = a2 + b2 – 2ab cos θ Suggestion: use the function “cos” and investigate Matlab’s debugging capabilities. Create a function from the script. Learn how to use the function “openvar”. Learn how to use “breakpoints”. 13. Flow Control: Learn to use the statements "if " , "for", "switch", "while" and "break" from Matlab’s help. 6 14. Function Graphs: Create a vector x containing 100 values between 0 and 2*pi. Create the vector y containing the sine of each value in x (use the function “sin”). Graph the sine function (use the “plot(x,y)” function). To alter the axis’ characteristics, read the help section for the function “axis”. Create a function to calculate the roots of the quadratic equation for arbitrary “a”, “b” and “c” coefficients and plot the equation. 15. Save and Load: Read the help section on the function “save”. Save in a file a (set of) variable(s) from your workspace. Clear your workspace (“clear all”), and confirm that it is really empty (“whos”). Read the help section on the function “load” and load the saved variable(s). Check your workspace. Proposed Exercises: 1. Create a function that calculates the distance between two pixels: The first two input parameters of the function are column vectors with two elements each (2x1) the first element corresponds to the x coordinate and the second, to the y coordinate of a pixel. The third parameter is a character that defines one of three possibilities: “e” for Euclidean distance; “m” for Manhatan distance; or “c” for chessboard distance. Create your own help for the function. 7 2. Performance Investigation: Create a function that calculates the logarithm of the elements of a square (NxN) matrix in two different ways. The input matrix should be initialized with random values (use the function “rand”) and the output of the function must be a matrix with the same size of the input. Initially implement the function using a loop that calculates at each iteration the log of a single element of the input matrix. Then implement the function using vector processing, that is, apply the function “log” to the entire input matrix. Modify the function to determine the execution time of each of the implementation methods (use the functions “clock and “etime”). Modify the function to evaluate the execution time for different values of N (use the functions “plot”, “hold”, “xlabel”, “ylabel”, “legend” and “title”). 8