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! :)
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.
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.
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:
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.
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]
In the first part of today’s assignment, you will start with some simple matrix manimulation tasks.
AVOID THE USE OF ANY LOOPS IN YOUR PROGRAM!
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.
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 1Hint: Search documentation with lookfor command.
A
and return it in output.A_3rd_col
.>> output.A_3rd_col output.A_3rd_col = 3 10 6 15Hint: You need to use matrix indexing with colon
:
character.
output.A_slice
. >> output.A_slice output.A_slice = 7 6 12 14 15 1
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 1Hint: Try
>
operator on the whole matrix. Use []
to construct the new matrix and ones function to create a vector with ones.
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 508Hint: No loops are needed, try it on a paper with a 2×2 matrix.
>> output.A_weighted_col_sum output.A_weighted_col_sum = 391Hint: You will need colon : special character to generate ranges, per element array multiplication
.*
and sum()
function.
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 -1Hint: Use
repmat
command to replicate a 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 -5Hint: 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!
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
.
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).
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.
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.
x = sum of pixel values in the left half of image - sum of pixel values in the right half of imageThen 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).
>> compute_lr_histogram('A', Alphabet, images, labels, 10) ans = 1 1 3 6 12 27 24 20 5 1
hist
function to compute the histogram.
initials_histograms.png
.
This is a simple task that demonstrates working with homogeneous planar points and lines.
[1, 1]
to [800, 600]
. Draw its boundary.
H = [1 0.1 0; 0.1 1 0; 0.004 0.002 1 ];
Example result of this task is shown in figure 1.
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
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:
dbquit
You can also start debugging automatically in case of error by turning on
dbstop error
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' ); else load( 'my_points.mat' ); end
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
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
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
u1 = [4;5;1]; u2 = [7;8;1]; dot12 = u1' * u2; % apostrophe is the transpose
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
numbers = [1 2 sqrt(-3) acos(2) 4 6+3i 7 ]; is_real = imag( numbers ) == 0; % logical array real_only = numbers( is_real );
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
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.
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.