% lecture 10
% (c) Moo K. Chung mkchung@wisc.edu


% Reading the Talairach template in the NIfTI file format.
% load_nii.m requires load_nii_hdr.m, xform_nii.m
nii = load_nii('talairach.nii');  
vol=nii.img;

% contains the total 1105 integer values 
% Total 1105 identified ROI are given in
% http://www.talairach.org/labels.txt

max(max(max(vol)))
min(min(min(vol)))

figure;imagesc(vol(:,:,55)); colorbar;


% This is the FA of template for our DTI tract data. 
nii = load_nii('mean_diffeomorphic_initial6_fa.nii');  
d=nii.img;
size(d)
figure;imagesc(d(:,:,30)); colorbar;
figure;imagesc(squeeze(d(:,60,:))); colorbar;


% Reading DTI in the NIfTI file format
nii = load_nii('405_spd_combined.nii');  
d=nii.img;
size(d)
d1=squeeze(d(:,:,:,:,1)); %d11
figure;imagesc(d1(:,:,30)); colorbar;

d1=squeeze(d(:,:,:,:,3)); %d13=d31
figure;imagesc(d1(:,:,30)); colorbar;

d1=squeeze(d(:,:,:,:,5)); %d23=d32
figure;imagesc(d1(:,:,30)); colorbar;


% ANTs toolbox demonstration. 
% TEMPLATE is automatically constructed. Read James Gee's paper. 
% Febuary 5, 2008

fname_template='template.hdr';
file_name=strcat(fname_template)

image_info = analyze75info(file_name)
template = analyze75read(image_info);
figure;imagesc(template(:,:,80));colormap('gray')

% subject 002
fname='0002_IR+acpc_corrected_final.hdr';
file_name=strcat(fname)

image_info = analyze75info(file_name)
subject002 = analyze75read(image_info);
figure;imagesc(subject002(:,:,80));colormap('gray')

% image intensities are rescaled
subject002=single(subject002);
subject002=subject002*max(max(max(template)))/max(max(max(subject002)));

figure
subplot(1,2,1);imagesc(template(:,:,125));%colormap('gray');colorbar
subplot(1,2,2);imagesc(subject002(:,:,125));%colormap('gray');colorbar

% visualizing displacement fields
i=1  % It will manipulate subject id 002 only. 

figure
fname='0002_IR+acpc_corrected_finalfullinversewarpxvec.hdr'
image_info = analyze75info(fname);
volx = analyze75read(fname);
subplot(1,3,1)
imagesc(volx(:,:,80));colorbar
set(gca, 'CLim', [-5, 5]);

fname='0002_IR+acpc_corrected_finalfullinversewarpyvec.hdr'
image_info = analyze75info(fname);
voly = analyze75read(fname);
subplot(1,3,2);
imagesc(voly(:,:,80));colorbar
set(gca, 'CLim', [-5, 5]);

fname='0002_IR+acpc_corrected_finalfullinversewarpzvec.hdr'
image_info = analyze75info(fname);
volz = analyze75read(fname);
subplot(1,3,3);
imagesc(volz(:,:,80));colorbar
set(gca, 'CLim', [-5, 5]);


%HW 21. Compute the Jacobian determinant. 

%HW 22. Determine if the Jacobian determinant of 
% a deformation is normal or lognormal in distribution. 
% Why is this an important problem in applications?
















