Home > cellorganizer > utilities > model2img.m

model2img

PURPOSE ^

MODEL2IMG Synthesizes a multicolor image from a set of SLML Level 1.0

SYNOPSIS ^

function imgs = model2img( models, param )

DESCRIPTION ^

 MODEL2IMG Synthesizes a multicolor image from a set of SLML Level 1.0
 Version 1.* instances. Saves the results to an intermedia folder and returns
 the synthesized image.

 List Of Input Arguments Descriptions
 ----------------------- ------------
 models                  A cell array of valid and parsed SLML instances.
 param                   A structure holding the function options

 The shape of param is described

 List Of Parameters        Descriptions
 ------------------        ------------
 targetDirectory           (optional) Directory where the images are going to be saved. Default is current directory.
 prefix                    (optional) Filename prefix for the synthesized images. Default is 'demo'
 nimgs                     (optional) Number of synthesized images. Default is 1.
 compression               (optional) Compression of tiff, i.e. 'none', 'lzw' and 'packbits'
 verbose                   (optional) Print the intermediate steps to screen. Default is true.
 microscope                (optional) Microscope model from which we select a point spread function. Default is 'none'
 sampling.method           (optional) Can be 'disc', 'trimmed' or 'sampled'. Default is trimmed
 sampling.density          (optional) An integer. Default is empty.
 protein.cytonuclearflag   (optional) Can 'cyto', 'nucleus' or 'all'. Default is all.

 Example
 -------
 instances = { 'model' };
 param.targetDirectory = pwd;
 param.prefix = 'demo'
 param.nimgs = 10;
 param.compression = 'lzw';
 param.microscope = 'svi';
 param.verbose = true;

 >> slml2img( instances, param );

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function imgs = model2img( models, param )
0002 % MODEL2IMG Synthesizes a multicolor image from a set of SLML Level 1.0
0003 % Version 1.* instances. Saves the results to an intermedia folder and returns
0004 % the synthesized image.
0005 %
0006 % List Of Input Arguments Descriptions
0007 % ----------------------- ------------
0008 % models                  A cell array of valid and parsed SLML instances.
0009 % param                   A structure holding the function options
0010 %
0011 % The shape of param is described
0012 %
0013 % List Of Parameters        Descriptions
0014 % ------------------        ------------
0015 % targetDirectory           (optional) Directory where the images are going to be saved. Default is current directory.
0016 % prefix                    (optional) Filename prefix for the synthesized images. Default is 'demo'
0017 % nimgs                     (optional) Number of synthesized images. Default is 1.
0018 % compression               (optional) Compression of tiff, i.e. 'none', 'lzw' and 'packbits'
0019 % verbose                   (optional) Print the intermediate steps to screen. Default is true.
0020 % microscope                (optional) Microscope model from which we select a point spread function. Default is 'none'
0021 % sampling.method           (optional) Can be 'disc', 'trimmed' or 'sampled'. Default is trimmed
0022 % sampling.density          (optional) An integer. Default is empty.
0023 % protein.cytonuclearflag   (optional) Can 'cyto', 'nucleus' or 'all'. Default is all.
0024 %
0025 % Example
0026 % -------
0027 % instances = { 'model' };
0028 % param.targetDirectory = pwd;
0029 % param.prefix = 'demo'
0030 % param.nimgs = 10;
0031 % param.compression = 'lzw';
0032 % param.microscope = 'svi';
0033 % param.verbose = true;
0034 %
0035 % >> slml2img( instances, param );
0036 
0037 % Author: Ivan E. Cao-Berg (icaoberg@cmu.edu)
0038 %
0039 % Copyright (C) 2008-2012  Murphy Lab
0040 % Lane Center for Computational Biology
0041 % School of Computer Science
0042 % Carnegie Mellon University
0043 %
0044 % March 7, 2012 R. F. Murphy Change scaling to 0-255
0045 % March 8, 2012 I. Cao-Berg Documented method
0046 % March 9, 2012 I. Cao-Berg Commented out sampling density,
0047 % changed disp to fprintf and improved logs
0048 % March 14, 2012 I. Cao-Berg Added protein.location parameter
0049 % March 14, 2012 I. Cao-Berg Added contrast stretching for every pattern
0050 % March 21, 2012 I. Cao-Berg Changed protein.location to protein.cytonuclear flag
0051 % April 11, 2012 I. Cao-Berg Parameter structure is set to empty by default
0052 % when the number of input arguments is one. Added try/catch clause to
0053 % determine verbose flag. Default is false.
0054 % April 12, 2012 R.F. Murphy Fix calculation of nucleardist and celldist;
0055 % fix name of microtubule model
0056 % August 4, 2012 D. Sullivan Used the param.nucleus and param.cell instead
0057 % of creating separate "filled" images to save memory.
0058 % August 6, 2012 I. Cao-Berg Updated access to temporary folder so that it access the
0059 % correct folder if more than one temporary folder is present in the current workspace path
0060 % October 1, 2012 I. Cao-Berg Updated code so that if model2framework fails
0061 % to make a framework and returns empty arrays, then this method returns an
0062 % empty cell array as well as throwing a warning if the debug flag is true
0063 % October 9, 2012 I. Cao-Berg Added synthesis option that allows user to
0064 % synthesize and return a nuclear instance ('nucleus'), a cell instance
0065 % ('cell'), a framework instance ('framework') or make an instance with all
0066 % the patterns in the existing model files ('all'). Default is all
0067 % November 14, 2012 D. Sullivan added param.resolution.cell and
0068 % param.resolution.objects and code to adjust frameworks to proper size for
0069 % object synthesis.
0070 % November 16, 2012 I. Cao-Berg Updated code so that parameter structure
0071 % can be passed to model2framework to allow model2diffeomorphic to inherit
0072 % parameter values from slml2img
0073 % January 21, 2013 D. Sullivan updated resolution framework s.t. user may
0074 % now specify multiple object model resolutions and the output will be in
0075 % the form of the lowest resolution (highest numbers since resolutions =
0076 % microns/pixel)
0077 % January 22, 2013 I. Cao-Berg modified if statement so that it checks
0078 % whether field exists before querying it
0079 % January 22, 2013 I. Cao-Berg extracts resolution information from first
0080 % model in list of models
0081 % February 20, 2013 D. Sullivan fixed resolution extraction
0082 % April 29, 2013 D. Sullivan Removed duplicate code in 2D case
0083 % May 8, 2013 Improved use of the param.synthesis flag for the 2D case
0084 % May 13, 2013 Fixed bug where it was not setting the param.synthesis to
0085 % the default value
0086 % May 29, 2013 G. Johnson Fixed bug in 2D that broke when no protein model was
0087 % specified
0088 % July 23, 2013 D. Sullivan Added checks for param.randomwalk which
0089 % indicates if synthesis of a walk through shape space should be done
0090 % Jul 26, 2013 G. Johnson If no synthesis param is specified, synthesize only what
0091 % is available
0092 %
0093 % This program is free software; you can redistribute it and/or modify
0094 % it under the terms of the GNU General Public License as published
0095 % by the Free Software Foundation; either version 2 of the License,
0096 % or (at your option) any later version.
0097 %
0098 % This program is distributed in the hope that it will be useful, but
0099 % WITHOUT ANY WARRANTY; without even the implied warranty of
0100 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0101 % General Public License for more details.
0102 %
0103 % You should have received a copy of the GNU General Public License
0104 % along with this program; if not, write to the Free Software
0105 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0106 % 02110-1301, USA.
0107 %
0108 % For additional information visit http://murphylab.web.cmu.edu or
0109 % send email to murphy@cmu.edu
0110 
0111 if nargin > 2
0112     error( 'CellOrganizer: Wrong number of input arguments.' );
0113 end
0114 
0115 if nargin == 1
0116     param = [];
0117 end
0118 
0119 imgs = {};
0120 
0121 try
0122     fileID = param.fileID;
0123 catch err
0124     fileID = [];
0125 end
0126 
0127 try
0128     sampling = param.sampling.method;
0129 catch err
0130     param.sampling.method = 'disc';
0131 end
0132 
0133 % try
0134 %     N = param.sampling.N;
0135 % catch
0136 %     param.sampling.N = [];
0137 % end
0138 N = [];
0139 
0140 try
0141     logger = param.logger;
0142 catch err
0143     logger = false;
0144 end
0145 
0146 try
0147     verbose = param.verbose;
0148 catch err
0149     verbose = true;
0150 end
0151 
0152 %icaoberg 10/1/2012
0153 try
0154     debug = param.debug;
0155     if ~isa( debug, 'logical' )
0156         debug = false;
0157     end
0158 catch
0159     debug = false;
0160 end
0161 
0162 %icaoberg 10/9/2012
0163 try
0164     synthesis = param.synthesis;
0165     if ~isa( synthesis, 'char' )
0166         synthesis = 'all';
0167     else
0168         if ~strcmpi( synthesis, 'nucleus' ) && ~strcmpi( synthesis, 'cell' ) && ...
0169                 ~strcmpi( synthesis, 'framework' ) && ~strcmpi( synthesis, 'all' )
0170             synthesis = 'all';
0171         else
0172             synthesis = lower( synthesis );
0173         end
0174     end
0175 catch
0176     %icaoberg 5/13/2013
0177     %grj 7/26/13 - if no synthesis param is specified, synthesize only what
0178     %is available
0179     if isfield(models{1}, 'proteinShape')
0180         param.synthesis = 'all';
0181     elseif isfield(models{1}, 'cellShapeModel')
0182         param.synthesis = 'framework';
0183     else
0184         param.synthesis = 'nucleus';
0185     end
0186     synthesis = param.synthesis;
0187 end
0188 
0189 %checking the validity of the input models
0190 
0191 %initialize parameters
0192 if isempty( models )
0193     disp( 'Input argument models cannot be empty' );
0194     imgs = [];
0195     return;
0196 else
0197     if ~isempty( fileID )
0198         fprintf( fileID, '%s\n', ['Setting model dimensionality to ' models{1}.dimensionality] );
0199     end
0200     
0201     if verbose
0202         fprintf( 1, '%s\n', ['Setting model dimensionality to ' models{1}.dimensionality] );
0203     end
0204     
0205     try
0206         dimensionality = models{1}.dimensionality;
0207     catch err
0208         warning('Unable to set model dimensionality');
0209         return
0210     end
0211 end
0212 
0213 if ~isempty( fileID )
0214     fprintf( fileID, '%s\n', 'Checking all models have the same dimensionality' );
0215 end
0216 if verbose
0217     fprintf( 1, '%s\n', 'Checking all models have the same dimensionality' );
0218 end
0219 
0220 if ~exist( [ pwd filesep 'temp'], 'dir' )
0221     mkdir( [pwd filesep 'temp'] );
0222 end
0223 
0224 for i=1:1:length(models)
0225     %icaoberg 10/1/2012
0226     if ~strcmpi( models{i}.dimensionality, dimensionality )
0227         %icaoberg 7/1/2013
0228         disp('Models have different dimensionality. Unable to synthesize image. Exiting method.');
0229         
0230         if ~isempty( fileID )
0231             fprintf( fileID, '%s\n', 'Models have different dimensionality. Unable to synthesize image.' );
0232         end
0233         
0234         return;
0235     end
0236 end
0237 
0238 %D. Sullivan 7/23/13 check for new randomwalk parameter
0239 if ~isfield(param,'randomwalk')
0240     param.randomwalk = 0;
0241 end
0242 
0243 switch lower(dimensionality)
0244     %synthesizes 2D images
0245     case '2d'
0246         if ~exist( 'param', 'var' )
0247             param  = [];
0248         end
0249         
0250         imgs = [];
0251         %the size of the images is fixed to be 1024x1024
0252         %D. Sullivan 4/29/13, this is redundent with the
0253         %ml_initparam call on ~line110 of model2framework removed the
0254         %model2framework one
0255         
0256         if ~isfield( param, 'image_size' )
0257             param = ml_initparam(param,struct('imageSize',[1024 1024]));
0258         else
0259             param = ml_initparam(param,struct('imageSize', param.image_size));
0260             param = rmfield( param, 'image_size' );
0261         end
0262         
0263         param = ml_initparam(param, ...
0264             struct('gentex',0,'loc','all', 'nimgs', 1));
0265         
0266         imgs = {};
0267         for i = 1:param.nimgs
0268             
0269             [nucEdge,cellEdge,outres] = model2framework( models{1}, param );
0270             
0271             if isempty( nucEdge ) || isempty( cellEdge )
0272                 return
0273             end
0274             
0275             param.resolution.cell = outres;
0276             
0277             %icaoberg 13/5/2012
0278             if strcmpi( param.synthesis, 'framework' ) || ...
0279                     strcmpi( param.synthesis, 'all' )
0280                 
0281                 %if you asked cellorganizer to synthesize a framework and
0282                 %either is empty, then return with a warning
0283                 if isempty( nucEdge ) || isempty( cellEdge )
0284                     if debug
0285                         warning('Unable to synthesize 2D framework.');
0286                         return
0287                     end
0288                 end
0289             else
0290                 %if you asked cellorganizer to synthesize a nucleus and it is
0291                 %empty, then return with a warning
0292                 if isempty( nucEdge )
0293                     warning('Unable to synthesize 2D nuclear shape.');
0294                     return
0295                 end
0296             end
0297             
0298             %icaoberg 5/13/2013
0299             dilate = false;
0300             if dilate
0301                 %post-process each channel
0302                 se=strel('disk',4,4);
0303                 if ~exist('nuctex','var')
0304                     nucimage = imdilate(nucEdge,se);
0305                 else
0306                     nucimage = nucEdge;
0307                 end
0308                 
0309                 %icaoberg 5/13/2013
0310                 if strcmpi( param.synthesis, 'framework' ) || ...
0311                         strcmpi( param.synthesis, 'all' )
0312                     cellimage = imdilate(cellEdge,se);
0313                 end
0314             else
0315                 %post-process each channel
0316                 nucimage = nucEdge;
0317                 cellimage = cellEdge;
0318             end
0319             
0320             img = [];
0321             img(:,:,1) = ml_bcimg(double(nucimage),[],[0 255]);
0322             
0323             if exist('cellimage', 'var') && ~isempty(cellimage) && ( strcmpi(synthesis, 'framework') || strcmpi(synthesis, 'all') )
0324                 img(:,:,2) = ml_bcimg(double(cellimage),[],[0 255]);
0325                 
0326                 %G. Johnson 5/29/13 added check to see if protein model exists
0327                 c = 1;
0328                 protimage = {};
0329                 for j=1:1:length(models)
0330                     %If the protein model is empty the only field will be
0331                     %'resolution'
0332                     if length(fieldnames(models{j}.proteinModel)) > 1
0333                         protimage{c} = ...
0334                             ml_genprotimg2D( models{j}.proteinModel, ...
0335                             nucEdge, cellEdge, param );
0336                         c = c+1;
0337                     end
0338                 end
0339                 
0340                 for j=1:1:length(protimage)
0341                     img(:,:,2+j) = ml_bcimg(double(protimage{j}),[],[0 255]);
0342                 end
0343                 
0344             end
0345             imgs{i} = img;
0346         end
0347         
0348         save( ['temp' filesep 'image.mat'], 'imgs' );
0349     case '3d'
0350         %synthesize 3D images
0351         %only do this check if synthesis is set to all'
0352         disp(['Checking synthesis flag: ' synthesis ] );
0353         
0354         %icaoberg 7/8/2013
0355         %this will verify the first model given the user only as
0356         if strcmpi( synthesis, 'nucleus' )
0357             disp( 'Checking parameters for the nuclear model from the first model in list');
0358         end
0359         
0360         
0361         %icaoberg 7/8/2013
0362         %we only care about checking the parameters for the framework iff
0363         %the synthesis flag is either set to framework or to all
0364         if strcmpi( synthesis, 'framework' ) || strcmpi( synthesis, 'all' )
0365             disp( 'Checking parameters for the cell model from the first model in list');
0366             %the cell framework, that is the nuclear and cell shape, are
0367             %generated using the first model found in the list
0368             fprintf( 1, '%s\n', 'Generating cell framework' );
0369             %icaoberg 11/16/2012
0370             
0371             %D. Sullivan 2/24/13 moved resolution setup to above
0372             %Note, setting initial framework resolution off first object
0373             %model
0374             %model2framework
0375             %Framework(cell)
0376             if ~isfield( models{1}, 'cellShapeModel' )
0377                 warning( 'First model in list did not contain a cell model. Exiting method' );
0378                 return
0379             end
0380             
0381             if isfield(models{1}.cellShapeModel,'resolution')
0382                 
0383                 %D. Sullivan 3/6/13 check if a framework instance was
0384                 %passed and if so use that resolution. If none was
0385                 %specified, return an error and exit
0386                 if isfield(param,'instance')
0387                     if isfield(param.instance,'cell')
0388                         if isfield(param,'resolution')
0389                             
0390                             if isfield(param.resolution,'cell')
0391                                 
0392                             else
0393                                 warning('No resolution specified for the pre-segmented framework (param.resolution.cell)');
0394                                 error('Cannot synthesize images');
0395                             end
0396                         else
0397                             warning('CellOrganizer error: no resolution specified for the pre-segmented framework (param.resolution.cell)');
0398                             error('Cannot synthesize images');
0399                         end
0400                     else
0401                         param.resolution.cell = models{1}.cellShapeModel.resolution;
0402                     end
0403                 else
0404                     param.resolution.cell = models{1}.cellShapeModel.resolution;
0405                 end
0406                 
0407             elseif isfield(param.resolution,'cell')
0408                 
0409             else
0410                 error('No cell resolution specified. Unable to synthesize image without this information');
0411             end
0412         end
0413         
0414         %icaoberg 7/8/2013
0415         %we only care about the resolution of objects iff the user wants to
0416         %synthesize objects
0417         if strcmpi( synthesis, 'all' )
0418             disp( 'Checking parameters for the protein model from the first model in list');
0419             
0420             %objects
0421             if isfield(models{1}.proteinShape,'resolution')
0422                 param.resolution.objects = models{1}.proteinShape.resolution;
0423             elseif isfield(param.resolution.objects)
0424                 
0425             else
0426                 error('No object resolution specified. Unable to synthesize image without this information.');
0427             end
0428         end
0429         
0430         %icaoberg 8/7/2013
0431         %this will verify the models have all the right information given
0432         %the fact the user asked to synthesize protein patterns
0433         if strcmpi( synthesis, 'all' )
0434             disp( 'Checking all the protein models from the list');
0435             
0436             disp( 'Calculating number of vesicle models' );
0437             vesicle = {};
0438             for j=1:1:length(models)
0439                 if strcmpi( models{i}.proteinShape.type, 'vesicle' )
0440                     vesicle{length(vesicle)+1} = j;
0441                 end
0442             end
0443             
0444             if ~isempty( vesicle )
0445                 fprintf( 1, '%s\n', ['At least one vesicle model was found, looking for a cell shape ' ...
0446                     ' and a nuclear shape model.']);
0447                 if isfield( models{1}, 'nuclearShapeModel' ) && isfield( models{1}, 'cellShapeModel' )
0448                     fprintf( 1, '%s\n', 'A nuclear shape and cell shape was found in the first model.');
0449                 end
0450             end
0451             
0452             microtubules = {};
0453             for j=1:1:length(models)
0454                 if strcmpi( models{j}.proteinShape.type, 'network' ) && ...
0455                         strcmpi( models{j}.proteinShape.class, 'microtubule' )
0456                     microtubules{length(microtubules)+1} = j;
0457                 end
0458             end
0459             
0460             if ~isempty( microtubules )
0461                 fprintf( 1, '%s\n', ['At least one microtubule model was found, ' ...
0462                     'looking for a centrosome model.'] );
0463                 centrosome = {};
0464                 for j=1:1:length(models)
0465                     if strcmpi( models{j}.proteinShape.type, 'centrosome' ) && ...
0466                             strcmpi( models{j}.proteinShape.class, 'centrosome' )
0467                         centrosome{length(centrosome)+1} = j;
0468                     end
0469                 end
0470             end
0471             
0472             if ~isempty( microtubules ) && isempty( centrosome )
0473                 if ~isempty( fileID )
0474                     fprintf( fileID, '%s\n', ['At least one microtubule model was found ' ...
0475                         'in your list but no centrosome models were found.'] );
0476                     fprintf( fileID, '%s\n', 'Closing log file' );
0477                     fclose( fileID );
0478                 end
0479                 
0480                 error( [num2str(length(microtubules)) ' microtubule models where found ' ...
0481                     'in your list but no centrosome models found.'] ) %uncool
0482             end
0483             
0484             if exist( 'centrosome', 'var' )
0485                 fprintf( 1, '%s\n', ['Centrosome models found:' num2str(length(centrosome))] );
0486             end
0487         end
0488         
0489         %at this point, we have verified the contents of the list of models
0490         [param.nucleus, param.cell,outres] = model2framework( models{1}, param );
0491         param.resolution.cell = outres;
0492         
0493         %icaoberg 7/8/2013
0494         if strcmpi( param.synthesis, 'nucleus' )
0495             if isempty( param.nucleus )
0496                 if debug
0497                     warning( 'Unable to synthesize 3D nucleus' );
0498                 end
0499                 
0500                 imgs = {};
0501                 return
0502             end
0503         elseif strcmpi( param.synthesis, 'framework' )
0504             if isempty( param.nucleus ) || isempty( param.cell )
0505                 if debug
0506                     warning( 'Unable to synthesize 3D framework' );
0507                 end
0508                 
0509                 imgs = {};
0510                 return
0511             end
0512         end
0513         
0514         %D. Sullivan 7/16/13 added support for walk model
0515 %         if iscell(param.cell)
0516 %             numimgs = length(param.cell);
0517 %             cellwalk = param.cell;
0518 %             param.cell = cellwalk{1};
0519 %             nucwalk = param.nucleus;
0520 %             param.nucleus = nucwalk{1};
0521 %         else
0522 %             numimgs = 1;
0523 %         end
0524         %Walk through all the cell frameworks generated by the walk through
0525         %shape space
0526 %         for currimg = 1:numimgs
0527             %if doing walk, get the current cell
0528         
0529             
0530         fprintf( 1, '%s\n', 'Filling cell and nuclear images' );
0531         
0532         %devins 8/4/12
0533         %used the param.nucleus and param.cell instead of creating
0534         %seperate "filled" images to save memory.
0535         
0536         param.nucleusfilled=imfill(param.nucleus>0,'holes');
0537         param.cellfilled=imfill(param.cell>0,'holes');
0538 %         if iscell(param.cell)&&iscell(param.nucleus)
0539 %             for i = 1:length(param.cell)
0540 %                 param.nucleus{i}=imfill(param.nucleus{i}>0,'holes');
0541 %                 param.cell{i}=imfill(param.cell{i}>0,'holes');
0542 %             end
0543 %         else
0544 %             param.nucleus=imfill(param.nucleus>0,'holes');
0545 %             param.cell=imfill(param.cell>0,'holes');
0546 %         end
0547         %D. Sullivan 11/14/12
0548         %moved resolution code from the model2instance in case the user
0549         %trained all object models at a single resolution
0550         %**this also allows the distance images to be calculated after
0551         %the resolution adjustment**
0552         %Check if the protein models were learned at a different
0553         %resolution than the cell shape models
0554         %D. Sullivan 1/21/13 moved above cell/nuc image save so the
0555         %images are saved at the correct resolution
0556         
0557         try
0558             %icaoberg 1/22/13
0559             %extracts resolution information from first model in list
0560             %D. Sullivan 2/24/13, change priority of resolution to prefer
0561             %model associated resolution
0562             %D. Sullivan 2/21/13, First try the user specified param struct.
0563             %                 if isfield(models{1}.cellShapeModel,'resolution')
0564             %                     param.resolution.cell = models{1}.cellShapeModel.resolution;
0565             %                 elseif isfield(param.resolution,'cell')
0566             %
0567             %                 else
0568             %                     error('No cell resolution specified!');
0569             %                 end
0570             %
0571             %D. Sullivan 2/20/13
0572             %loop through and get resolution for each object model
0573             if isfield(models{1}.proteinShape,'resolution')
0574                 param.resolution.objects = zeros(length(models),3);
0575                 for i = 1:length(models)
0576                     param.resolution.objects(i,:) = models{i}.proteinShape.resolution;
0577                 end
0578             elseif isfield(param.resolution,'objects')
0579                 
0580             else
0581                 error('No object resolutions specified!');
0582             end
0583             
0584             
0585             if( size( param.resolution.objects, 1 ) == 1 )
0586                 %D. Sullivan 7/18/13 added a function to resize so this can
0587                 %be used in other places
0588                 %Resize nucleus
0589                 param.nucleus = AdjustResolutions(param.nucleus,...
0590                     param.resolution.cell,param.resolution.objects);
0591                 %Resize cell
0592                 param.cell = AdjustResolutions(param.cell,...
0593                     param.resolution.cell,param.resolution.objects);
0594                 %set output resolution
0595                 if ~isfield(param, 'outputres')
0596                 param.outputres = param.resolution.objects;
0597                 end
0598 %                 %since the cell is built conditionally on the nucleus, they should
0599 %                 %ALWAYS have the same resolution
0600 %                 initres = param.resolution.cell;%e.g. [0.05,0.05,0.35]
0601 %                 finalres = param.resolution.objects;
0602 %                 param.outputres=finalres;
0603 %
0604 %                 %nucleus
0605 %                 finalsize_x = floor(initres(1)./finalres(1).*size(param.nucleus,1));
0606 %                 finalsize_y = floor(initres(2)./finalres(2).*size(param.nucleus,2));
0607 %                 finalsize_z = floor(initres(3)./finalres(3).*size(param.nucleus,3));
0608 %
0609 %                 param.nucleus = imresize(param.nucleus,[finalsize_x finalsize_y],'bilinear');
0610 %                 %need to resize the z
0611 %                 param.nucleus = tp_stretch3d(param.nucleus,finalsize_z);
0612 %                 param.nucleus = param.nucleus>0;
0613 %
0614 %                 %cell
0615 %                 %Note: the recalculation of final size should be unnecessary since the
0616 %                 %cell and nucleus images should always be the same size, but since the
0617 %                 %arithmatic is trivially fast I recalculate to deal with potential
0618 %                 %weird situations in the future(e.g. if we need the nuclear image to be
0619 %                 %a smaller object that we add in to the cell image for space)DPS
0620 %                 finalsize_x = floor(initres(1)./finalres(1).*size(param.cell,1));
0621 %                 finalsize_y = floor(initres(2)./finalres(2).*size(param.cell,2));
0622 %                 finalsize_z = floor(initres(3)./finalres(3).*size(param.cell,3));
0623 %                 param.cell = imresize(param.cell,[finalsize_x finalsize_y],'bilinear');
0624 %                 %need to resize the z
0625 %                 param.cell = tp_stretch3d(param.cell,finalsize_z);
0626 %                 param.cell = param.cell>0;
0627 
0628             elseif(size(param.resolution.objects,1)>1)
0629                 %D. Sullivan 3/15/13, temporarily changing from max to
0630                 %min so that images will be at highest resolution
0631                 %possible, but artifacts from upsampling may be present
0632                 %
0633                 if ~isfield(param, 'outputres')
0634                 param.outputres = param.resolution.objects;
0635                 end
0636             end
0637         catch
0638             %icaoberg 7/8/2013
0639             if ~strcmpi( param.synthesis, 'nucleus' )
0640                 warning(['CellOrganizer: No resolution specified for either cell or object class ',...
0641                     'assuming no resizing necessary. If this is incorrect unexpected results will occur.'])
0642             end
0643         end
0644         
0645         
0646         %icaoberg 10/9/2012
0647         %save the synthetic nucleus to temporary folder
0648         if strcmpi( synthesis, 'nucleus' ) || ...
0649                 strcmpi( synthesis, 'framework' ) || ...
0650                 strcmpi( synthesis, 'all' )
0651             img = param.nucleus;
0652             %icaoberg 8/6/2012
0653             save( [pwd filesep 'temp/image1.mat'], 'img' );
0654             imgs{length(imgs)+1} = img;
0655             clear img;
0656         end
0657         
0658         %if the user only requested a nucleus, stop method
0659         if strcmpi( synthesis, 'nucleus' )
0660             return
0661         end
0662         
0663         %save synthetic cell to temporary folder
0664         if strcmpi( synthesis, 'cell' ) || ...
0665                 strcmpi( synthesis, 'framework' ) || ...
0666                 strcmpi( synthesis, 'all' )
0667             img = param.cell;
0668             %icaoberg 8/6/2012
0669             save( [pwd filesep 'temp/image2.mat'], 'img' );
0670             imgs{length(imgs)+1} = img;
0671             clear img;
0672         end
0673         
0674         %if the user requested a cell membrane or a framework, stop
0675         %method
0676         if strcmpi( synthesis, 'cell' ) || strcmpi( synthesis, 'framework' )
0677             return
0678         end
0679         
0680         fprintf( 1, '%s\n', ['Computing Euclidean distance transform to cell and nuclear edges'] );
0681         param.celldist = bwdist(bwperim(param.cell));
0682         param.nucleardist = bwdist(bwperim(param.nucleus));
0683         param.nucleardist(param.nucleus==1) = -param.nucleardist(param.nucleus==1);
0684         
0685         if exist( 'centrosome', 'var' ) && ~isempty( centrosome )
0686             fprintf( 1, '%s\n', 'Generating centrosome.' );
0687             [param.centrosome,outres] = model2instance( models{centrosome{1}}.proteinShape, param );
0688         end
0689         
0690         if verbose
0691             fprintf( 1, '%s\n', ['Saving temporary cell shape model instance'] );
0692         end
0693         
0694         clear blank;
0695         clear box;
0696         clear instance;
0697         clear nuclei;
0698         
0699         for j=1:1:length(models)
0700             param.currentmodelnum = j;
0701             if ~strcmpi( models{j}.proteinShape.class, 'centrosome' )
0702                 if verbose
0703                     tic
0704                     disp( ['Generating protein pattern model instance ' num2str(j) ' from ' models{j}.proteinShape.class ' pattern'] );
0705                 end
0706                 
0707                 [img,outres] = model2instance( models{j}.proteinShape, param );
0708                 %img = ml_bcimg(double(img),[],[]);
0709                 
0710                 if verbose
0711                     disp( ['Saving temporary protein model instance ' num2str(j+2) ] );
0712                 end
0713                 %icaoberg 8/6/2012
0714                 save( [pwd filesep 'temp' filesep 'image' num2str(j+2) '.mat'], 'img' );
0715                 imgs{length(imgs)+1} = img;
0716                 clear img;
0717             end
0718             %D. Sullivan 7/18/13 adding object movement model
0719             if param.randomwalk == true
0720                 objectMovementModel(models{j},param)
0721             end
0722         end
0723         
0724         %D.Sullivan 1/21/13 readjust cell and nucleus to be same
0725         %resolution as final object models and resave temporary files
0726         if( isfield( param, 'resolution' ) && isfield(param, 'outputres'))
0727             %since the cell is built conditionally on the nucleus, they should
0728             %ALWAYS have the same resolution
0729             initres = param.resolution.cell;%e.g. [0.05,0.05,0.35]
0730             
0731             %if outputres is a single value, we assume it's a scalar on the
0732             %image resolution
0733             finalres = param.outputres;
0734             
0735             if length(finalres) == 1
0736                 finalres = repmat(1/finalres, [1,3]).*initres;
0737             end
0738             
0739             %nucleus
0740             finalsize_x = ceil(initres(1)./finalres(1).*size(param.nucleus,1));
0741             finalsize_y = ceil(initres(2)./finalres(2).*size(param.nucleus,2));
0742             finalsize_z = ceil(initres(3)./finalres(3).*size(param.nucleus,3));
0743             param.nucleus = imresize(param.nucleus,[finalsize_x finalsize_y],'bilinear');
0744             
0745             
0746             %need to resize the z
0747             param.nucleus = tp_stretch3d(param.nucleus,finalsize_z);
0748             param.nucleus = param.nucleus>0;
0749             imgs{1} = param.nucleus;
0750             %resave the synthetic nucleus to temporary folder
0751             if strcmpi( synthesis, 'nucleus' ) || ...
0752                     strcmpi( synthesis, 'framework' ) || ...
0753                     strcmpi( synthesis, 'all' )
0754                 img = param.nucleus;
0755                 %icaoberg 8/6/2012
0756                 save( [pwd filesep 'temp/image1.mat'], 'img' );
0757                 clear img;
0758             end
0759             
0760             %cell
0761             %Note: the recalculation of final size should be unnecessary since the
0762             %cell and nucleus images should always be the same size, but since the
0763             %arithmatic is trivially fast I recalculate to deal with potential
0764             %weird situations in the future(e.g. if we need the nuclear image to be
0765             %a smaller object that we add in to the cell image for space) DPS
0766             finalsize_x = ceil(initres(1)./finalres(1).*size(param.cell,1));
0767             finalsize_y = ceil(initres(2)./finalres(2).*size(param.cell,2));
0768             finalsize_z = ceil(initres(3)./finalres(3).*size(param.cell,3));
0769             param.cell = imresize(param.cell,[finalsize_x finalsize_y],'bilinear');
0770             
0771             %need to resize the z
0772             param.cell = tp_stretch3d(param.cell,finalsize_z);
0773             param.cell = param.cell;
0774             param.cell = param.cell>0;
0775             imgs{2} = param.cell;
0776             %resave synthetic cell to temporary folder
0777             if strcmpi( synthesis, 'cell' ) || ...
0778                     strcmpi( synthesis, 'framework' ) || ...
0779                     strcmpi( synthesis, 'all' )
0780                 img = param.cell;
0781                 %icaoberg 8/6/2012
0782                 save( [pwd filesep 'temp/image2.mat'], 'img' );
0783                 clear img;
0784             end
0785             
0786             
0787             %resize the remaining images to the output resolution
0788             for j = 3:length(imgs)
0789                 img = imgs{j};
0790                 img = imresize(img,[finalsize_x finalsize_y],'bilinear');
0791                 img = tp_stretch3d(img, finalsize_z);
0792                 save( [pwd filesep 'temp/image' num2str(j) '.mat'], 'img' );
0793                 imgs{j} = img;
0794                 
0795             end
0796             
0797         end
0798         
0799 %    end
0800     otherwise
0801         error( 'CellOrganizer: Unknown dimensionality or dimensionality argument missing.' );
0802 end
0803 end%model2img

Generated on Sun 29-Sep-2013 18:44:06 by m2html © 2005