Högskolan i Halmstad
Halmstad University - For the Development of Organisations, Products and Quality of Life

Image Analysis Lab 0

This excercise is to get you all started with the environment we will use in the course: Matlab. It will be very basic, but it is important that you understand this environment for the coming excercises. It is also for us to get to know each other, and it is recommended that you spend time informing and helping each other. I will be here for any questions you may have, but first try to discuss it amongs each other and EXPERIMENT!

There is NO DEMAND ON REPORTING YOUR PROGRESS IN THIS LAB. There is no quiz or questions you will be required to answer. This is to help you get through the rest of the course. Other excercises will have questions for you, and perhaps require you to report.

After this lab you should be aquinted with Matlab to the extent that you understand:

  • The matlab GUI interface
  • The Matlab help
  • file handling and manipulation from the prompt
  • Matrix manipulation
  • making scripts and functions
  • elementary debugging/testing procedure
  • The notion of matlab "figures" and simple plotting and image display

First off, we will go through an introductory tutorial. For the future excercises in this course, I recommend this materialexternal link (for our purposes, skip sections 6, 7, 9, and 10).

There are a wealth of resources out there for introduction to Matlab. Mathworks keeps a good list of tutorialsexternal link. Also, you may explore the interactive help section of matlab.

When you are done with this, you should be able to solve the following tasks:

Task: create a function that display an image of noise, and add functionality.

  • Create directory (a folder) called "myFirstProject" and change it to the current directory

(help mkdir)

  • Make a function called "myFunc" in the new directory. This function should be in a file myFunc.m

the easiest way of doing this is typing:

edit myFunc.m

because myFunc.m does not exist, Matlab asks if you would like to create it, say yes, and then it opens up the new file which will be empty(just a plain text file).
for a working function, it needs to have its first line:

function myFunc(input)

When the function is called from the command prompt, by "myFunc(n)", for some integer n, then an image of size n by n should be displayed containing noise. Noise means that each pixel value should be random. For example, myFunc(20) will give a 20 by 20 pixel big image of noise.

help rand
help imagesc

try to
display as gray scale, type "colormap(gray)"
get x axis and y axis to be scaled correctly, type "axis image"
get the image to display without the numbering on the axis, type "axis off"

  • Modify the myFunc(input) to return the sum of all pixel values

To generate a return value for a function, you need to change its first line to say:

function sumOfIm= myFunc(input)

now, whatever value is held in a variable called "sumOfIm", at the end of the function execution, will be returned. Thus, typing "myFunc(10)" at the command prompt, should display a random image of 10 by 10 pixels, AND also echo att the command window the sum of all the pixel values.

help sum

hint: read the help carefully regarding the function sum. It sums the elements of the image column wise. Thus, if a 20 by 20 image is in variable "im", then sum(im) gives a 20 element long vector(each element is the sum of a column in the image). Thus, the sum of the entire image im is given by sum(sum(im)).

At the prompt, note the difference between typing:

"myFunc(10)"
and
"myFunc(10);"

The semi colon supresses the echoing of the returned value. You can also type

"valueReturned = myFunc(10);"

and store the value in the new variable "valueReturned";

  • Modify the myFunc(input) to return the sum of all pixel values AND the maximum pixel value.

To generate several return values for a function, you need to change its first line to say:

function [sumOfIm, maxOfIm]= myFunc(input)

as before, you just have to make sure there are two variables at the end of the function execution called sumOfIm and maxOfIm.

help max

FAQs,

  • What is the function "linspace" mentioned in the pdf. What is about, what does it do, why is named the way it is, and how is it different from the matlab colon notation "a:b:c".

The function linspace(a,b,c) (real scalars a b c) generates a vector, similar to the matlab command a:d:b (for real scalars a b d).

If you type "a:d:b" in matlab, you will get:
a vector from value a, until value b.

If you type  linspace(a,b,c) in matlab, you will get:
a vector from value a, until value b.

The difference is between the two methods is in how "d" and "c" are used. With linspace you can specify how long the vector should be. With colon notation, you specify how big every increment should be.

Its called linspace, because you get a linear increase with each element. There are other functions that generates vectors of this type (like logspace) that gives a vector that is not with equal increments.

  • How do i make my shiny new matlab function return a value? I am used to C/C++/Java programming, where I type "return Value;" at the end of my function. That doesnt work here.

Here is some C code:

int myFunc(int a, int b)
{
    printf("\nhello world! The user sent me the values:");
    printf(" %i, %i", a, b);
    printf("\nI am gonna be real clever-like and sum them together and hand them back.")

    return a+b;
}

Here is the equivalent Matlab code

function WhatEverName = myFunc(a, b)
    disp('hello world! The user sent me the values:');
    fprintf(" %i, %i", a, b);
    disp('I am gonna be real clever-like and sum them together and hand them back.')

    WhatEverName = a+b;

  • Ok, I made a function that has multiple return values, how do I use it?

if your function looks like this:

function [a, b] = func(in)

then at the prompt you may type (for example):

[variable1, variable2] = func(300);

which creates 2 variables, and stores the output in them. If you type:

variable1 = func(300);

then you will only get one of the outputs of the function (output b, which was stored in variable2 above, will be discarded)

Page editor: Stefan Karlsson
Page last updated 2010-09-01
Halmstad University  |  Box 823  |  S-301 18  Halmstad, Sweden  |  Tel: +46 35-16 71 00  |  registrator@hh.se |  Visiting address: Kristian IV:s väg 3