Home | | Schedule | | Assignments | | Lecture Notes
The purpose of this lab is to learn how to work in the MATLAB environment, and how to read and display images in MATLAB.
On your NUC, start up MATLAB by selecting the Matlab icon from the sidebar.
The default MATLAB desktop contains three inner windows:
Like other windows-based applications, you can change the size of the inner windows by
dragging their borders, close the inner windows, and use scroll bars to navigate up and
down the window contents. If your MATLAB windows change their appearance in an undesirable
way, you can restore the original setup by selecting Layout>Default
from the menu at the top of the main window.
One useful window not included in the default layout is the Command History
window. You can include this window by selecting Layout>Command History>Docked
from the menu at the top of the main window.
MATLAB is interactive - the user enters commands in the Command Window, MATLAB executes
these commands, and the results are returned and printed. Enter some commands in the Command
Window (the %
symbol precedes comments that you do not need to enter):
num = 10 % create a variable "num" with the value 10
values = [1 2 3 4 5] % create a 1D array (vector) of 5 numbers
num % print the value of an existing variable
format compact % omit vertical spaces in future printout
a = 2
clc % clear the Command Window
b = 20.0; % a semi-colon at the end of a statement
% suppresses printout of the value
whos % list the variables currently defined
% in the MATLAB workspace
c = (2 * 3) + num % perform simple calculations
num = sqrt(c^3 + b^2)
val2 = [3 8 -4; -1 0 2] % create a 2x3 matrix of numbers
% note the semi-colon in the middle!
size(val2) % get the dimensions of the matrix
Now type the following statements to generate a simple image:
A = 5*ones(5, 5)
B = 10*ones(3, 4)
Image = zeros(10, 10)
Image(2:6, 4:8) = A
Image(5:7, 6:9) = B
colormap(gray)
pcolor(Image)
Following the pcolor command, a window should pop up with a grayscale image of 2 rectangles in it. Note that the Y coordinate is inverted from the matrix form and there are lines between each box. You can get rid of the lines by typing:
shading flat
axis ij
You can also observe the variables listed in the Workspace window.
Execute a previous command again by double-clicking on the command in the Command History window, or by copying-and-pasting the command into the Command Window and hitting the carriage return.
Use the four arrow keys to do the following:
Once a command is complete, it can be executed by hitting the carriage return, regardless of the position of the cursor in the line.
MATLAB has a built-in editor for creating and viewing code files and text files. There are at least three ways to open an initial editor window:
edit
in the Command Window
New>Script
from the menu at the
top of the main MATLAB window
MATLAB code files are referred to as M-Files and have a .m
file extension.
In this lab, we'll create a type of M-file called a script, which contains
a sequence of MATLAB commands that can be executed all at once. Executing a script produces the
same results that would be obtained if the individual commands were entered, one-by-one, in the
Command Window.
First create a directory named csci363
in your home directory on
radius. Inside the csci363
directory, create a directory named lab1
where you will save the code file you create.
In MATLAB, make this
directory your Current Folder
or Current Directory
. You can do this
by clicking on folders in the current folder window.
In the editor, type the following script:
Image = ones(3, 4) for i = 1:3 for j = 1:4 Image(i, j) = i+j; %Don't forget the semicolon here! end end Image %This will print out the final matrix
Save the script in one of the following two ways:
Save
from the File menu
A dialog box will appear where you can enter the name of the script file - enter the
full file name (e.g. lab1.m
) in the text box labeled with Save As:
.
The box labeled Where:
should contain your lab1
folder.
Click on the Save
button, and your file will be stored in your
lab1
folder - check its listing in the Current Folder (Directory) window. Now
execute the script in the Command Window by entering the first file
name lab1
, and note the printout of the variable names and values.
Enter whos
to see that
your script variables now exist in the MATLAB workspace.
Edit the script (for example, change the computation in the for loop), save the modified file, and execute it again in the Command Window. As a shortcut, you can save the file by clicking on the floppy disk icon at the top of the editor window. You can also run your script by clicking on the green arrow icon at the top of the editor window.
An existing M-File can be opened in the MATLAB editor by (1) selecting Open...
from
the File menu and navigating to the desired code file, (2) double-clicking on the file
name in the Current Directory window, or (3) invoking the edit
command in the Command
Window:
edit lab1
The file (lab1.m
in this case) must be stored in the
Current Directory, or exist somewhere on MATLAB's search path.
Use the imread
function to load an image into MATLAB, and give it a name:
coins = imread('coins.png');
% note the semi-colon!
(The coins.png
image comes with MATLAB's Image Processing Toolbox.) The image is
now stored in a
2D matrix named coins
- you can check its size by entering the whos
command or checking the list of variables in the Workspace
window. There are two
ways to display an image. Use imshow
for quick viewing:
imshow(coins)
The image will appear in a separate figure window. Closer examination of the image
is possible with imtool
:
imtool(coins)
Two windows will appear, an Image Tool window and a smaller Overview
window (if the Overview window does not appear automatically, select Overview
from the Tools menu). Try the following:
(X,Y)
coordinates of the image location and the intensity value stored at this
location (an integer in the range from 0 to 255)
Examine the changes in image intensity between adjacent image locations, around the borders of the coins and along the edges of the raised figures on the surface of the coins. Also note that even in regions that appear to have constant intensity, such as the dark background, the intensities do not have a constant value. Our first step in analyzing images will be to detect and describe the changes in image intensity - this information provides the first hint about the structure of the scene being viewed.
Close all of the open windows by executing the following two commands in the Command Window:
close all
imtool close all
When you are done, make sure to quit out of MATLAB.
Home | | Schedule | | Assignments | | Lecture Notes
Constance Royden--croyden@holycross.edu
Computer Science 363--Computational Vision
Last Modified: January 29, 2023
Page Expires: January 29, 2024