0001 function [crop_box] = trackOptiAlignPad(dirname_, workers, CONST, targetd)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 if exist( dirname_, 'dir' )
0039
0040 if ~exist('workers', 'var') || isempty( workers )
0041 workers = 0;
0042 end
0043
0044
0045
0046 precision = 100;
0047
0048 if dirname_ == '.'
0049 dirname_ = pwd;
0050 end
0051 dirname_ = fixDir(dirname_);
0052
0053 verbose = CONST.parallel.verbose;
0054 file_filter = '*.tif';
0055 contents=dir([dirname_ file_filter]);
0056 num_im = numel( contents );
0057
0058 nt = [];
0059 nc = [];
0060 nxy = [];
0061 nz = [];
0062
0063
0064 for i = 1:num_im
0065 nameInfo = ReadFileName(contents(i).name);
0066 nt = [nt, nameInfo.npos(1,1)];
0067 nc = [nc, nameInfo.npos(2,1)];
0068 nxy = [nxy,nameInfo.npos(3,1)];
0069 nz = [nz, nameInfo.npos(4,1)];
0070 end
0071
0072 nt = sort(unique(nt));
0073 nc = sort(unique(nc));
0074 nxy = sort(unique(nxy));
0075 nz = sort(unique(nz));
0076
0077
0078
0079
0080 if isfield(CONST, 'imAlign') && isfield(CONST.imAlign, 'AlignChannel') && ...
0081 any(CONST.imAlign.AlignChannel == nc)
0082 nc = [CONST.imAlign.AlignChannel, ...
0083 nc(nc~=CONST.imAlign.AlignChannel)];
0084 else
0085 CONST.imAlign.AlignChannel = 1;
0086 CONST.imAlign.medFilt = false;
0087 CONST.imAlign.AlignToFirst = false;
0088 end
0089
0090
0091 if ~exist( 'targetd', 'var' ) || isempty( targetd )
0092 targetd = [dirname_,'align',filesep];
0093 else
0094 targetd = fixDir(targetd);
0095
0096 end
0097
0098
0099 mkdir(targetd);
0100 num_xy = numel(nxy);
0101
0102 if (workers>0) && (num_xy>1)
0103 SHOW_FLAG = false;
0104 else
0105 SHOW_FLAG = true;
0106 workers = 0;
0107 end
0108
0109
0110 crop_box = cell(1, num_xy);
0111
0112
0113 parfor(jj=1:num_xy, workers)
0114 crop_box{jj} = intFrameAlignXY( SHOW_FLAG, nt, nz, nc, nxy(jj), ...
0115 dirname_, targetd, nameInfo, precision, CONST);
0116 end
0117
0118 end
0119
0120 end
0121
0122
0123
0124 function crop_box = intFrameAlignXY( SHOW_FLAG, nt, nz, nc, ...
0125 nnxy, dirname, targetd, nameInfo, precision, CONST)
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144 FOCUS_NUM_LIM = 0;
0145 ERR_LIM = 1000000;
0146
0147 if SHOW_FLAG
0148 h = waitbar(0,'aligning frames: ');
0149 cleanup = onCleanup( @()( delete( h ) ) );
0150 end
0151
0152 verbose = CONST.parallel.verbose;
0153
0154 nnt = numel( nt );
0155 OutArray = zeros( nnt, 2);
0156 FocusArray = zeros( nnt, 1);
0157
0158 countt = 0;
0159 nz = sort(nz);
0160
0161 nnz = numel(nz);
0162 nnz2 = ceil(nnz/2);
0163 nz = [nz(nnz2), nz([1:(nnz2-1),(nnz2+1):nnz])];
0164 initFlag = false;
0165
0166
0167
0168 for it = nt;
0169 if SHOW_FLAG
0170 waitbar(countt/nnt,h);
0171 end
0172
0173 countt = countt+1;
0174
0175 for iz = nz
0176 for ic = nc
0177 nameInfo.npos(:,1) = [it; ic; nnxy; iz];
0178 in_name = [dirname, MakeFileName(nameInfo)];
0179 if verbose
0180 disp(['trackOptiAlignPad : Image name: ',in_name]);
0181 end
0182 im = imread(in_name);
0183
0184 if numel(size(im)) > 2
0185 disp('Images are in color - attempting to convert to monochromatic.');
0186 im = convertToMonochromatic(im);
0187 end
0188
0189 if (ic == nc(1)) && (iz == nnz2 | iz == -1)
0190
0191 if ~initFlag
0192 phaseBef = im;
0193 end
0194
0195
0196 if isfield(CONST.imAlign,'medFilt') && CONST.imAlign.medFilt
0197 im_ = medfilt2( im, [3,3], 'symmetric' );
0198 phaseBef_ = medfilt2( phaseBef, [3,3], 'symmetric' );
0199 else
0200 im_ = im;
0201 phaseBef_ = phaseBef;
0202 end
0203
0204 [out,errNum,focusNum] = intAlignIm(im_, phaseBef_, precision );
0205 if verbose
0206 disp(['focusNum: ',num2str(focusNum),' errNum: ',num2str(errNum)]);
0207 end
0208 FOCUS_FLAG = (focusNum > FOCUS_NUM_LIM) & (errNum < ERR_LIM);
0209
0210 if FOCUS_FLAG
0211 initFlag = true;
0212 OutArray(countt,:) = out(3:4);
0213 FocusArray(countt) = true;
0214 end
0215 end
0216
0217 im = intShiftIm(im, out);
0218
0219 if SHOW_FLAG
0220 if ic == nc(1)
0221 if ic>0
0222 try
0223 figure(ic)
0224 clf;
0225 imshow( cat( 3, ag(im), ...
0226 double(FOCUS_FLAG)*(0.5*ag(im)+0.5*ag(phaseBef)), ...
0227 double(FOCUS_FLAG)*ag(phaseBef) ));
0228 end
0229 end
0230 else
0231 backer = .8*ag(phaseBef);
0232 backer0 = backer;
0233 fluor = ag(uint16(im-median(im(:))));
0234
0235 figure(ic)
0236 clf;
0237 if mod(ic,2) == 0
0238 imshow( cat( 3, backer, backer0+fluor, backer0));
0239 else
0240 imshow( cat( 3, backer+fluor, backer0, backer0));
0241 end
0242 end
0243 drawnow;
0244 end
0245
0246 if FOCUS_FLAG
0247 if ic == nc(1) && ~CONST.imAlign.AlignToFirst
0248 phaseBef = im;
0249 end
0250 out_name = [targetd, MakeFileName(nameInfo)];
0251 else
0252 disp( ['Skipping frame: ', in_name] );
0253 end
0254 end
0255 end
0256 end
0257
0258
0259
0260 ss = size(phaseBef);
0261
0262 maxy = ceil( max(-OutArray(:,1)));
0263 maxx = ceil( max(-OutArray(:,2)));
0264 miny = floor(min(-OutArray(:,1)));
0265 minx = floor(min(-OutArray(:,2)));
0266
0267 sspad = [ss(1)+maxy-miny,ss(2)+maxx-minx];
0268 countt = 0;
0269 initFlag = false;
0270
0271 crop_box = [OutArray,OutArray];
0272 crop_box(:,1) = -crop_box(:,1) + 1-miny;
0273 crop_box(:,2) = -crop_box(:,2) + 1-minx;
0274 crop_box(:,3) = crop_box(:,1) + ss(1);
0275 crop_box(:,4) = crop_box(:,2) + ss(2);
0276
0277 for it = nt;
0278 if SHOW_FLAG
0279 waitbar(countt/nnt,h);
0280 end
0281
0282 countt = countt+1;
0283
0284 for iz = nz
0285 for ic = nc
0286 nameInfo.npos(:,1) = [it; ic; nnxy; iz];
0287 in_name = [dirname, MakeFileName(nameInfo)];
0288 if verbose
0289 disp(['Image name: ',in_name]);
0290 end
0291 im_ = imread( in_name );
0292 if numel(size(im_)) > 2
0293 disp('Images are in color - attempting to convert to monochromatic.');
0294 im_ = convertToMonochromatic(im_);
0295 end
0296 im = zeros(sspad, class(im_) ) + mean( im_(:));
0297 im((1-miny):(ss(1)-miny),(1-minx):(ss(2)-minx)) = im_;
0298 out = [0,0,OutArray(countt,:)];
0299
0300 im = intShiftIm(im, out + CONST.imAlign.out{ic} - ...
0301 CONST.imAlign.out{1});
0302
0303 if ~initFlag
0304 phaseBef = im;
0305 end
0306
0307 if SHOW_FLAG
0308 if ic == nc(1)
0309 figure(ic)
0310 clf;
0311 imshow(cat( 3, ag(im), ...
0312 double(FOCUS_FLAG)*(0.5*ag(im)+0.5*ag(phaseBef)), ...
0313 double(FOCUS_FLAG)*ag(phaseBef) ));
0314 else
0315 backer = .8*ag(phaseBef);
0316 backer0 = backer;
0317 fluor = ag(uint16(im-median(im(:))));
0318
0319 figure(ic)
0320 clf;
0321 if mod(ic,2) == 0
0322 imshow( cat( 3, backer, backer0+fluor, backer0));
0323 else
0324 imshow( cat( 3, backer+fluor, backer0, backer0));
0325 end
0326 end
0327 drawnow;
0328 hold on;
0329 xxxx = [crop_box(countt,2),crop_box(countt,2),...
0330 crop_box(countt,4),crop_box(countt,4),...
0331 crop_box(countt,2)];
0332 yyyy = [crop_box(countt,1),crop_box(countt,3),...
0333 crop_box(countt,3), crop_box(countt,1),...
0334 crop_box(countt,1)];
0335 plot( xxxx,yyyy,'y');
0336 end
0337
0338 if FocusArray(countt)
0339
0340 out_name = [targetd, MakeFileName(nameInfo)];
0341 imwrite(uint16(im), out_name ,'tif','Compression', 'none');
0342 initFlag = true;
0343
0344 if ic == nc(1)
0345 phaseBef = im;
0346 end
0347 else
0348 disp(['Skipping frame: ', in_name]);
0349 end
0350 end
0351 end
0352
0353 end
0354
0355
0356 if SHOW_FLAG
0357 close(h);
0358 end
0359
0360 end