Docsity

MATLAB Image Processing: Understanding Scalars, Vectors, Matrices, and Image Operations, Lab Reports of Electrical and Electronics Engineering

Toggle sidebar
bg1
Handout #3 ECE 178
Jan 10, 2006 W2006
Guidelines for Getting Started with Matlab
I. You should familiarize yourself with the Matlab environment by either
accessing the online documentation or the help/Matlab help menu within
Matlab. The online documentation at
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.html
does not require you to have Matlab on your computer (i.e. can be read at
home). Do not worry about remembering all the details because you will
learn them as the course progress. Below are some key items to which
you should give more attention.
1) Know how to define and perform computations on scalars, vectors,
and matrices (addition, multiplication, etc.).
2) Make sure you understand the difference between element-by-
element computations and matrix computations, i.e. the difference
between A.*B and A*B.
3) Understand how to use and write functions (look up functions in
help for more details).
4) Know simple plotting in 2D.
II. You should read over the information on the Matlab Image Processing
Toolbox at http://www.ece.ucsb.edu/~manj/ece178/matlabip.htm and the
online/Matlab documentation for the Image Processing Toolbox. Here are
some key items.
1) Understand the types of images: Indexed, Intensity, Binary, RGB.
2) Understand some basic image formats: jpg, bmp, tiff, etc.
3) Know how to load/read an image onto the workspace, and how to
save/write an image from the workspace.
4) Know how the image data is represented in the workspace.
5) Know how to convert from one image type/format to another.
6) Understand image coordinates.
7) Know how to display an image.
III. You should read and do some of the exercises from An introductory
tutorial on MATLAB in Image Processing (attached to this page). Note
the Common Pitfalls section.
pf3
pf4

Partial preview of the text

Download MATLAB Image Processing: Understanding Scalars, Vectors, Matrices, and Image Operations and more Lab Reports Electrical and Electronics Engineering in PDF only on Docsity!

Handout #3 ECE 178

Jan 10, 2006 W

Guidelines for Getting Started with Matlab

I. You should familiarize yourself with the Matlab environment by either

accessing the online documentation or the help/Matlab help menu within

Matlab. The online documentation at

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.html

does not require you to have Matlab on your computer (i.e. can be read at

home). Do not worry about remembering all the details because you will

learn them as the course progress. Below are some key items to which

you should give more attention.

1) Know how to define and perform computations on scalars, vectors,

and matrices (addition, multiplication, etc.).

2) Make sure you understand the difference between element-by-

element computations and matrix computations, i.e. the difference

between A.B and AB.

3) Understand how to use and write functions (look up functions in

help for more details).

4) Know simple plotting in 2D.

II. You should read over the information on the Matlab Image Processing

Toolbox at http://www.ece.ucsb.edu/~manj/ece178/matlabip.htm and the

online/Matlab documentation for the Image Processing Toolbox. Here are

some key items.

1) Understand the types of images: Indexed, Intensity, Binary, RGB.

2) Understand some basic image formats: jpg, bmp, tiff, etc.

3) Know how to load/read an image onto the workspace, and how to

save/write an image from the workspace.

4) Know how the image data is represented in the workspace.

5) Know how to convert from one image type/format to another.

6) Understand image coordinates.

7) Know how to display an image.

III. You should read and do some of the exercises from An introductory

tutorial on MATLAB in Image Processing (attached to this page). Note

the Common Pitfalls section.

An introductory tutorial on MATLAB in Image Processing ECE 178, W

I. GETTING STARTED

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.

II. BASIC MATRIX OPERATIONS IN

MATLAB

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?

V. COMMANDS THAT YOU WILL BE

USING LATER

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.

VI. COMMON PITFALLS

  • Be sure to use .* instead of * when you're multiplying two images together point-by-point. Otherwise, Matlab will do a matrix multiplication of the two, which will take forever and result in total nonsense.
  • Be sure not to forget the semicolon at the end of a command. Otherwise, you may sit for a while watching all the pixel values from the resulting image scroll by on the screen!
  • Do not use for loops unless absolutely necessarily. The use of for loops will make your programs take much longer.