Matlab Commands Used in Class

Below is a summary of matlab commands used in class.

Class of Jan. 22nd, 2013

load mandrill
whos
image(X)
colormap(gray)
colormap(map)
load clown
whos
image(X)
colormap(map)
colormap(gray)
Y=X(1:2:end,1:2:end);
size(X)
size(Y)
image(Y)
Z=X(:,end:-1,1);
image(Z); colormap(gray)
figure(2); image(X); colormap(gray)
Z2=X(end:-1:1,:);
figure(3); image(Z2); colormap(gray)
A=imread('cameraman.tif');
whos
image(A)
colormap(gray)
B=imread('board.tif');
whos
image(B)
B1=B(:,:,1);
image(B1); colormap(gray)
Bgray=rgb2gray(B);
image(Bgray); colormap(gray)
help imread
help uint

 

Class of Jan. 29th, 2013

A=imread('autumn.tif');
A=imread('kids.tif');
[U,S,V]=svd(A,0);
A=double(A);
[U,S,V]=svd(A,0);
plot(diag(S))
ranK(A)
rank(A)
U40=U(:,1:40);
V40=V(:,1:40);
s=diag(S);
s40=s(1:40);
S40=diag(s40);
A40=U40*S40*V40';
figure(1); imagesc(A);colormap(gray)
figure(2); imagesc(A40);colormap(gray)
plot(s)
U4=U(:,1:4);
V4=V(:,1:4);
s4=s(1:4);
S4=diag(s4);
A4=U4*S4*V4';
figure(3); imagesc(A4);colormap(gray)

 

Class of Jan. 31st, 2013

A=imread('kids.tif);
whos
A=double(A);
image(A)


colormap(gray)
B=zeros(400,400);
B(:,1:318)=A;
image(B)
help meshgrid
[X,Y]=meshgrid(1:400,1:400);
Z=double(B);
help mesh
meshc(X,Y,Z)
whos A
C=A(:,1:200);
b=A(:,300);
whos
help regress
x=regress(b,C);
norm(Cx-b)
norm(C*x-b)
norm(C*x-b)/norm(x)
norm(C*x-b)/norm(b)

Class of Feb. 5th, 2013

For some reason the commands were not saved on my computer (unfortunately), but i used the following similar demo in a previous class (though somewhat different curves with different geometry of projection)

% projection of 10-dim data on top 3 principal components
clear; close all
N=1000;
A=zeros(N,10);
t = (1:1000)'/1000;
for i=1:10
A(:,i) = t.^i;
end
A(501:N,3)=t(501:N);
c=mean(A);
A=A-ones(N,1)*c;
[U,S,V]=svd(A);
An = A*V;
plot3(An(:,1),An(:,2),An(:,3),'.')
hold on; plot3(An(1:500,1),An(1:500,2),An(1:500,3),'r.')
figure; plot(An(:,1),An(:,2),'.')
hold on; plot(An(1:500,1),An(1:500,2),'r.')

Class of Feb. 7th, 2013

We use the files in the following zipped folder to convert image to patches and patches to images

mex im2colstep.c
mex col2imstep.c
help im2patch % example for bad documontation of code...
load mandrill
whos
imagesc(X); colormap(gray)
Y=im2patch(X,2,[3,3],[1,1]);
size(X)
size(Y)
478*498 % confirmation of size(Y)
Y=Y';
c=mean(Y,1);
size(c)
Y=Y-ones(238044,1)*c;
[U,S,V]=svd(Y,0);
plot(diag(S),'.')
(norm(Y,'fro')^2-S(1,1)^2)/(norm(Y,'fro')^2)
proj2V=V(:,1:2);
Yproj2=Y*proj2V;
scatter(Yproj2(:,1),Yproj2(:,2))
proj3V=V(:,1:3);
Yproj3=Y*proj3V;
scatter3(Yproj3(:,1),Yproj3(:,2),Yproj3(:,3))
close all
imagesc(X); colormap(gray)
Ynew=zeros(238044,9);
Ynew(:,1:3)=Yproj3;
imout = patch2im(X,Ynew',2,[3,3],[1,1]);
imagesc(imout); colormap(gray)
clear all
X=imread('cameraman.tif')
Y=im2patch(X,2,[3,3],[1,1]);
whos X
X=double(X);
Y=im2patch(X,2,[3,3],[1,1]);
Y=Y'
c=mean(Y,1);
sz=size(Y);
Y=Y-ones(sz(1),1)*c;
[U,S,V]=svd(Y,0);
proj2V=V(:,1:2);
Yproj2=Y*proj2V;
scatter(Yproj2(:,1),Yproj2(:,2))
imagesc(X); colormap(gray)
Ynew=zeros(sz(1),9);
Ynew(:,1:2)=Yproj2;
imout = patch2im(X,Ynew',2,[3,3],[1,1]);
imagesc(imout); colormap(gray)
imout=-imout;
imagesc(imout); colormap(gray)


Class of Feb. 12th, 2013

A=imread('tire.tif');
A=double(A);
imagesc(A); colormap(gray)
Alog=log(A);
figure(2)
imagesc(Alog); colormap(gray)
Ailog=exp(A);
figure(3)
imagesc(Ailog); colormap(gray)
min(min(A))
max(max(A))
% we used log, where there any zeros?
find(A==0)
find(Alog==-Inf)
% imagesc seems to know to scale -inf as the next integer lower value
% will compare the image with log(A+1)
Alog=log(A+1);
figure(4)
imagesc(Alog); colormap(gray)
% Histograms
A=imread('pout.tif');
imshow(A)
imhist(A)
a=reshape(A,size(A,1)*size(A,2),1);
% we could have use instead a=A(:);
hist(a)
a=double(a);
hist(a)
hist(a,256)
figure(2); imhist(A)
figure(2); imhist(A,256) % no difference as 256 is default value here
figure(2); ylim([0,4000])
% demonstration of a problem of imhist with double images and its correction
load clown
imhist(X)
X=X/max(max(X));
imhist(X)
imhist(X,12)
load clown
imhist(X,map)
colormap(gray)
A=imread('pout.tif');
A=imread('pout.tif');
B=histeq(A);
imagesc(B)
close all
imhist(B)

Class of Feb. 14th, 2013

help corr
help xcorr
%checking an example worked up on the board
f=[1,-1]
g=[2,3]
xcorr(f,g)
conv(f,g)
help conv2
help xcorr2

Class of Feb. 19th, 2013
(added few corrections)

A=rand(100);
B=ones(3)
B=B/9
C=xcorr2(A,B);
% mentioned filter2 command, but will not be used
C2=filter2(A,B);
size(C2)
C2=filter2(B,A);
size(C2)
% imfilter and hspecial will be commonly used
help imfilter
h=fspecial('average')
h=fspecial('average',[5,5])
B=imread('tire.tif');
whos B
C=imfilter(B,h);
imshow(B)
figure(2); imshow(C)
h=fspecial('average');
C=imfilter(B,h);
figure(2); imshow(C)
h=fspecial('disk');
h
imshow(h)
imagesc(h); colormap(gray)
h=fspecial('gaussian',10,1);
imagesc(h)
ih=fspecial('gaussian',10,4);
imagesc(h)
C=imfilter(B,h);
imshow(h)
imshow(C)
h=fspecial('gaussian',10,1);
C=imfilter(B,h);
imshow(C)
h=fspecial('prewitt');
% we work with negative filters, so it is better to change B to be double
B = double(B);
C=imfilter(B,h);
imshow(C) % should not use imshow
figure(2); imagesc(B); colormap(gray)
figure(1); imagesc(C); colormap(gray)
h=h';
C=imfilter(B,h);
figure(1); imagesc(C)
% looking for a better image to distinguish horizonal vs. vertical changes
B=imread('cameraman.tif');
B = double(B);
hy=fspecial('prewitt')
hx=hy'
Cx=imfilter(B,hx);
Cy=imfilter(B,hy);
figure(1); imagesc(B); colormap(gray)
figure(2); imagesc(Cx); colormap(gray)
figure(3); imagesc(Cy); colormap(gray)
figure(4); imagesc(abs(Cx)); colormap(gray)
figure(5); imagesc(abs(Cy)); colormap(gray)
C=abs(Cx)+abs(Cy)
figure(6); imagesc(C); colormap(gray)
C=sqrt(Cx.^2+Cy.^2);
figure(7); imagesc(C); colormap(gray)
hxx=ones(3,3);
hxx(2,:)=-2*ones(1,3);
hxx
hyy=hxx';
Cxx=imfilter(B,hxx);
Cyy=imfilter(B,hyy);
figure(8); imagesc(Cxx); colormap(gray)
figure(9); imagesc(Cyy); colormap(gray)
figure(10); imagesc(abs(Cxx)); colormap(gray)
figure(11); imagesc(abs(Cyy)); colormap(gray)

Class of Feb. 21st, 2013

h=fspecial('prewitt')
h=fspecial('sobel')
h=fspecial('laplacian', .7);
B=imread('cameraman.tif');
C7=imfilter(B,h);
imagesc(C7); colormap(gray)
h=fspecial('laplacian',.3);
C3=imfilter(B,h);
figure(2)
imagesc(C3); colormap(gray)
h=fspecial('log');
C=imfilter(B,h);
figure; imagesc(C); colormap(gray)
h=fspecial('log',10,2);
C=imfilter(B,h);
figure; imagesc(C); colormap(gray)
h=fspecial('log',10);
C=imfilter(B,h);
figure; imagesc(C); colormap(gray)
h=fspecial('gaussian');
Cavg=imfilter(B,h);
figure; imagesc(Cavg); colormap(gray)
Cs=B-Cavg;
figure; imagesc(Cs); colormap(gray)
h=fspecial('average')
Cavg=imfilter(B,h);
Cs=B-Cavg;
figure; imagesc(Cs); colormap(gray)
figure; imagesc(-Cs); colormap(gray)
h=fspecial('unsharp')
C=imfilter(B,h);
figure; imagesc(C); colormap(gray)
h=fspecial('laplacian')
C2=imfilter(B,h);
figure; imagesc(C2); colormap(gray)
C3=B+C;
figure; imagesc(C3); colormap(gray)

Class of Mar. 3rd, 2013

x=zeros(1,256);
x(1)=1;
help fft
plot(x,'.')
y=fft(x);
plot(y,'.')
x=zeros(1,256);
x(2)=1;
y=fft(x);
plot(y,'.')
plot(abs(y),'.')
plot(real(y),'.')
plot(imag(y),'.')
plot(phase(y),'.')
x=zeros(1,256);
x(3)=1;
y=fft(x);
plot(real(y),'.')
plot(phase(y),'.')
z=phase(y);
z{1:5)
z(1:5)
x=zeros(1,256);
x(129)=1;
y=fft(x);
plot(real(y),'.')
y(1:5)
plot(phase(y),'.')
x=zeros(1,256);
x(90:140)=1;
plot(x,'.')
y=fft(x);
plot(abs(y),'.')
help fftshift
u=fftshift(y);
plot(abs(u),'.')
plot(real(u),'.')
plot(imag(u),'.')


Back to Gilad Lerman's webpage
Back to current 5467 webpage


Last Modified
The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.