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 amongst 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:

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.

(help mkdir)

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"

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";

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,

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.

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;

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)

Author (this page): Stefan Karlsson, Date: 2010-09-01