% Matlab examples for cs484
% Richard Mann, Oct 10, 2011, Revised Jan 28, 2013.

%%% Start image Processing, Lecture #7.

%Im=double(imread('microserf.tif','tiff'));
Im=double(imread('einstein.pgm','pgm'));
figure; colormap gray; imagesc(Im); axis('square'); title('original');
[M,N]=size(Im);
k=waitforbuttonpress;

%%% Blurring DEMO
close all;
figure; colormap gray; imagesc(Im); axis equal; title('original');
SigmaMax=8;
xx=floor(-3*SigmaMax):ceil(3*SigmaMax);
rr=repmat(xx,length(xx),1).^2+repmat(xx',1,length(xx)).^2;

Sigma=8.0;
Filt=1/(2*pi)/(Sigma^2)*exp(-0.5*rr/(Sigma*Sigma)); z=size(Filt); z=z(1);
ImFilt=conv2(Im,Filt,'same');
figure; colormap gray; imagesc(ImFilt);
title(sprintf('Sigma=%g\n',Sigma)); axis square;
figure; colormap gray; II=zeros(N); II(1:z,1:z)=Filt; imagesc(II); axis square;
k=waitforbuttonpress;

Sigma=4.0;
Filt=1/(2*pi)/(Sigma^2)*exp(-0.5/(Sigma*Sigma)*rr); z=size(Filt); z=z(1);
ImFilt=conv2(Im,Filt,'same');
figure; colormap gray; imagesc(ImFilt);
title(sprintf('Sigma=%g\n',Sigma)); axis square;
figure; colormap gray; II=zeros(N); II(1:z,1:z)=Filt; imagesc(II); axis square;
k=waitforbuttonpress;

Sigma=2.0;
Filt=1/(2*pi)/(Sigma^2)*exp(-0.5/(Sigma*Sigma)*rr); z=size(Filt); z=z(1);
ImFilt=conv2(Im,Filt,'same');
figure; colormap gray; imagesc(ImFilt);
title(sprintf('Sigma=%g\n',Sigma)); axis square;
figure; colormap gray; II=zeros(N); II(1:z,1:z)=Filt; imagesc(II); axis square;
k=waitforbuttonpress;

Sigma=1.0;
Filt=1/(2*pi)/(Sigma^2)*exp(-0.5/(Sigma*Sigma)*rr); z=size(Filt); z=z(1);
ImFilt=conv2(Im,Filt,'same');
figure; colormap gray; imagesc(ImFilt);
title(sprintf('Sigma=%g\n',Sigma)); axis square;
figure; colormap gray; II=zeros(N); II(1:z,1:z)=Filt; imagesc(II); axis square;
k=waitforbuttonpress;

%%% Highpass demo, just one value;
close all;
Sigma=2.0;
Filt=1/(2*pi)/(Sigma^2)*exp(-0.5/(Sigma*Sigma)*rr); z=size(Filt); z=z(1);
ImFilt=conv2(Im,Filt,'same');
figure; colormap gray; imagesc(ImFilt);
title(sprintf('Sigma=%g\n',Sigma)); axis square;
figure; colormap gray; imagesc(Im-ImFilt); axis square; title('highpass');

%%% Laplacian DEMO
close all;
figure; colormap gray; imagesc(Im); axis equal; title('original');

Sigma=8.0;
lapFilt=-(rr/(Sigma.^4)-2/(Sigma.^2)).*exp(-0.5/Sigma/Sigma*rr);
%figure; mesh(lapFilt); title(sprintf('Sigma=%g\n',Sigma));
ImLap=conv2(Im,lapFilt,'same');
figure; colormap gray; imagesc(ImLap); title(sprintf('Sigma=%g\n',Sigma)); axis square;
k=waitforbuttonpress;

Sigma=4.0;
lapFilt=-(rr/(Sigma.^4)-2/(Sigma.^2)).*exp(-0.5/Sigma/Sigma*rr);
%figure; mesh(lapFilt); title(sprintf('Sigma=%g\n',Sigma));
ImLap=conv2(Im,lapFilt,'same');
figure; colormap gray; imagesc(ImLap); title(sprintf('Sigma=%g\n',Sigma)); axis square;
k=waitforbuttonpress;

Sigma=2.0;
lapFilt=-(rr/(Sigma.^4)-2/(Sigma.^2)).*exp(-0.5/Sigma/Sigma*rr);
%figure; mesh(lapFilt); title(sprintf('Sigma=%g\n',Sigma));
ImLap=conv2(Im,lapFilt,'same');
figure; colormap gray; imagesc(ImLap); title(sprintf('Sigma=%g\n',Sigma)); axis square;
k=waitforbuttonpress;