Parte1-Introduzione A Matlab
Parte1-Introduzione A Matlab
Parte1-Introduzione A Matlab
MatLab
Alternatives Complements
Matrix-X Maple (symbolic)
Octave(free; GNU) Mathematica (symbolic)
Lyme (free; Palm)
MATLAB
» a=5;
» b=a/2
b=
2.5000
»
MATLAB Variable Names
- (unary) + (unary)
Addition + a + b
Subtraction - a - b
Assignment = a = b (assign b to a)
Other MATLAB symbols
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
MATLAB Help System
z Scalars are matrices with only one row AND one column
MATLAB Matrices
» a_value=23
a_value =
23
MATLAB Matrices
rowvec =
12 14 63
MATLAB Matrices
colvec =
13
45
-2
MATLAB Matrices
» matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
1 2 3
4 5 6
7 8 9
Extracting a Sub-Matrix
sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;
» matrix=[1,2,3;4,5,6;7,8,9] » col_two=matrix( : , 2)
matrix = col_two =
1 2 3 2
4 5 6 5
7 8 9 8
MATLAB Matrices
matrix = » rowvec=matrix(2 : 2 , 1 : 3)
1 2 3 rowvec =
4 5 6
7 8 9 4 5 6
Reading Data from files
z MATLAB will plot one vector vs. another. The first one will
be treated as the abscissa (or x) vector and the second as
the ordinate (or y) vector. The vectors have to be the same
length.
z MATLAB will also plot a vector vs. its own index. The index
will be treated as the abscissa vector. Given a vector “time”
and a vector “dist” we could say:
>> plot (time, dist) %plotting versus time
>> plot (dist) %plotting versus index
Plotting with MATLAB
if expression1 % is true
% execute these commands
elseif expression2 % is true
% execute these commands
else % the default
% execute these commands
end
MATLAB Repetition Structures
» a=3;
» b=[1, 2, 3;4, 5, 6]
b=
1 2 3
4 5 6
» c= b+a % Add a to each element of b
c=
4 5 6
7 8 9
Scalar - Matrix Subtraction
» a=3;
» b=[1, 2, 3;4, 5, 6]
b=
1 2 3
4 5 6
» c = b - a %Subtract a from each element of b
c=
-2 -1 0
1 2 3
Scalar - Matrix Multiplication
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
» c = a * b % Multiply each element of b by a
c=
3 6 9
12 15 18
Scalar - Matrix Division
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
»c=b/a % Divide each element of b by a
c=
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
MATLAB Toolbox
200
>> cropped=f(60:300, 60:420,:);
150
>> subplot(2,2,3);
100 >> imshow(cropped);
50 >> subplot(2,2,4);
0
0 100 200 300 400 >> plot(f(:,10,1));
Tips and Tricks
z Code Optimization
– Preallocating Arrays: zeros(M,N)
– Vectorizing Loops (1D):
z Code Optimization
– Vectorizing Loops (2D):
function [ratio_time,f,g]=twodsinnew(A,u0,v0,M,N)
tic %Start timing
for r=1:M
u0_x=u0*(r-1);
for c=1:N
v0_y=v0*(c-1);
f(r,c)=A*sin(u0_x + v0_y);
end;
end;
t1=toc; %End Timing
Example 1
tic
r=0:M-1;
c=0:N-1;
[C,R]=meshgrid(c,r);
g=A*sin(u0*R+v0*C);
t2=toc; %End timing
B Y Cr
G
Cb
R
Example
RGB vs YCbCr
RGB conversion &
subsampling
RGB → CMY
⎡ C ⎤ ⎡1⎤ ⎡ R ⎤
⎢ M ⎥ = ⎢1⎥ − ⎢ G ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢⎣ Y ⎥⎦ ⎢⎣1⎥⎦ ⎢⎣ B ⎥⎦
RGB → YCbCr
⎡ Y ⎤ ⎡0.299 0.587 0.114 ⎤ ⎡ R ⎤
⎢C ⎥ = ⎢0.596 − 0.275 − 0.321⎥ ⎢G⎥
⎢ b⎥ ⎢ ⎥⎢ ⎥
⎢⎣Cr ⎥⎦ ⎢⎣0.212 − 0.523 0.311 ⎥⎦ ⎢⎣ B ⎥⎦
Subsampling
4:4:4 (no subsampling)
4:2:2 (Cb, Cr horizontal subsampling)
4:2:0 (Cb, Cr horizontal + vertical subsampling)
Tutorials on Web
www.imageprocessingplace.com/DIPUM/dipum_tutorials/tutorials.htm