% Image Processing and Pattern Recognition
% (Bildverarbeitung und Mustererkennung, 710.081)
% 
% A (very) short introduction to MATLAB basics.
% 
% Check also these online resources:
% MATLAB documentation
%   http://www.mathworks.com/help/index.html
% MATLAB image processing toolbox documentation
%   http://www.mathworks.com/help/toolbox/images/ref/f3-23960.html
% Howto write fast MATLAB code
%   http://www.mathworks.com/matlabcentral/fileexchange/5685

% Close all open figures
close all;
% Clear all workspace variables
clear;


%% Scalar operations
a = 2;
b = 3;
c = 4;
% Omit the semicolon (;) at the end to display the result
(a + b)^c


%% Vector basics
% Use comma (,) or space to separate entries of a row-vector
a = [1, 2, 3, 4]    
a = [1  2  3  4]    
% Use semicolon to define a column-vector
b = [1; 2; 3; 4]    
% j:k = [j, j+1, ..., k-1, k]
a = 1:4
c = 5:0.1:6
% Use single quote (') to transpose
d = a'


%% Matrix basics
% Create a matrix ...
% ... explicitly
A = [1, 2, 3, 4 ; 5, 6, 7, 8]

% ... by stacking vectors
a = 1:4;
b = 5:8;
B = [a; b]

% ... using built-in functions
% 3x3 matrix of zeros
Z = zeros(3)
% 4x5 matrix of ones
O = ones(4,5)
% 2x2 identity matrix
I = eye(2)


%% Indexing
% 5x3 matrix of normally distributed random values
A = randn([5,3])

% Subscript (row,col) indexing (Note that MATLAB indices start at 1)
A(2,3)
% Linear indexing (Note that MATLAB is column-major, whereas OpenCV is
% row-major!)
A(2*5+2)
% Indexing blocks, rows, columns
A(1:2,2:3)
A(3,:)
A(2:end-1,2)
% Dynamic allocation via indexing - try to avoid it (and allocate in
% advance!)
A(10, 6) = 1


%% Matrix operations
A = [1, 2; 3, 4]
B = A'

% Matrix multiplication
A*B     
% Element-wise multiplication
A.*B
% A^-1*B, or inv(A)*B
A\B 
% Exponentiation A*A
A^2
% Exponentiation element-wise
A.^2   
% Delete the first column of A
A(:,1) = []

%% Vectorized operations
A = [1 2 3 4; 5 6 7 8]
% Minimum of each column
min(A)
% Maximum of all elements
max(A(:))        
max(max(A))
% Mean of all elements
mean(A(:))
% (Linear) indices of elements x, 4 < x < 7
find(A > 4 & A < 7)

% Count how many elements of A are less than 123 - using a 4000x8000
% uniformly distributed array of integers in the range [100, 1000]
A = randi([100 1000], [4000 5000]);
% Method 1 - Using loops
tic
count = 0;
for i = 1:size(A,1)
    for j = 1:size(A,2)
        if A(i,j) < 123
            count = count + 1;
        end
    end
end
count
toc

% Method 2 - Using vectorized operations
tic
sum(sum(A < 123))
toc


%% Image input/visualization

% Read image
peppers = imread('peppers.png');
size(peppers)

% Display
figure;
imshow(peppers);

% Convert to grayscale
peppers_gray = rgb2gray(peppers);
figure; 
imshow(peppers_gray);

% Resize
peppers_small = imresize(peppers, 0.5);
figure; 
imshow(peppers_small);

% Save
imwrite(peppers_gray,'demo.png');


%% Gaussian smoothing
kernel = fspecial('gaussian',17,4); % Create predefined 2-D filters
% Use '...' to continue a command at the next line
peppers_smooth = imfilter(peppers_gray, kernel, 'same', 'conv', ...
    'replicate');

figure;
% Display input image
subplot(1,3,1); 
imshow(peppers_gray); 
title('Input');

% Plot convolution kernel
subplot(1,3,2); 
imagesc(kernel); 
axis equal tight; 
title('Kernel');

% Display smoothed image
subplot(1,3,3); 
imshow(peppers_smooth); 
title('Smoothed');


%% Image derivatives (using the Sobel operator)
peppers_gray = im2double(peppers_gray);
dx = conv2(peppers_gray, [-1 0 1; -2 0 2; -1 0 1], 'same');
dy = conv2(peppers_gray, [-1 0 1; -2 0 2; -1 0 1]', 'same');

figure;
% Display input image
h1 = subplot(2,2,1); 
imshow(peppers_gray);
title('Input');

% Gradient dx
h2 = subplot(2,2,2); 
imagesc(dx); 
axis equal tight; 
title('dx');

% Gradient dy
h3 = subplot(2,2,3); 
imagesc(dy); 
axis equal tight; 
title('dy');

% Gradient magnitude
mag = sqrt(dx.^2 + dy.^2);
h4 = subplot(2,2,4);
imagesc(mag); 
colormap gray; 
axis equal tight;
title('Magnitude');

% Zooming one of the subplots should be propagated to the others too
linkaxes([h1, h2, h3, h4]);
colormap gray;


