Laboratory of Computer and Information Science / Neural Networks Research Centre CIS Lab Helsinki University of Technology

T-61.5100 Digitaalinen kuvankäsittely / Digital Image Processing

Harjoitustyö / Course assignment

Getting started with Matlab

Starting the program

Log in to one of HUT's unix workstations in the normal manner. Then, to start the program, give the commands:

use matlab

If you are accessing the computer over a slow Internet connection (from some other X11-capable workstation) or you just don't like Matlab's rather slow and cumbersome graphical interface you can also start is as:

use matlab
matlab -nojvm -nosplash

Then only the images will be displayed in separate windows, the commands will be given directly in the terminal.

Basics of Matlab

Commands can be given at the ">>" prompt or can be written into functions. Each command is limited to one line unless three consecutive periods are given which signifies the continuation of a line. End the command with a semicolon ";" unless you want the result of the expression to be printed out.

A list of some useful Matlab commands can be found here:

To get started, experiment with simple expressions first and then try more advanced commands.

It is often useful to write commands into functions. A function is a text file (with the filename extension ".m") which as a first line contains something like

function x=fname(var1, var2)

Of course the arguments and the returned variable can have any names, and 'fname' is the name of the function (should be in file 'fname.m'). The rest of the file contains standard Matlab commands which eventually give the returning variable its value. You may then call the function from the prompt (or some other function) e.g. like this

>> y = fname(7, 3)

ans = 21


Loading an image into Matlab

Use the Image Processing Toolbox 'imread'. Help on this function can be found by typing 'help imread'. The function accepts most standard image file formats.

>> I=imread('mypic.tif','tif');
>> I=double(I);

Note the second row, where we convert to image to floating point (double precision) format! This is very important if you are going to do any arithmetic operations, since the original 8-bit integer format cannot for example express large or negative numbers. This can also be done in one line only:

>> I=double(imread('mypic.tif','tif'));

Displaying an image

Having read the image into the matrix 'X', you can display it by

>> figure; colormap(gray(256)); image(X);

There is another command called 'imagesc', which adjusts the scaling such that the smallest element of X becomes black and the largest white.

Printing an image

This can be done directly from the image figure, using the menu commands. Be sure to direct it to the correct printer. Another way is of course to save it to a file (using 'imwrite') first.

An example program

This sample program reads in a picture of the HUT main building (tkk_gray.tif) and performs the following operations.

  1. The original image.
  2. Added Gaussian noise.
  3. Improved contrast.
  4. Blurred by a 5x5 mask.
  5. Thresholded to black/white.
  6. Emphasized the center.

You are at: CIST-61.5100 → Matlab Guide

Page maintained by t615100 at, last updated Tuesday, 23-Oct-2007 12:21:23 EEST