CSCI 363 Computational Vision--Spring 2023

    Lab 1

    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.

    Start MATLAB

    On your NUC, start up MATLAB by selecting the Matlab icon from the sidebar.

    The default MATLAB desktop contains three inner windows:

    • Command Window (middle), where MATLAB commands are entered and results printed
    • Current Folder (Directory) (left) lists the files in the current directory
    • Workspace (right) lists variables currently stored in the MATLAB workspace

    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.

    Enter some commands in the Command 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

    You can make the Y axis match that of the matrix by typing:

    axis ij

    You can also observe the variables listed in the Workspace window.

    Use the Command History to re-execute earlier commands

    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 arrow keys for command execution and editing

    Use the four arrow keys to do the following:

    • up-arrow key cycles backwards through previous commands
    • down-arrow key cycles forwards through previous commands
    • left-arrow key moves the cursor to the left through the current command line, allowing you to edit the command
    • right-arrow key moves the cursor to the right through the current command line, for editing

    Once a command is complete, it can be executed by hitting the carriage return, regardless of the position of the cursor in the line.

    Open the MATLAB editor

    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:

    • type edit in the Command Window
    • click on the New Script icon in the upper left corner of the main MATLAB window
    • select New>Script from the menu at the top of the main MATLAB window

    Create a script file of MATLAB code

    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:

    • Select Save from the File menu
    • Click on the floppy disk icon near the top menu bar

    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.

    Load and display an image

    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:

    • move the cursor around the image in the Image Tool window, and note the changing Pixel info: in the bottom left corner, which indicates the (X,Y) coordinates of the image location and the intensity value stored at this location (an integer in the range from 0 to 255)
    • zoom in to view part of the image in more detail - click on the "+" magnifying glass icon at the top of the Image Tool window, move the cursor to a central area of the image, and click again - note the changes in both windows
    • drag around the rectangle in the overview window to view different areas of the image at a higher resolution
    • zoom out by clicking on the "-" magnifying glass icon and then clicking on a central region of the image
    • use the Pixel Region tool to view the contents of the image matrix in a separate window, by clicking on the second icon (to the right of the arrow) at the top of the Image Tool window
      • use the scroll bars to move around the image in the Pixel Region Tool window
      • use the mouse to move around the image in the Image Tool window
      • zoom in or out using the icons at the top of the Pixel Region Tool window

    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

    Quit MATLAB

    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