% lecture 04
% Moo K. Chung mkchung@wisc.edu
% 
% Kernel Smoothing

% Read one image from the directory /CCdensity
dim2=95;dim3=68;
CC=load('CCautism01.txt');
CC=reshape(CC,dim2,dim3);
figure;imagesc(CC);colormap('bone');colorbar;

% smoothing a single image
figure;imagesc(CC);colormap('bone');colorbar;
smooth = gaussblur(CC,5)
figure;imagesc(smooth);colormap('bone');colorbar;


% almost identical code but simpler
K=inline('exp(-(x.^2+y.^2)/2/sigma^2)');
[dx,dy]=meshgrid([-2:2])

weight=K(sqrt(2),dx,dy)/sum(sum(K(sqrt(2),dx,dy)))
smooth2=conv2(CC,weight,'same');
figure;imagesc(smooth2);colormap('bone');colorbar;

% iterated kernel smoothing
weight=K(1,dx,dy)/sum(sum(K(1,dx,dy)))

smooth3=CC;
for i=1:2
  smooth3=conv2(smooth3, weight,'same');
end;
figure;imagesc(smooth3);colormap('bone');colorbar;


% heat kernel smoothing on cortical thickness

load autism_cortical_surface.mat

surf.vertices=squeeze(autism_outer(1,:,:))';
surf.faces=tri;
thickness=squeeze(L2norm(autism_outer(1,:,:) - autism_inner(1,:,:)));
figure_trimesh(surf,thickness)

smooth4=hk_smooth(thickness,surf,1,50);
figure_trimesh(surf,smooth4);

%HW7: Redo HW5 with at least three different 
%     bandwidths (suggestions: 5, 10, 15, 20mm) 
%     and discuss the effect of different bandwith 
%     on the final result. There are a couple of 
%     NeuroImage papers out there that discuss this important issue.

%HW8: Redo HW3 after heat kernel smoothing. Discuss your 
%     resuls with and without smoothing. Reading Chung et al., 
%     NeuroImage 2005 will help you understand the problem.



