0001 function [ nucimg, cellimg, outres ] = model2framework( model, param )
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
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 if nargin > 2
0071 error('Wrong number of input arguments.' );
0072 end
0073
0074
0075 if length(model) > 1 || ( isa( model, 'cell' ) && length(model) == 1 )
0076
0077 warning(['More than one model has been found. Synthesizing framework ' ...
0078 ' from the first model in the cell array']);
0079 model = model{1};
0080 end
0081
0082 if ~exist( 'param', 'var' )
0083 param = [];
0084 end
0085
0086
0087 param = ml_initparam( param, struct( 'debug', false ) );
0088 param = ml_initparam( param, struct( 'display', false ) );
0089 param = ml_initparam( param, struct( 'verbose', false ) );
0090
0091
0092 param = ml_initparam( param, struct( 'spherical_cell', false ) );
0093 try
0094 dimensionality = model.dimensionality;
0095 catch
0096
0097 disp( 'Unable to set dimensionality. Exiting method' );
0098 nucimg = [];
0099 cellimg = [];
0100 outres = [];
0101 return
0102 end
0103
0104
0105 if ~isfield( param, 'synthesis' ) && ...
0106 ( ~strcmpi( param.synthesis, 'all' ) || ...
0107 ~strcmpi( param.synthesis, 'framework' ) || ...
0108 ~strcmpi( param.synthesis, 'nucleus' ) )
0109 disp( 'Unrecognized synthesis flag. Setting to default value' );
0110 param.synthesis = 'all';
0111 end
0112
0113 switch lower(dimensionality)
0114 case '2d'
0115
0116
0117
0118
0119
0120
0121
0122
0123 if param.spherical_cell
0124 [nucimg,cellimg] = rm_gencellcomp2D_circle( model, param);
0125 else
0126 [nucimg,cellimg] = ml_gencellcomp2D( model, param );
0127 end
0128
0129
0130
0131 if isempty( nucimg ) && isempty( cellimg )
0132 disp( 'Nuclear image is empty. Returning empty framework.' );
0133 outres = [];
0134 return
0135 end
0136
0137 if strcmpi( param.synthesis, 'framework' ) || ...
0138 strcmpi( param.synthesis, 'all' )
0139
0140 if isempty( cellimg )
0141 disp( 'Cell image is empty. Returning empty framework.' );
0142 nucimg = [];
0143 cellimg = [];
0144 return
0145 else
0146 box = tp_imbox(cellimg);
0147 nucimg = nucimg(box(1):box(2),box(3):box(4),:);
0148 cellimg = cellimg(box(1):box(2),box(3):box(4),:);
0149 end
0150 else
0151 if isempty( nucimg )
0152 disp( 'Nuclear image is empty. Returning empty framework.' );
0153 nucimg = [];
0154 cellimg = [];
0155 else
0156 box = tp_imbox(nucimg);
0157 nucimg = nucimg(box(1):box(2),box(3):box(4),:);
0158 end
0159 end
0160
0161
0162
0163 if isfield( param,'resolution' )
0164 if isfield( param.resolution,'objects' )
0165 outres = param.resolution.objects;
0166 end
0167 outres = param.resolution;
0168 else
0169
0170 disp( 'Generated framework from 2D models are not downsampled. Inheriting resolution from model.' );
0171 outres = model.nuclearShapeModel.resolution;
0172 end
0173 case '3d'
0174
0175
0176
0177 if isfield( param, 'instance' ) && isfield(param.instance,'nucleus')
0178 if isfield(param.instance,'cell')
0179
0180 if all(size(param.instance.cell)==size(param.instance.nucleus))
0181 nucimg = param.instance.nucleus;
0182 cellimg = param.instance.cell;
0183 if isfield(param,'resolution')
0184 if isfield(param.resolution,'cell')
0185 outres = param.resolution.cell;
0186
0187 end
0188 else
0189 outres = [];
0190 end
0191 return
0192
0193 end
0194 if debug
0195 warning('Cell and nucleus found, but the images were not the same size, synthesizing new framework');
0196 end
0197 end
0198 end
0199
0200 if strcmpi( param.synthesis, 'nucleus' ) && strcmpi( model.nuclearShapeModel.type, 'medial axis' )
0201
0202 model.nuclearShapeModel.height.stat.mu = model.nuclearShapeModel.height.stat.mu;
0203 model.nuclearShapeModel.height.stat.sigma = model.nuclearShapeModel.height.stat.sigma;
0204
0205
0206
0207 param.resolution.nucleus = model.nuclearShapeModel.resolution;
0208
0209
0210
0211 instance = tp_genspsurf(model.nuclearShapeModel,param);
0212
0213
0214
0215
0216 [nucimg,nucsurf,outres] = tp_gennucshape(instance,param);
0217
0218 nucleus.nucimgsize = size(nucimg);
0219 nucleus.nucsurf = nucsurf;
0220 nucleus.nucimg = nucimg;
0221
0222 box = tp_imbox(nucimg);
0223 nucimg = nucimg(box(1):box(2),box(3):box(4),:);
0224 cellimg = [];
0225
0226 elseif ( strcmpi( param.synthesis, 'framework' ) || ...
0227 strcmpi( param.synthesis, 'all' ) ) && strcmpi( model.nuclearShapeModel.type, 'medial axis' ) && ...
0228 strcmpi( model.cellShapeModel.type, 'ratio' )
0229
0230
0231 model.nuclearShapeModel.height.stat.mu = model.nuclearShapeModel.height.stat.mu;
0232 model.nuclearShapeModel.height.stat.sigma = model.nuclearShapeModel.height.stat.sigma;
0233
0234
0235
0236 instance = tp_genspsurf(model.nuclearShapeModel,param);
0237
0238
0239
0240
0241 if param.spherical_cell
0242
0243 [nucimg,nucsurf] = rm_gensphere([1024*1.25, 1024*1.25, 5*instance.height],[0.33, 0.33, 0.33]);
0244 else
0245 [nucimg,nucsurf,outres] = tp_gennucshape(instance,param);
0246 end
0247
0248 nucleus.nucimgsize = size(nucimg);
0249 nucleus.nucsurf = nucsurf;
0250 nucleus.nucimg = nucimg;
0251
0252
0253
0254 if param.spherical_cell
0255 [cellimg,cellsurf] = rm_gensphere(1.0);
0256 else
0257 [cellimg,nucimg] = ml_gencellshape3d( ...
0258 model.cellShapeModel, nucleus,param );
0259 end
0260
0261
0262 box = tp_imbox(cellimg);
0263 nucimg = nucimg(box(1):box(2),box(3):box(4),:);
0264 cellimg = cellimg(box(1):box(2),box(3):box(4),:);
0265
0266 elseif strcmpi( model.nuclearShapeModel.type, 'diffeomorphic' ) && ...
0267 strcmpi( model.cellShapeModel.type, 'diffeomorphic' )
0268
0269 outres = param.resolution.cell;
0270 [nucimg, cellimg] = model2diffeomorphicInstance( model, param );
0271 else
0272
0273 warning( 'CellOrganizer: Unrecognized model type or combination of model types.' );
0274 nucimg = [];
0275 cellimg = [];
0276 end
0277 otherwise
0278 return
0279 end
0280 end