This page is located in archive.

MATLAB preliminaries

To fulfill this assignment, you need to submit these files (all packed in one .zip file):

  • answers.txt - answers to the Assignment Questions
  • assignment_01.m - a script for data initialization, calling of the implemented functions and plotting of their results (for your convenience, will not be checked)
  • matrix_manip.m - a function implementing the matrix manipulation tasks specified in the section Matrix manipulation
  • compute_letter_mean.m and compute_lr_histogram.m - functions specified in the section Simple data task
  • initial1_mean.png, initial2_mean.png and initials_histograms.png - images specified in the section Simple data task

Use the template of the assignment. When preparing a zip file for the upload system, do not include any directories, the files have to be in the zip file root.

Beware of using for loops! :)

MATLAB installation

You will be provided with a virtual machine that has MATLAB R2016b installed. To complete this lab before the course starts on your own laptop, it is possible to use a MATLAB trial version OR to install Octave, which is a free alternative to MATLAB.

MATLAB resolution problem

If you have a high-resolution display (like 3000×1800 on 13.3“), the MATLAB toolbar strip could be too small for comfortably working with it. Unfortunately, there is no workaround for Linux yet except to lower your screen resolution, say, to 1920×1080 or similar.

MATLAB introduction

We will be using MATLAB programming language during this course. It is necessary for you to be as familiar with it as possible, since the labs are time limited and the tasks are often challenging. It may happen that you spend more time looking for MATLAB syntax of some function instead of solving the problem itself.

For the case you are not too sure about your MATLAB skills, here are few useful links:

Assignment Questions

In most of the assignments, there will be a set of simple questions. Answering the questions should help you to solve the assignment. All questions need to be answered correctly to get the points for the assignment.

Fill the correct answers to your answers.txt file.

  1. MATLAB function name to generate matrix of zeros (without parentheses)
  2. Symbol(s) used in MATLAB for matrix product
  3. Symbol(s) used in MATLAB for element-wise product
  4. MATLAB way to generate vector [0 1 2 3 4 5 … 100] (check all correct answers):
    • a) seq(0,100)
    • b) xrange(100)
    • c) 0:100
    • d) <0,100>
    • e) linspace(0,100,101)
  5. Select the second to last (předposlední) row matrix A (check all correct answers):
    • a) A(:,-1)
    • b) A(-1,:)
    • c) A(:,end-1)
    • d) A(end-1,:)
    • e) A[:,end-1]
    • f) A[end-1,:]

Example answers.txt (bogus answers only, fill the correct ones):

question1 : gen_zero_matrix
question2 : 'x*' # enclose special characters in tick marks, also known as single quotation marks.
question3 : '^'  # enclose special characters in tick marks, also known as single quotation marks.
question4 : [a,d,e]
question5 : [b,f]

Matrix manipulation in MATLAB

In the first part of today’s assignment, you will start with some simple matrix manimulation tasks.


Your goal is to complete a function output = matrix_manip(A, B), where A and B are input matrices and output is a resulting structure containing results of operations described below.

To have some data to work with, use matrices A and B from assignment_01.m:

A = [16     2     3    13;
      5    11    10     8;
      9     7     6    12;
      4    14    15     1]
B = [ 3     4     9     4     3     6     6     2     3     4;
      9     2    10     1     4     3     7     1     3     5]

Function should work on general input matrices, not only for above prescribed A and B matrices or matrices with the same dimensions. Dimensions of the input matrices will be always suitable for all of the following tasks.

  1. Find the transpose of the matrix A and return it in output.A_transpose. Example result:
    >> output.A_transpose
    output.A_transpose =
       16     5     9     4
        2    11     7    14
        3    10     6    15
       13     8    12     1
    Hint: Search documentation with lookfor command.
  2. Select the third column of the matrix A and return it in output.A_3rd_col.
    >> output.A_3rd_col
    output.A_3rd_col =
    Hint: You need to use matrix indexing with colon : character.
  3. Select last two rows from last three columns of the matrix A and return the matrix in output.A_slice.
    >> output.A_slice
    output.A_slice =
          7   6  12
         14  15   1
  4. Find all positions in A greater then 3 and increment them by 1. Afterwards add column of ones to the matrix. Save the result to output.A_gr_inc.
    >> output.A_gr_inc
    output.A_gr_inc =
         17     2     3    14     1
          6    12    11     9     1
         10     8     7    13     1
          5    15    16     1     1
    Hint: Try > operator on the whole matrix. Use [] to construct the new matrix and ones function to create a vector with ones.
  5. Create matrix C such that $C_{i,j} = \sum_{k=1}^n A\_gr\_inc_{i,k} \cdot A\_gr\_inc_{k,j}^T$ and store it in output.C.
    >> output.C
    output.C = 
        499   286   390   178
        286   383   351   396
        390   351   383   296
        178   396   296   508
    Hint: No loops are needed, try it on a paper with a 2×2 matrix.
  6. Compute $\sum_{c=1}^n c \cdot \sum_{r=1}^m A\_gr\_inc_{r,c}$:
    >> output.A_weighted_col_sum
    output.A_weighted_col_sum =
    Hint: You will need colon : special character to generate ranges, per element array multiplication .* and sum() function.
  7. Subtract a vector $(4,6)^T$ from all columns of matrix B. Save the result to matrix output.D.
    >> output.D
    output.D = 
         -1     0     5     0    -1     2     2    -2    -1     0
          3    -4     4    -5    -2    -3     1    -5    -3    -1
    Hint: Use repmat command to replicate a matrix .
  8. Select all column vectors in the matrix D, which have greater euclidean norm than the average euclidean norm.
    >> output.D_select
    output.D_select =
            0     5     0    -2
           -4     4    -5    -5
    Hint: You can find useful element-wise power of two .^2 and find function that returns indices of non zero elements of a matrix. Euclidean length of a vector can be calculated with the norm function, but for calculating length of all column vectors in a matrix you need to do it with your own code. No loops are needed!

Simple data task in MATLAB

In this part of the assignment, you are supposed to work with a simple input data which contains images of letters. We will use similar data structures later on during the labs. Do the following:

Note: Plotting and displaying of images should be implemented only in assignment_01.m.

  1. In the data_33rpz_cv01.mat data file in the template are stored the following variables:
    • images (3D array of 2000 10×10 grayscale images)
    • Alphabet (letters in the images, not full alphabet is included)
    • labels (indexes of the images into Alphabet array).
  2. Inspect all images at once with the code:
    ims = permute(images,[1 2 4 3]); % The images must be ordered by the 4th dimension.
    montage(ims); colormap gray;
    Hint: check documentation for functions imshow, imagesc, subplot, montage.
  3. Compute the grayscale pixel value mean images of your name initials and save them as initial1_mean.png and initial2_mean.png. You will probably encounter a problem with non-decimal numbers in the image matrix. You can avoid it by using the uint8 function before returning the mean image.
    • For the purpose of mean image calculation, complete the function:
      letter_mean = compute_letter_mean(letter_char, Alphabet, images, labels)
      where letter_char is a character (e.g. 'A', 'B', 'C') representing the letter whose mean we want to compute, Alphabet, images and labels are loaded from the provided data, and letter_mean is the resulting mean image.
  4. Compute an image feature x - a sole number characterizing an image. It is defined as:
    x = sum of pixel values in the left half of image - sum of pixel values in the right half of image
    Then make a histogram of feature values of all images of a letter. Complete a function for the feature histogram computation:
    lr_histogram = compute_lr_histogram(letter_char, Alphabet, images, labels, num_bins)
    where letter_char is a character representing the letter whose feature histogram we want to compute, Alphabet, images and labels are loaded from the provided data, num_bins is the number of histogram bins and lr_histogram is the resulting histogram (num_bins long vector containing counts of items in the corresponding bins).
    • For reference the following histogram was computed for letter A with 10 bins:
      >> compute_lr_histogram('A', Alphabet, images, labels, 10)
      ans =
           1     1     3     6    12    27    24    20     5     1
    • Hint: use hist function to compute the histogram.
    • Plot feature histograms of your initials into one figure to compare them and save the figure as initials_histograms.png.

Optional Task 1: Points and Lines in a Plane

This is a simple task that demonstrates working with homogeneous planar points and lines.

  1. Let the image area has an extent [1, 1] to [800, 600]. Draw its boundary.
  2. Develop a simple tool, allows to enter two pairs of points within this area and displays them.
  3. Calculate the straight line passing through the first pair and the straight line passing through the second pair. Use homogeneous representation. Display the intersection of each line with the image area.
  4. Calculate the intersection of both lines and draw it, if it is inside the image area.
  5. Apply following homography to all entities and draw the result to another figure.
H = [1     0.1   0;
     0.1   1     0;
     0.004 0.002 1 ];

Example result of this task is shown in figure 1.

Fig. 1: Example intersection

Notes: the Matlab function ginput is suitable for entering the points.

[u v] = ginput( 4 );
% or
x = ginput(4)';  % note that ginput returns row vectors, hence the transpose

Additional MATLAB Tips


Matlab has a powerful debugger. Either put breakpoints everywhere you want, or use the command keyboard in your code to stop processing at the place. Exit the debugging mode:


You can also start debugging automatically in case of error by turning on

dbstop error

Saving intermediate data

Especially when some user input is needed, it is wise to store intermediate data for later reuse. Following example could be helpful

if( ~exist( 'my_points.mat', 'file' ) )
  [x y] = ginput( 7 );
  save( 'my_points.mat', 'x', 'y' );
  load( 'my_points.mat' );

Vectors and its transformations

Vectors are stored as column matrices.

u = [1;2];
X = [1;2;3];
Xmore = [1 2 3; 4 5 6]';  % transpose at the end

Vectors are multiplied by a matrices on the left:

P = [1 0 0 -5; 0 1 0 -6; 0 0 1 1];  % a camera
ux_homog = P * [X;1]; % projection

Special matrices: Identity, Zeros, Ones

I = eye(3); % 3x3 identity
I2 = diag( [1 1 1] ); % same result
Z = zeros(3); % 3x3 matrix of zeroes
O = ones(3); % 3x3 matrix of ones

Euclidean distance of two 3D points

X1 = [1;2;3];
X2 = [4;5;6];
d2 = norm(X1-X2); % cannot be used for more vectors
d2 = sqrt(sum((X1-X2).^2)); % for matrices with points stored in columns

Dot (scalar) product

u1 = [4;5;1];
u2 = [7;8;1];
dot12 = u1' * u2; % apostrophe is the transpose

Element-by-element matrix operations

In addition to standard matrix addition and subtraction, there are some operators working element-by-element.

M1 = [1 2; 3 4; 5 6];
M2 = [7 8; 9 10; 11 12]; % note the sizes
A = M1 .* M2; % Hadamard product (multiplication element-by-element; note the dot)
B = M1 ./ M2; % Hadamard division (division element-by-element)
C = M1 .^ 2;  % power element-by-element

Selection of the real part of complex numbers

numbers = [1 2 sqrt(-3) acos(2) 4 6+3i 7 ];
is_real = imag( numbers ) == 0; % logical array
real_only = numbers( is_real );

Cross product

u1 = [4;5;1];
u2 = [7;8;1];
u12x = cross(u1,u2);
% verification of orthogonality by dot product 
% (remember the recommendations 1 and 2)
u12x' * u1 % should be zero
u12x' * u2 % should be zero

Angle given x and y coordinate.

Let x = r cos(a) and y = r sin(a). Then the angle can be recovered as

a = atan2( y, x );

This function is safe when either of its arguments is zero (but not both), and works for four quadrants.

Capturing a bitmap snapshot of a figure/axis

Let hnd be some axes handle (e.g. gca) or some figure handle (e.g. gcf). The snapshot identical to the (pixel-wise) appearance of the screen is captured as

f = getframe( hnd );
img = f.cdata;

Notes: when capturing a sequence, do not forget to call drawnow before capturing. Also be careful not to obscure the captured window by other GUI content, mouse pointer, etc.

courses/ucuws17/labs/01_intro/start.txt · Last modified: 2017/01/15 14:54 by mishkdmy