-
Notifications
You must be signed in to change notification settings - Fork 2
/
batch_greenberg_reg2.m
executable file
·127 lines (104 loc) · 3.93 KB
/
batch_greenberg_reg2.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function batch_greenberg_reg2(src_path, main_fname, out_path, varargin)
% Run image registration on multiple trials in the same folder, specified
% by the main_fname. Save registered image files to "out_path".
% Should first start "matlabpool open 8" to open up multiple cores for
% parallel processing.
% wholeFrame_reg_flag = varargin{1}; An option that skips whole frame dft registration
%
if ~isempty(varargin)
wholeFrame_reg_flag = varargin{1};
else
wholeFrame_reg_flag = 1;
end
if wholeFrame_reg_flag == 1
datafiles = dir([src_path filesep main_fname '*.tif']);
fprintf('total data files %d\n', length(datafiles));
if ~exist('out_path','var')
out_path = [src_path filesep main_fname '_reg'];
elseif ~isdir(out_path)
mkdir(out_path);
end
% out_path = [out_path filesep main_fname '_reg'];
fprintf('Output path: %s\n', out_path);
fnames = {};
for i = 1:length(datafiles)
fname = datafiles(i).name;
% Check whether the file name fits the trial data file
% "main_fname_000.tif"
if ~strcmp(main_fname, '*')
if length(fname) == length(main_fname) + 7
fnames{i} = [src_path filesep fname];
end
else
fnames{i} = [src_path filesep fname];
end
end
% Take mean image of trial No 10 (arbitrary) as target image for the whole session
im_temp = imread_multi(fnames{10},'g');
im_tg = dft_reg(im_temp);
im_tg_mean = mean(im_tg,3);
% dft_shift = {}; im_descr = {};
dft_dir = [src_path filesep 'dft_reg'];
dft_shift_all = batch_dft_reg(im_tg_mean, fnames, 0, dft_dir);
dft_files = dir([dft_dir filesep main_fname, '*.tif']);
else
dft_dir = src_path;
dft_files = dir([dft_dir filesep main_fname, '*.tif']);
fprintf('Skip whole frame reg, start greenberg reg ....\nFirst file: %s\n', dft_files(1).name);
if isempty(findstr(dft_files(1).name, 'dftReg'))
warning('The images seems not registered by dft, still proceed?')
ans = input('Y / N :');
if strcmpi(ans, 'N')
return
end
end
dft_shift_all = [];
end
if matlabpool('size') == 0
matlabpool open 8
end
parfor i=1:length(dft_files)
% if isdir(datafiles(i).name)
% continue;
% end
filename = [dft_dir filesep dft_files(i).name];
info = imfinfo(filename);
if isfield(info(1), 'ImageDescription')
im_descr = info(1).ImageDescription;
else
im_descr = '';
end
im_s = imread_multi(filename,'g');
% Use as target the mean of late frames of the trial, which is more
% still, and not illuminated by light stimulation.
im_t = mean(im_s(:,:,89:end),3); %mean(im_s,3);
disp(['Start ''greenberg_reg'' for file ' dft_files(i).name ' ...']);
[im_c dx_r dy_r E] = imreg_greenberg(im_s, im_t, []);
[pthstr,temp_name,ext] = fileparts(filename);
out_name = [temp_name(1:end-3) 'greenberg_' temp_name(end-2:end)];
dest = [out_path filesep out_name];
imwrite(uint16(im_c(:,:,1)), [dest '.tif'], 'tif', 'Compression', 'none', 'Description',im_descr, 'WriteMode', 'overwrite');
for f=2:size(im_c,3)
imwrite(uint16(im_c(:,:,f)), [dest '.tif'], 'tif', 'Compression', 'none', 'WriteMode', 'append');
end
if ~isempty(dft_shift_all)
dft_shift = dft_shift_all(:,:,i); % [];%
else
dft_shift = [];
end
parsave([dest '_reginfo'], dx_r,dy_r,E,dft_shift);
fprintf('Reg data saved to %s\n',out_name);
end
if matlabpool('size') > 0
matlabpool close
end
function parsave(fn, dx_r, dy_r, E, dft_shift)
save(fn, 'dx_r','dy_r','E','dft_shift');
function [im_dft, shift] = dft_reg(im_s)
im_tg = mean(im_s(:,:,1:10),3);
for i=1:size(im_s,3);
output(:,i) = dftregistration(fft2(double(im_tg)),fft2(double(im_s(:,:,i))),1);
end
shift = output(3:4,:);
padding = [0 0 0 0];
im_dft = ImageTranslation_nx(im_s,shift,padding,0);