Lab 1

horizontal rule

ece480 | doering | ece labs | ece | rhit

Home
Schedule
Course Information
Policies
Homework
Labs
Resources

MATLAB and Images

horizontal rule

 

Introduction

MATLAB is a powerful environment for processing and visualizing numerical data. In this lab you will have a chance to become acquainted with MATLAB as a tool for working with image data sets.

Objectives

bulletLearn how to start MATLAB
bulletSet up a "startup" file to simplify access to course resources
bulletLearn how to visualize matrices as images
bulletPractice with image measurement tools
bulletLearn how to make hardcopy of your results

Deliverables

Before you leave today, submit hardcopy of your subimage and row/column plots. Please include names of all who worked with you, and include a campus mail address. A write-up is not necessary for this lab.

horizontal rule

Running MATLAB

There are at least four ways to access MATLAB on at Rose-Hulman:

bulletOn your own network-connected computer
bulletInstall MATLAB from TIBIA (be sure to install Signal Processing Toolbox and Image Processing Toolbox)
bulletMust be connected to network in order for MATLAB to operate
bulletOn a network-connected computer
bulletInstall "Terminal Server" client from TIBIA
bulletRun the client and connect to computer "ts2"
bulletMap your AFS directory via SAMBA (right-click on the "My Computer" icon, and select "Map Network Drive"; enter \\SAMBA\yourAFSname)
bulletRun MATLAB
bulletOn the SGI computers in the Imaging Systems Laboratory (these are presently at version 5.3, other techniques are version 6)
bulletOn your own stand-alone computer
bulletPurchase MATLAB Student Edition and Image Processing Toolbox

MATLAB Startup File

A startup.m file is useful for automatically setting up the MATLAB search path, for changing the current working directory to something other than the default, and for any other activity you would like to perform each time MATLAB starts up. You can place any valid MATLAB code inside your startup.m file. Common activities include:

  1. Append your own path to the system search path: path(path,’C:\mypath\matlab’);

  2. Change working directory: cd C:\mydir

  3. Display a message to confirm that startup.m file commands were processed (put a line such as "disp('it works!')" in your file)

A sample startup.m file is available. Please edit it to suit your needs.

Recommended methods for using a startup file:

bulletWith locally-installed MATLAB: Place startup.m file in the "work" subdirectory of the MATLAB installation directory
bulletVia Terminal Server method:
bulletPlace startup.m file in your AFS directory
bulletEach time you start MATLAB, enter "cd" followed by the mapped drive letter for your AFS directory (probably F:), then type "startup" to run the file.
bulletOn Imaging Lab SGI computers:
bulletLog in using your Imaging Systems lab account
bulletOpen a (non-console) terminal window by selecting "Desktop -> Unix Shell"
bulletOne Time Only: Copy the class startup.m file to your home directory: type the command "cd" to guarantee that your working directory is the same as your home directory, then type the command "cp ~doering/startup.m ." (the period is part of the command)
bulletStart MATLAB by typing "matlab" at the prompt

MATLAB Overview

bulletOnce you have started MATLAB, type "demo".
bulletFeel free to look around at will so that you can develop some idea of the total scope of MATLAB capabilities.
bulletAlso look at the Image Processing Toolbox demos

MATLAB and and Small Images

The following activities are designed to help you learn how to work with image data using a small data set. You can get help on all MATLAB functions and commands by typing "help" or "helpwin" followed by the function name. "help" displays results in the MATLAB command window, while "helpwin" displays the results in a new window.

What to do How to do it Comments
Enter image data into the variable "a"
a = [1 3 5 2; 21 7 9 12;...
9 8 40 6]
The ellipsis "..." is how you continue a command on a new line of input
Display the data values you just entered (as a matrix)
a
 
Visualize the matrix as an image
image(a)
 
Add a colorbar
colorbar
 
Explain what happens when you do this ->
colormap(gray)
 
Try this ->
figure
 
Now try this ->
imagesc(a); colorbar
colormap(gray)
Note that you can combine multiple commands on a single line
Talk it over: Can you explain the difference between image and imagesc?
 
 
Display the pixel value at row 1, second pixel from the left
a(1,2)
 
Display all the pixel values in row 1
a(1,:)
 
Extract the second row (a vector) and place it in a new variable
r = a(2,:)
 
Plot the row vector and add axis labels and a title See "help" for these commands:
plot
xlabel, ylabel, title
 
Extract the third column vector from the image "a", assign it to the variable "p", and plot the column vector in a new figure window Try to do this yourself based on what you have learned so far  
Extract a subimage from "a"
b = a(1:2,3:4)
Make sure you understand what is happening here!
Set the third pixel in the third row of image "a" to zero
a(3,3) = 0
 
Set the first row of pixels in the image "a" to 30 Try to do this yourself  

MATLAB and "Real" Images

What to do How to do it Comments
Load a PNG (pronounced "ping") image
[x map] = imread('boats.png')
PNG = Portable Network Graphics format
If the image data numerical values are still spewing out, type Ctrl-C to interrupt.

Now, repeat the previous command and add a semicolon to suppress the output

Use the up arrow to retrieve the previous command, attach the semicolon, then resubmit the command Note that the arrow keys allow you to scroll through previous commands to save typing effort.

You can also type one or more letters first, then the arrow keys retrieve only the commands that begin with those letters.

Determine the size of the variables returned by "imread"
whos x
whos map
"who" displays the variable names.

"whos" displays the names with their sizes.

Display the image data as a grayscale image. Add a colorbar, too. You know how to do this now (see above, if needed)  
Create a new figure window, and repeat using imagesc    
Create yet another figure window, and try these new image display commands
imshow(x,map); colorbar
imshow(x,[]); colorbar
The "imshow" method is preferred since it deals more effectively with both grayscale and color image files.
Try some of the built-in tools in the figure window to zoom in and out, and to add text and axis labels    
Make sure that you have the profiles.m file in your MATLAB search path.
which profiles
If "profiles' is not currently in your search path, then do a shift+click on the link at left, and save it to a directory that is in your search path.
Experiment with the "profiles" function.

Talk it over: Can you explain what you see?

Use "helpwin profiles" to learn how to use it  
While still using "profiles", find the coordinates (row number and pixel number) of the very top of the lighthouse.    
Extract the row vector and column vector (also called row and column "slices") that intersect the coordinates that you found in the previous step.    
Make a plot of the row vector. Scale the vertical axis to the range 0 to 255.

Repeat for the column vector.

Look up the "axis" command You may find it necessary to enter "figure(n)" to set the current figure before you enter the "axis" command. You can find the figure number in the title strip at the top of the figure window.

Side note: If you ever need to delete a figure, just do "delete(n)"

Deliverable: Make hardcopy of your plots Either type "print" in the MATLAB command window, or select "File | Print" in the figure window.  
Find and record the minimum and maximum brightness values in the image
bmin = min(x(:))
bmax = max(x(:))
"x(:)" converts the 2-D matrix into a 1-D vector. You typically have to do this before using other functions that are designed to work with matrices.

For example, can you explain what happens when you enter "min(x)"?

Find the coordinates of the brightest pixel(s):
[r,p] = find(x==bmax)
Make sure you know what is happening here... maybe look at the help page for "find"
Extract a 128x128 subimage centered about the first coordinate your found in the last step. You can do it!  
Deliverable: Display your subimage, put a title on it, and print it    
When you are finished, close down MATLAB
exit
Please shut down MATLAB when you finished to free up the license for another user. Thanks!

 

Congratulations, you're almost done!

Please submit the deliverables mentioned at the top of the page before you leave the lab.

horizontal rule

Home | Schedule | Course Information | Policies | Homework | Labs | Resources

 ECE480/PH437: Introduction to Image Processing (W 2001-02)
Department of Electrical and Computer Engineering
Rose-Hulman Institute of Technology


For questions or comments regarding this web contact:
Last updated: 03/10/05.