clearvars
clc

%change microscope name for new scopes!
microscope_name = 'birch';
today = date;

fname='Max_fluorescent.tif';

%%
im=imread(fname,1);
for n=2:4
    im(:,:,n)=imread(fname,n);
end


%%
rng(1);
chshift20x_tform={};
[optimizer,metric]=imregconfig('multimodal');
optimizer.InitialRadius=optimizer.InitialRadius/20;
optimizer.GrowthFactor=1.01;
optimizer.Epsilon=1e-9;
optimizer.MaximumIterations=300;

parfor n=1:4
   chshift20x_tform{n}=imregtform(im(:,:,n),im(:,:,1),'translation',optimizer,metric);
end

%%
im_tform=im;
for n=1:4
    im_tform(:,:,n)=imwarp(im(:,:,n),chshift20x_tform{n},'OutputView',imref2d(size(im(:,:,1))));
end
%%
im_lim=prctile(im_tform,99.9,[1,2]);
im1=double(im_tform)./double(im_lim);
%%
for n=1:4
    figure;imshowpair(im1(:,:,1),im1(:,:,n));
    title(['channel',num2str(n)]);
end
%%

%%
save(['chshift20x-', microscope_name,'-',date,'.mat'],'chshift20x_tform');

