


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



An introductory tutorial on MATLAB in Image Processing ECE 178, W
MATLAB is a data-analysis and visualization tool widely used by electrical engineers and stands for “Mathematics Laboratory.” The most important difference between MATLAB and C (or C++) is that functions in MATLAB are specifically written with a focus on large array- based operations in mind. Image processing is heavy on the memory usage as well as the run- time of programs.
You would need the Image Processing toolbox installed in your MATLAB to work with the commands that are listed later in this worksheet. You can check if the Image Processing toolbox is installed using the ver command. Using “help images” one can see all the commands that are supported in this toolbox. If you find the help document scrolling past very fast you can switch the ‘more’ command using >> more on or for a more formal help use >> doc ‘command’ A MATLAB function is a keyword that accepts various parameters, and produces output of some sort. At times in this course, we may need to write our own functions (and this is very easy as you will see). A command is a particular usage of a function. Variables are elements used to store values. If you are using a windowed version of MATLAB, you may find a Workspace item in the View menu. This lists all your currently defined variables, their data types, sizes etc.
Exercise 1: Explore the whos and who commands that are used to list the variables used in the workspace.
At this point, I would like to stress that learning/ understanding MATLAB is impossible by just reading this handout. The exercises in this handout are motivated primarily by this reason. Using standard MATLAB routines “efficiently” Could mean the difference between a program that runs for a day and one that runs in an hour. Use of for loops is known to increase the run- time by a significant amount.
Exercise 2: This exercise describes how ‘slow’ the for command can be. We can measure the time that elapses between the executions of a command using the tic toc timer of MATLAB. tic starts a stopwatch timer and toc stops it and prints out the elapsed time in seconds. Execute
the following code and compare the elapsed time differences between the two.
tic; for i = 1:10ˆ6, sin(i);end; toc; tic; i=1:10ˆ6; sin(i); toc; Understand how the for loop can slow down the process by quite a bit.
The standard data type of MATLAB operations is the matrix. Images, of course, are matrices whose elements are the gray values (or possibly the RGB values) of its pixels. I am assuming that most of you are aware of the basics of MATLAB. More information on these commands can be obtained using the help and lookfor commands. A standard 2 × 3 matrix
is defined as >> A = [a11 a12 a13; a21 a a23;]; Matrix elements can be obtained using the standard row, column indexing scheme >> a(2,3) The reshape function produces a matrix with elements taken column by column from the given matrix. MATLAB also allows matrix elements to be obtained using a single number; this number being the position when the matrix is written as a single column. For e.g. a matrix with r rows and c columns, element a ( i, j ) corresponds to a ( i + r · ( j - 1)). We would rarely be using this notation. Addition of matrices is done as >> c = a+b; The inv , det and ’ commands do the inverse, determinant and transpose operations respectively.
Exercise 3: Matrices can be flipped up or down using flipud , left or right using fliplr and rotated by 90 degrees by rot90. Explore them.
To obtain a row of values, or a block of values, we use the colon (:) operator. Pointwise multiplication (Hadamard product of two matrices) is done with >> c = a.*b; We also have dot division and dot powers. The command >> a.ˆ2 produces a matrix, each element of which is the square of corresponding elements of a.
Exercise 4: Using the colon and dot operators alone, generate the first 15 cubes.
Sometimes, we would need to generate some special matrices and random matrices. An
image as a matrix I. Be sure to put the ‘;’ or you will be staring at a set of numbers running endlessly on the screen. If you store the image as
I = imread(‘filename.fileextension’); you let MATLAB decide by what it sees, the file format etc. Indexed images can be identified using the imfinfo command. For indexed images, one also has to read the colormap as follows. [I,cmap]=imread(‘filename.fileextension’); After you have done the necessary image processing, storing an image back in the jpg (or similar) formats is done with the ‘imwrite’ command. The standard procedure is to imwrite(I,‘filename.fileextension’); Check for corresponding commands with indexed images. You may sometimes want to save the image matrix so that you can work with it later. This is done with the load and save commands (which are not image processing toolbox specific). save I saves the image matrix I in the file ‘I.mat’ and load I loads the saved contents to the variable ‘I’. There are three different file formats in MATLAB, the m files, the mat files and the mex files. Mat files are stored files and you would not be dealing with the mex files in this course. You can check the variables in your workspace with the whos command and list all the files with the ls command. Displaying an image can be done with the imshow or imagesc commands. We only describe the syntax for indexed images (For rest, check the help files.). >> imshow(I,cmap); The images are displayed in the Figure window.
Exercise 8: Pick a gray-scale image, say cameraman.tif or any other file that you can get hold of, and using the imwrite function write it to files of types JPEG, BMG and GIF. What are the sizes of those files?
The following commands maybe useful later in this course.
- pixval on/off : You can see the values of the pixels using this command in the figure window. - impixel(·, ·, ·) : You can obtain the values of the pixels using this command. - brighten(·) : This command brightens an image if the parameter inside is positive. Check the help files for syntax. - isgray(·) : Answers if the image is a gray-scaled image. - imcrop(·) : Produces a copy of the image which can then be cropped. Should store the image then in a different file for post-processing. - image(·) : Displays image in a Figure window. - imfinfo(·) : Returns information about the image file stored (in the jpg/gif/other formats). - plot(·,·,·) : Plots the vectors against each other. Sizes should match. - zoom on/off : Zooms into or out of the figure. - colormap : You can get to see the colormap associated with an indexed figure using this command. Consult the help file for syntax. - fft2(·) : This does the 2-D Fourier transform of the image as a matrix. - dct2(·) : The DCT-II of the input matrix is done with this command. - conv2(A,B) : This does the 2-D convolution of A by B. Note that the resultant convolved matrix is of increased size. More information can be obtained using the MATLAB help files or a standard MATLAB book or consulting the Image processing document pages in the Mathworks site.