Matlab Commands Used in Class
An older file of commands used in Spring 2013 can be found here (you can use it in order to prepare for classes or see different sets of commands)
Classes of Jan. 20th and 22nd, 2015
load clown
whos
image(X)
image(X); colormap(gray)
imshow(X)
help imshow
imshow(X,[])
% imshow is naturally designed for color images (we'll mention it later)
imagesc(X)
help colormap
% noticed that default was the jet colormmap and checked what it was as well as other color maps
Y=zeros(200,320);
Y(1:100,1:150)=X(1:100,1:150);
image(Y); colormap(gray)
Y=X;
Y=Y(1:2:end,1:2:end);
image(Y); colormap(gray)
Y=Y(1:2:end,1:2:end);
image(Y); colormap(gray)
norm(X-Y)
load mandrill
whos
clear
I=imread('board.tif');
whos
image(I); colormap(gray)
image(I)
image(I); colormap(jet)
% of course colormap is not in use
image(2*I); colormap(jet)
image(10*I); colormap(jet)
imagesc(10*I); colormap(jet)
% note operations with uint8
Y=double(I);
imagesc(10*I)
image(10*Y)
help image
% Note: "For matrices containing doubles, color
% intensities are on the range [0.0, 1.0]. For uint8 and uint16
% matrices, color intensities are on the range [0, 255]."
max(max(max(Y)))
min(min(min(Y)))
image(Y/255)
I=rgb2gray(I);
whos
imagesc(10*double(I)); colormap(gray)
imagesc(double(I)); colormap(gray)
figure(1); imagesc(double(I)); colormap(gray)
figure(2); imagesc(double(10*I)); colormap(gray)
figure(2); imagesc(10*double(I)); colormap(gray)
figure(3); image(1e12*double(I)); colormap(gray)
clear
load clown
whos
Xnew=X(end:-1:1,:);
figure; imagesc(X_new); colormap(gray)
figure; imagesc(Xnew); colormap(gray)
Ynew=Xnew(end:-1:1,:);
figure; imagesc(Xnew); colormap(gray)
figure; imagesc(Ynew); colormap(gray)
help meshgrid
whos
[Xx,Xy]=meshgrid(1:200,1:320);
Xz=double(X);
mesh(Xx,Xy,Xz)
help meshgrid
% we need a square
T=zeros(320,320);
T(1:200,:)=X;
X=T;
[Xx,Xy]=meshgrid(1:320,1:320);
Xz=double(X);
mesh(Xx,Xy,Xz)
Class of February 5
load mandrill
whos
[U,S,V]=svd(X,0);
s=diag(S);
plot(s)
A=U(:,1:20)*S(1:20,1:20)*(V(:,1:20))';
imagesc(A); colormap(gray)
figure; imagesc(X); colormap(gray)
[m,n]=size(X)
m*n
20*(m+n)
%compression ratio:
20*(m+n)/(m*n)
%relative error
s(21)/s(1)
Class of February 12
N=1000;
D=10;
A=zeros(N,D);
t=(1:1000)'/1000;
for i=1:10
A(:,i)=t.^i;
end
A(501:N,3)=t(501,N);
A(501:N,3)=t(501:N);
c=mean(A);
help mean
size(c)
A=A-ones(N,1)*c;
[U,S,V]=svd(A,0);
size(V)
Apv=A*V;
plot3(Apv(:,1),Apv(:,2),Apv(:,3),'.')
hold on; plot3(Apv(1:500,1),Apv(1:500,2),Apv(1:500,3),'r.')
figure; plot(Apv(:,1),Apv(:,2),'.')
hold on; plot(Apv(1:500,1),Apv(1:500,2),'r.')
[x,y]=getpts
% we formed a parabola with noise by clicking on points on screen, once we clicked "Enter" we got back to Matlab screen
size(x)
size(y)
plot(x,y,'*')
A=[x.^2,x,ones(30,1)];
b=y;
z=regress(b,A);
hold on
plot(x,A*z,'r') % there is an issue since x axis values are not sorted (one can either sort them or use the option below)
figure; plot(x,y,'*'); hold on
plot(x,A*z,'r*')
clear all
A=imread('tire.tif');
imagesc(A);colormap(gray)
image(A);colormap(gray)
imshow(A,[,]); colormap(gray)
Alog=log(A);
A=double(A);
Alog=log(A);
figure(2); imshow(Alog,[,]); colormap(gray)
figure(2); imagesc(Alog); colormap(gray)
Aexp=exp(A);
figure(3);imagesc(Aexp); colormap(gray)
min(Aexp(:))
max(Aexp(:))
find(A==0)
find(Alog==-Inf)
Alog=log(A+1);
figure(4);imagesc(Alog); colormap(gray)
Class of February 17
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
load mandrill
whos
figure; imagesc(X); coloarmap(gray)
Y=im2patch(X,2,[3,3],[1,1]);
size(X)
size(Y)
(480-2)*(500-2)
Y=Y';
c=mean(Y,1);
Y=Y-ones(238044,1)*c;
[U,S,V]=svd(Y,0);
plot(diag(S),'.')
proj2V=V(:,1:2);
Yproj2=Y*proj2V;
scatter(Yproj2(:,1),Yproj2(:,2))
V(:,1)
c
% we'll continue with this example the following class
Class of February 19th
% Continuation of previous class
%copying and pasting commands from previous class:
mex im2colstep.c
mex col2imstep.c
help im2patch
load mandrill
whos
figure; imagesc(X); colormap(gray)
Y=im2patch(X,2,[3,3],[1,1]);
size(X)
size(Y)
(480-2)*(500-2)
Y=Y';
c=mean(Y,1);
Y=Y-ones(238044,1)*c;
[U,S,V]=svd(Y,0);
figure; plot(diag(S),'.')
proj2V=V(:,1:2);
Yproj2=Y*proj2V;
figure; scatter(Yproj2(:,1),Yproj2(:,2))
V(:,1)
c
%New commands:
V(:,2)
close all
clear all
X=imread('cameraman.tif');
whos
Y=im2patch(X,2,[3,3],[1,1]);
% probablem since X is uint8
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);
dYproj2=Y*proj2V;
scatter(Yproj2(:,1),Yproj2(:,2))
figure; imagesc(X); colormap(gray)
figure; plot(diag(S),'.')
V(:,1)
c
sz
Ynew=zeros(sz);
Ynew(:,1:2)=Yproj2;
imout=patch2im(X,Ynew',2,[3,3],[1,1]);
figure; imagesc(imout); colormap(gray)
figure; imagesc(-imout); colormap(gray)
proj3V=V(:,1:3);
Yproj3=Y*proj3V;
scatter3(Yproj3(:,1),Yproj3(:,2),Yproj3(:,3))
%Histograms
clear all
close all
A=imread('pout.tif');
imshow(A)
imshow(A,[])
image(A)
imagesc(A)
imhist(A)
a=A(:);
hist(a)
a=double(a);
hist(a)
hist(a,255)
figure(2); imhist(A)
figure(2); ylim([0,4000])
B=histeq(A);
dfigure; imagesc(B); colormap(gray)
figure; imhist(B)
% demonstration of a problem of imhist with double images and its correction (not done in class due to lack of time)
load clown
imhist(X)
X=X/max(max(X));
imhist(X)
imhist(X,12)
imhist(X,map)
colormap(gray)
Class of February 24th
You may download the demos written by Alex Gutierrez for discrete 1D convolution and 2D convolution
Back to Gilad Lerman's webpage
Back to current 5467 webpage