function [r_stain, r_fn, border_output, score, failed_emb, imfilesout] = TIgen(dirname, emesh, lmn, varargin) % Batch segmentation of the embryos in a directory % Version 2, variable arguments (similar to do_meshes.m) % % [r_stain, r_fn, border_output, score, failed_emb, imfilesout] = % meshgen2(dirname, emesh, lmn, varagin) % % e.g. [bill_stain, bill_fn] = % meshgen2('/Users/erwin/Desktop/bill-2008101', emesh, 0, 'verbose','dic'); % % Output: % r_stain: stain values % r_fn: filenames corresponding to r_stain % border_output: cell of the border x/y coordinates % score: scores % failed_emb: numbers of failed embryos (can be used as input) % imfilesout: filenames % % Input: % dirname: name of directory % outfile: name of the output file % lmn: cornerpoint identifier (for fembryo), 0 to use default % cornerpoint identifier: % 0=16 cornerpoints (default) % 1=8 cornerpoints % 2=16 cornerpoints % 3=4 cornerpoints (only interactive) % varargin: % 'startembryo', : start at embryo number % 'embryos', : do only embryos in that list (1D vector), overrides startembryo % 'dic': use DIC correction % 'gray': images are grayscale, DIC correction won't work % 'eff', : write to EFF file % 'verbose': verbose % 'interactive': interactive segmentation, mark points with mouse on % image % r_stain = []; r_fn = {}; r_idx = 1; version = 3; oversion = 1; %outlinemesh version %outlinemesh score noeff=0; arg_om={}; mesh_dic=''; embryos = []; startembryo = 1; verbose = 0; interactive = []; resize = 0.5; arg_gray = []; if ~isempty(varargin) i = 1; while i <= length(varargin), switch(varargin{i}), case 'eff' noeff = 1; i=i+1; outfile = varargin{i}; case 'verbose' verbose = 1; case 'start' i=i+1; startembryo = varargin{i}; case 'embryos' i=i+1; embryos = varargin{i}; case 'resize' i=i+1; resize = varargin{i}; case 'dic' if isempty(arg_gray) % args for outlinemesh arg_om = {'meshstaining', 'method', 'meshstaining', 'corrdic'}; % args for meshstaining (interactive mode) arg_int = {'method', 'corrdic'}; % EFF tagline mesh_dic = ' DIC corrected'; end case 'gray' % args for outlinemesh arg_om = {'meshstaining', 'method', 'meshstaining', 'gray'}; % args for meshstaining (interactive mode) arg_int = {'method', 'gray'}; % EFF tagline mesh_dic = ' grayscale'; arg_gray = 1; case 'interactive' interactive = 1; otherwise error('Argument %s not known', varargin{i}); end i = i+1; end end switch lmn case 0 oscore = 16; lmn = 2; case 1 oscore = 8; case 2 oscore = 16; otherwise oscore = lmn; if isempty(interactive) || lmn == 3, fprintf('Warning: lmn not known - score assigned to lmn'); end end if nargin < 5 || startembryo == 1, startembryo = 1; fmode = 'wt'; else fmode = 'at'; end failed_emb = []; stderr = 2; % dirname is directory or filename? if isdir(dirname), dirfiles = dir(dirname); j = 1; for i = 1:size(dirfiles,1) % dirfiles(i).name % if isempty(strfind(dirfiles(i).name, '.jpg')) && isempty(strfind(dirfiles(i).name, '.jpe')) dots = strfind(dirfiles(i).name, '.'); if isempty(dots) || dots(1) == 1 continue; end % dirfiles(i).name imfiles{j} = dirfiles(i).name; j = j + 1; end else % dirname is a complete path - separate the dirname into filename and dirname fnidx = max(strfind(dirname, '/')); imfiles{1} = dirname(fnidx+1:end); dirname = dirname(1:fnidx-1); end if verbose > 0, fprintf(stderr, 'Calculating boundaries:\n'); end cellno = 1; if noeff > 0, if outfile == 0, fid = 1; else fid = fopen(outfile, fmode); end fprintf(fid,'EFF 1.0\n'); end if isempty(embryos), embryos = startembryo:length(imfiles); end for i = embryos if verbose > 0, fprintf(stderr, 'Image %s (Number %u)\n', imfiles{i},i); end try fim = fullfile(dirname, imfiles{i}); im1 = imread(fim); if resize ~= 1, im1 = imresize(im1, resize); end catch errormsg = lasterr; fprintf(stderr, 'ERROR: reading/prepping image file:\n%s\n', errormsg); fprintf(stderr, 'Skipping!\n'); failed_emb = [ failed_emb i ]; continue; end if isempty(interactive), % Automatic segmentation try [x, y, escore] = fembryo(im1, version, verbose); score{cellno} = escore; border_output{cellno} = [x, y]; imfilesout{cellno} = imfiles{i}; cellno = cellno + 1; catch errormsg = lasterr; fprintf(stderr, 'ERROR: segmenting embryo failed:\n%s\n', errormsg); fprintf(stderr, 'Skipping!\n'); failed_emb = [ failed_emb i ]; continue; end j = cellno - 1; if noeff > 0, if verbose > 0, fprintf(stderr, 'Writing EFF line, score = %u\n', score{j}); end fprintf(fid, '%s\tMatlab fembryo\toutline\t%u\t%u\t%u\t0.5\t', imfilesout{j}, version, score{j}, length(border_output{j})); fprintf(fid,'%u/%u',border_output{j}(1,1),border_output{j}(1,2)); for dp = 2:length(border_output{j}) fprintf(fid,',%u/%u',border_output{j}(dp,1),border_output{j}(dp,2)); end fprintf(fid,'\n'); end if verbose > 0, fprintf('Processing %s (%u)\n', imfiles{i}, i); end try stain = outlinemesh([], im1, emesh.p, emesh.t, lmn, 1, [x, y], arg_om{:}); catch errormsg = lasterr; fprintf(stderr,'ERROR: meshing embryo failed:\n%s\n', errormsg); fprintf(stderr,'Skipping!\n'); erremb{j} = imfiles{i}; j = j+1; continue; end else % Interactive mesh generation try p_embryo = defmesh2(emesh, 3, im1, lmn); if isempty(p_embryo), error('No manual segementation output - probably user aborted'); end if verbose > 0, fprintf('Calculating the staining ...\n'); end stain = meshstaining(im1, p_embryo, emesh.t, 0, arg_int{:}); catch errormsg = lasterr; fprintf(stderr,'ERROR: meshing embryo failed:\n%s\n', errormsg); fprintf(stderr,'Skipping!\n'); erremb{j} = imfiles{i}; j = j+1; continue; end end r_stain = [r_stain, stain]; r_fn{r_idx} = imfiles{i}; r_idx = r_idx + 1; if noeff > 0, fprintf(fid, '%s\tMatlab outlinemesh%s\tmesh\t%u\t%u\t%u\t0.5\t', imfiles{i}, mesh_dic, oversion, oscore, length(stain)); fprintf(fid,'%u',stain(1)); for dp = 2:length(stain) fprintf(fid,',%u',stain(dp)); end fprintf(fid,'\n'); end end if noeff > 0, if fid ~= 1, fclose(fid); end end return;