0001 function targetd = trackOptiCropMulti(dirname,xydir,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 if ~isempty(dirname)
0030
0031 file_filter = '*.tif';
0032 dirname = fixDir(dirname);
0033
0034 contents=dir([dirname,file_filter]);
0035 num_im = numel( contents );
0036 if num_im < 0
0037 errordlg('No images found');
0038 elseif ~isRightNameFormat(dirname)
0039 errordlg('Incorrect naming convention. Please rename your images first.');
0040 else
0041 nt = [];
0042 nc = [];
0043 nxy = [];
0044 nz = [];
0045
0046 for i = 1:num_im
0047 nameInfo = ReadFileName(contents(i).name);
0048 nt = [nt, nameInfo.npos(1,1)];
0049 nc = [nc, nameInfo.npos(2,1)];
0050 nxy = [nxy,nameInfo.npos(3,1)];
0051 nz = [nz, nameInfo.npos(4,1)];
0052 end
0053
0054 nt = sort(unique(nt));
0055 nc = sort(unique(nc));
0056 nz = sort(unique(nz));
0057 nxy = sort(unique(nxy));
0058 if exist('xydir','var') && ~isempty( xydir )
0059 nxy = xydir;
0060 end
0061
0062 if ~exist( 'targetd','var' ) || isempty( targetd )
0063 targetd = [dirname,'crop',filesep];
0064 else
0065 targetd = fixDir(targetd);
0066 end
0067
0068 mkdir(targetd);
0069
0070 for nnxy = nxy;
0071
0072
0073 nameInfo.npos(:,1) = [nt(1); nc(1); nnxy; nz(1)];
0074
0075 im1 = imread( [dirname, MakeFileName(nameInfo) ]);
0076
0077 nameInfo.npos(:,1) = [nt(end); nc(1); nnxy; nz(1)];
0078 imEnd = imread( [dirname, MakeFileName(nameInfo) ]);
0079 figure(1);
0080 clf;
0081
0082 maxer = max( [im1(:);imEnd(:)]);
0083 miner = min( [im1(:);imEnd(:)]);
0084 im = cat(3, ag(im1,miner,maxer), ag(im1,miner,maxer)/2+ag(imEnd,miner,maxer)/2, ag(imEnd,miner,maxer));
0085
0086 disp('Select the crop region and double click on the image.');
0087 ss = size(im);
0088
0089 [~,C] = imcrop( im );
0090 C = floor( C );
0091 x = [C(1),C(1)+C(3)];
0092 y = [C(2),C(2)+C(4)];
0093
0094
0095 if x(1)<1
0096 x(1) = 1;
0097 elseif x(2)>ss(2)
0098 x(2) = ss(2);
0099 end
0100
0101 if y(1)<1
0102 y(1) = 1;
0103 elseif y(2)>ss(1)
0104 y(2) = ss(1);
0105 end
0106
0107 yy = y(1):y(2);
0108 xx = x(1):x(2);
0109 figure(1);
0110 clf;
0111 imshow(im(yy,xx,:));
0112 drawnow;
0113
0114
0115 for it = nt;
0116 for ic = nc;
0117 for iz = nz;
0118 nameInfo.npos(:,1) = [it; ic; nnxy; iz];
0119 in_name = [dirname, MakeFileName(nameInfo)];
0120
0121 disp( in_name );
0122
0123 im = imread( in_name );
0124 out_name = [targetd, MakeFileName(nameInfo)];
0125 imwrite( im(yy,xx), out_name, 'TIFF' );
0126 end
0127
0128 end
0129 end
0130
0131 end
0132 end
0133 end
0134 end