EE 4780 Matlab tutorial

advertisement
EE 4780
Matlab tutorial
MATLAB



Review your matrix-vector knowledge
Matlab help files are helpful to learn it
Exercise:
f = [1 2; 3 4]
g = [1; 1]
g = [1 1]
g’
z = f * g’
n=0:10
plot(sin(n));
plot(n,sin(n)); title(‘Sinusoid’); xlabel(‘n’); ylabel(‘Sin(n)’);
n=0:0.1:10
plot(n,sin(n));
grid;
figure; subplot(2,1,1); plot(n,sin(n)); subplot(2,1,2); plot(n,cos(n));
Bahadir K. Gunturk
2
MATLAB

Some more built-ins
a = zeros(3,2)
b = ones(2,4)
c = rand(3,3) %Uniform distribution
help rand
help randn %Normal distribution
d1 = inv(c)
d2 = inv(rand(3,3))
d3 = d1+d2
d4 = d1-d2
d5 = d1*d2
d6 = d1.*d3
e = d6(:)
Bahadir K. Gunturk
3
MATLAB

Image processing in Matlab
x=imread(‘cameraman.tif’);
figure;
imshow(x);
[h,w]=size(x);
y=x(0:h/2,0:w/2);
imwrite(y,’man.tif’);
% To look for a keyword
lookfor resize
Bahadir K. Gunturk
x[0,1]
 x[0, 0]
 x[1, 0]
x[1,1]
x[n1 , n2 ]  


 x[ M  1, 0]
x[0, N  1] 
x[1, N  1] 


x[ M  1, N  1] MxN
4
MATLAB


M-file
Save the following as myresize1.m
function [y]=myresize1(x)
% This function downsamples an image by two
[h,w]=size(x);
for i=1:h/2,
for j=1:w/2,
y(i,j) = x(2*i,2*j);
end
end

Compare with myresize2.m
function [y]=myresize2(x)
% This function downsamples an image by two
[h,w]=size(x);
for i=0:h/2-1,
for j=0:w/2-1,
y(i+1,j+1) = x(2*i+1,2*j+1);
end
end
Bahadir K. Gunturk

Compare with myresize3.m
function [y]=myresize3(x)
% This function downsamples an image by two
y = x(1:2:end,1:2:end);

We can add inputs/outputs
function [y,height,width]=myresize4(x,factor)
% Inputs:
% x is the input image
% factor is the downsampling factor
% Outputs:
% y is the output image
% height and width are the size of the output image
y = x(1:factor:end,1:factor:end);
[height,width] = size(y);
5
Download