-
Notifications
You must be signed in to change notification settings - Fork 2
/
im_reg_single.m
executable file
·100 lines (88 loc) · 3.09 KB
/
im_reg_single.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function im_reg_single(filename, line_range, islowbaseline)
%
% Image registration for frames in a single file
% Can be compiled and run on the cluster
% filename, the image data file, can contain full path.
%
% NX -2012
if isdeployed
line_range = eval(line_range);
islowbaseline = eval(islowbaseline);
end
if ~exist(filename, 'file')
error('Data file not exist!')
end
[pthstr,temp_name,ext] = fileparts(filename);
if isempty(pthstr)
out_path = 'reg_single/';
else
out_path = [pthstr '/reg_single/'];
end
if ~exist(out_path, 'dir')
mkdir(out_path);
end
I = strfind(temp_name, 'tif');
out_name = [temp_name(1:end-3) 'greenberg_' temp_name(end-2:end)];
dest = [out_path filesep out_name '.tif'];
info = imfinfo(filename);
if isfield(info(1), 'ImageDescription')
im_descr = info(1).ImageDescription;
else
im_descr = '';
end
% Read raw data to im_s_0
% im_s_0 = imread_multi(filename,'g');
[im_s_0, header] = load_scim_data(filename);
% dft registration within the same file.
if islowbaseline == 1
im_tg_dft = get_active_frame_mean(im_s_0);
else
im_tg_dft = mean(im_s_0(:,:,end-9:end),3);
end
im_s_dft = dft_reg(im_s_0, im_tg_dft, line_range);
% disp(['Using subregion of [' num2str(line_range) '] for dft registration!']);
if islowbaseline == 1
im_t = [];
disp('Low Baseline! Using preceding frames as TARGET frame! ');
else
% Use mean of the last 10 frames as the target image for Greenberg Reg.
im_t = mean(im_s_dft(:,:,end-9:end),3);
disp('Using the mean of last 10 frame as TARGET frame! ');
end
% im_t = mean(im_s_dft(:,:,:),3);
% Options settings for greenberg registration
opt.norm_int = 0;
opt.settings.move_thresh = 0.06; % .075 % .06 paper
opt.settings.max_iter = 50 ;% 50; % 120 paper too
opt.settings.scanlinesperparameter = 2; % 1 (2 paper) % NX
%settings.scanlinesperparameter = 4; % 1 (2 paper)
opt.settings.corr_thresh = 0.55; % 0.9; % .75 % .85 paper ... .9 works 4 me
opt.settings.pregauss = .75 ; %0.75
opt.settings.haltcorr = 0.99;% .95 ; .99 paper
opt.settings.dampcorr = 0.8; % .8 paper as well
disp(['...............Start ''Greenberg_Reg'' for file ' temp_name ' .....................................']);
warning('off')
[im_c dx_r dy_r E] = imreg_greenberg_nxmod(im_s_dft, im_t, opt);
disp(['===============Done ''Greenberg_Reg'' for file ' temp_name ' ===========================']);
imwrite(uint16(im_c(:,:,1)), dest, 'tif', 'Compression', 'none', 'Description',im_descr, 'WriteMode', 'overwrite');
for f=2:size(im_c,3)
imwrite(uint16(im_c(:,:,f)), dest, 'tif', 'Compression', 'none', 'WriteMode', 'append');
end
function im_t = get_active_frame_mean(im_s)
% z-score
frame_mean_fluo = squeeze(mean(mean(im_s,1), 2));
zsc = (frame_mean_fluo - mean(frame_mean_fluo))/std(frame_mean_fluo);
% Find the peak in z-score, and average the 10 frames around the peak
% as the target frame.
[~, inds_max] = max(zsc);
if inds_max<=5
ind1 = 1;
else
ind1 = inds_max-5;
end
if inds_max+5 > length(zsc)
ind2 = length(zsc);
else
ind2 = inds_max+5;
end
im_t = mean(im_s(:,:, ind1 : ind2),3);