Home > SuperSegger > batch > trackOptiCropMulti.m

trackOptiCropMulti

PURPOSE ^

trackOptiCropMulti : user chooses two corners to crops multiple images

SYNOPSIS ^

function targetd = trackOptiCropMulti(dirname,xydir,targetd)

DESCRIPTION ^

 trackOptiCropMulti : user chooses two corners to crops multiple images
 Images must be in NIS name-format.
 New cut images are saved in dirname/crop folder.

 INPUT :
       dirname : directory with .tif images named in NIS name-format.
       xydir : number of xydirectory you would like to crop, it cuts all
       if none.

 Copyright (C) 2016 Wiggins Lab
 Written by Paul Wiggins.
 University of Washington, 2016
 This file is part of SuperSegger.

 SuperSegger is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 SuperSegger is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with SuperSegger.  If not, see <http://www.gnu.org/licenses/>.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function targetd = trackOptiCropMulti(dirname,xydir,targetd)
0002 % trackOptiCropMulti : user chooses two corners to crops multiple images
0003 % Images must be in NIS name-format.
0004 % New cut images are saved in dirname/crop folder.
0005 %
0006 % INPUT :
0007 %       dirname : directory with .tif images named in NIS name-format.
0008 %       xydir : number of xydirectory you would like to crop, it cuts all
0009 %       if none.
0010 %
0011 % Copyright (C) 2016 Wiggins Lab
0012 % Written by Paul Wiggins.
0013 % University of Washington, 2016
0014 % This file is part of SuperSegger.
0015 %
0016 % SuperSegger is free software: you can redistribute it and/or modify
0017 % it under the terms of the GNU General Public License as published by
0018 % the Free Software Foundation, either version 3 of the License, or
0019 % (at your option) any later version.
0020 %
0021 % SuperSegger is distributed in the hope that it will be useful,
0022 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0023 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0024 % GNU General Public License for more details.
0025 %
0026 % You should have received a copy of the GNU General Public License
0027 % along with SuperSegger.  If not, see <http://www.gnu.org/licenses/>.
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             % displays the first and last image on top of each other
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             % reads all the images, crops them and saves them
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

Generated on Thu 19-Jan-2017 13:55:21 by m2html © 2005