This is a static copy of a profile reportHome
preprocess (404 calls, 40584.927 sec)
Generated 05-Nov-2014 07:52:30 using cpu time.
function in file /usr0/home/jenkins/workspace/cellorganizer-demo3D11-glnx64/utilities/preprocess.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
258 | segimg = region_seg(image, mas... | 404 | 40131.790 s | 98.9% |  |
189 | resizeimg = ml_downsize( img, ... | 404 | 384.452 s | 0.9% |  |
240 | mask(:,:,i) = bwfill(mask(:,:,... | 7556 | 23.913 s | 0.1% |  |
238 | mask = image > ml_rcthresho... | 404 | 12.527 s | 0.0% |  |
303 | segimg = ml_findmainobj(segimg... | 404 | 10.424 s | 0.0% |  |
All other lines | | | 21.820 s | 0.1% |  |
Totals | | | 40584.927 s | 100% | |
Children (called functions)
Code Analyzer results
Line number | Message |
67 | The value assigned to variable 'display' might be unused. |
80 | The value assigned to variable 'display' might be unused. |
86 | The variable 'param' might be used before it is defined. |
123 | The value assigned to variable 'display' might be unused. |
125 | The value assigned to variable 'display' might be unused. |
138 | The first argument of WARNING should be a message identifier. Using a message identifier allows users better control over the message. |
147 | The value assigned to variable 'imsize' might be unused. |
148 | The value assigned to variable 'masksize' might be unused. |
215 | The value assigned to variable 'psf2' might be unused. |
266 | EXIST with two input arguments is generally faster and clearer than with one input argument. |
269 | EXIST with two input arguments is generally faster and clearer than with one input argument. |
275 | The variable 'img' appears to change size on every loop iteration. Consider preallocating for speed. |
279 | The value assigned here to 'path' appears to be unused. Consider replacing it by ~. |
279 | The value assigned to variable 'ext' might be unused. |
283 | The first argument of WARNING should be a message identifier. Using a message identifier allows users better control over the message. |
291 | EXIST with two input arguments is generally faster and clearer than with one input argument. |
294 | Best practice is for CATCH to be followed by an identifier that gets the error information. |
327 | Use of brackets [] is unnecessary. Use parentheses to group, if needed. |
Coverage results
[ Show coverage for parent directory ]
Total lines in function | 335 |
Non-code lines (comments, blank lines) | 140 |
Code lines (lines that can run) | 195 |
Code lines that did run | 94 |
Code lines that did not run | 101 |
Coverage (did run/can run) | 48.21 % |
Function listing
time calls line
1 function [segimg, top_slice, bot_slice, sumslices, zflip ] = preprocess( img, varargin )
2 %PREPROCESS segments and preprocesses a given image.
3 %
4 % Backwards compatibility mode
5 % Inputs
6 % ------
7 % img = image array to be processed
8 % imgFile = mat file containing image to be processed
9 % psfPath = path to psf to use
10 % downsample = [1xn] or single number defining the amount to downsample the image before preprocessing.
11 % adherent = boolean flag defining if the cell is adherent(forces largest slice to bottom)
12 % top_thresh = the fraction of fluorescence you wish to consider as the top slice
13 % bot_thresh = the fraction of fluorescence you wish to consider as the bottom slice
14 % display = boolean flag of whether to display progress on screen
15 % cellmask = binary valued image array masking the inside of the cell
16 %
17 % Outputs
18 % -------
19 % segimg = segmented image
20 % top_slice = integer value of the top slice where signal lies
21 % bot_slice = integer value of the top slice where signal lies
22 % sumslices = the cumsum of each slice
23 % zflip = boolean flag indicating if preprocess thinks the cell is upsidedown
24
25 % Copyright (C) 2006-2014 Murphy Lab
26 % Carnegie Mellon University
27 %
28 % This program is free software; you can redistribute it and/or modify
29 % it under the terms of the GNU General Public License as published
30 % by the Free Software Foundation; either version 2 of the License,
31 % or (at your option) any later version.
32 %
33 % This program is distributed in the hope that it will be useful, but
34 % WITHOUT ANY WARRANTY; without even the implied warranty of
35 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 % General Public License for more details.
37 %
38 % You should have received a copy of the GNU General Public License
39 % along with this program; if not, write to the Free Software
40 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
41 % 02110-1301, USA.
42 %
43 % For additional information visit http://murphylab.web.cmu.edu or
44 % send email to murphy@cmu.edu
45
46 %Created by Devin Sullivan 3/21/13 moved from being a subfunction of
47 %seg_cell_and_nucleus.m
48 %
49 %G. Johnson 8/22/13 - support for 2D masks, even if img is 3D
50 %G. Johnson 8/26/13 - Re-mask the output of region_seg incase the snake moves
51 % out of bounds
52 %G. Johnson 1/15/14 - Remove psf as per Ivan's request
53 %D. Sullivan 4/24/14 Removed if statment that would not work if bot_slice and bot_seg were
54 %equal
55
56 %icaoberg 26/1/2014
57 %backward compatibility mode means that it will run as before but new call
58 %will simplify calling this method directly
404 59 if nargin == 9
60 disp( 'Running method in backward compatibility mode' )
61 imgFile = varargin{1};
62 psfPath = varargin{2};
63 downsample = varargin{3};
64 adherent = varargin{4};
65 top_thresh = varargin{5};
66 bot_thresh = varargin{6};
67 display = varargin{7};
68 cellmask = varargin{8};
69 param = [];
70 param = ml_initparam( param, struct( 'display', false ) );
71 param = ml_initparam( param, struct( 'debug', false ) );
404 72 elseif nargin == 10
404 73 disp( 'Running method in backward compatibility mode' )
0.01 404 74 imgFile = varargin{1};
0.03 404 75 psfPath = varargin{2};
404 76 downsample = varargin{3};
404 77 adherent = varargin{4};
404 78 top_thresh = varargin{5};
404 79 bot_thresh = varargin{6};
0.01 404 80 display = varargin{7};
0.01 404 81 cellmask = varargin{8};
404 82 param = varargin{9};
0.12 404 83 param = ml_initparam( param, struct( 'display', false ) );
0.10 404 84 param = ml_initparam( param, struct( 'debug', false ) );
85 elseif nargin == 2
86 if ~isfield( param, 'image_file' )
87 imgFile = '';
88 else
89 imgFile = param.image_file;
90 end
91
92 if ~isfield( param, 'psf_path' );
93 psfPath = '';
94 else
95 psfPath = varargin{2};
96 end
97
98 if ~isfield( param, 'downsample' )
99 downsample = [ 5, 5, 1 ];
100 else
101 downsample = param.downsample;
102 end
103
104 if ~isfield( param, 'adherent' )
105 adherent = true;
106 else
107 adherent = param.adherent;
108 end
109
110 if ~isfield( param, 'top_threshold' )
111 top_thresh = 0.98;
112 else
113 top_thresh = param.top_threshold;
114 end
115
116 if ~isfield( param, 'bottom_threshold' )
117 bot_thresh = 0.02;
118 else
119 bot_thresh = param.bottom_threshold;
120 end
121
122 if ~isfield( param, 'display' )
123 display = false;
124 else
125 display = param.display;
126 end
127
128 if ~isfield( param, 'cellmask' )
129 cellmask = [];
130 else
131 cellmask = varargin{8};
132 end
133
134 param = [];
135 param = ml_initparam( param, struct( 'display', false ) );
136 param = ml_initparam( param, struct( 'debug', false ) );
137 else
138 warning( 'Wrong number of input arguments. Exiting method.' );
139 segimg = []; top_slice = []; bot_slice = []; sumslices = []; zflip = [];
140 return
141 end
142
0.02 404 143 if ~exist('cellmask', 'var') || isempty( cellmask )
144 cellmask = ones(size(img));
145 end
146
404 147 imsize = size(img);
404 148 masksize = size(cellmask);
149
150 %icaoberg
151 %if the psfpath is empty then do not attempt to load psf
152 % Load PSFs
0.01 404 153 if ~isempty( psfPath )
154 infPSF = imfinfo( psfPath);
155
156 psf = zeros( infPSF(1).Height, infPSF(1).Width, length(infPSF));
157
158 for I=1:length(infPSF)
159 psf(:,:,I)=imread(psfPath,I);
160 end
161
162 psf = psf.^2; % Approximate confocal PSF
404 163 else
404 164 psf = [];
404 165 end
166
167 %D. Sullivan 3/21/13 initialize the z flip to 0
404 168 zflip = 0;
169
170 %icaoberg 15/1/2014
171 %downsample PSFs and images as specified by user
172 %if the psf is not specified then the psf should be empty
0.52 404 173 if exist( imgFile, 'file' )
174 load(imgFile)
404 175 else
404 176 if ~isempty( psf )
177 disp( 'Downsampling PSF and image'); tic;
404 178 else
0.01 404 179 disp( 'Downsampling image'); tic;
404 180 end
181
0.21 404 182 disp( [ 'Downsampling using vector [x,y,z]: [' ...
183 num2str(downsample) ']' ] );
184
404 185 if ~isempty( psf )
186 psf = ml_downsize(psf,downsample,'linear');
187 end
188
384.45 404 189 resizeimg = ml_downsize( img, downsample, 'linear'); toc
190
191 % if length(size(cellmask)) == 3 && masksize(3) ~= imsize(3)
192 % [~,ind] = max(squeeze(sum(sum(cellmask,1),2)));
193 % cellmask = cellmask(:,:,ind);
194 % end
195
0.03 404 196 if length(size(cellmask)) == 3
197 %if the mask isnt the same size as the currently downsampled image
215 198 if ~all(size(cellmask) == size(resizeimg))
1.27 13 199 resizecellmask = ml_downsize(cellmask,downsample) > 0;
202 200 else
202 201 resizecellmask = cellmask;
202 202 end
203
0.01 189 204 else
7.31 189 205 resizecellmask = ml_downsize(cellmask, downsample(1:2)) > 0;
0.24 189 206 resizecellmask = repmat(resizecellmask, [1,1, size(resizeimg,3)]);
189 207 end
208
0.34 404 209 clear dnaim3
210
211 %icaoberg 15/1/2014
212 %only attempt to deconvolve image if psf is not empty
404 213 if ~isempty( psf )
214 fprintf(1,'%s\n','Deconvolving image'); tic;
215 [image,psf2] = deconvblind(resizeimg,psf); toc
404 216 else
404 217 image = resizeimg;
0.01 404 218 end
404 219 end
220
221
404 222 fprintf(1,'%s\n','Segmenting image'); tic;
0.59 404 223 sumslices = cumsum(squeeze(sum(sum(image))));
404 224 sumslices = sumslices/sumslices(end);
404 225 bot_slice=find(sumslices> bot_thresh); %0.05, 0.02
404 226 bot_slice=bot_slice(1);
404 227 top_slice=find(sumslices< top_thresh); %0.95, 0.98
0.01 404 228 top_slice=top_slice(end);
229 %D.Sullivan 6/6/13, these two lines literally do nothing
230 % bot_slice=max(bot_slice,bot_slice);
231 % top_slice=min(top_slice,top_slice);
232
233 %fprintf(1,'Total slices=%i, Bottom slice=%i, Top slice=%i\n',length(sumslices),bot_slice,top_slice);
0.12 404 234 disp( ['Total number of slices: ' num2str(length(sumslices))] );
0.10 404 235 disp( ['Bottom slice index: ' num2str(bot_slice)] );
0.12 404 236 disp( ['Top slice index: ' num2str(top_slice)] );
237
12.53 404 238 mask = image > ml_rcthreshold(image(:));
0.01 404 239 for i = 1:size(mask,3)
23.91 7556 240 mask(:,:,i) = bwfill(mask(:,:,i), 'holes');
0.04 7556 241 end
2.57 404 242 mask = repmat(sum(mask,3), [1,1,size(mask,3)]) > 0;
243
244 % bounds = ones(size(mask));
245 % bounds(3:end-3,3:end-3,:) = 0;
246 % mask(bounds >0) = 0;
247
0.01 404 248 disp( 'Cropping 3D image using mask' ); tic
2.32 404 249 mask = tz_maskimg_3d( mask, resizecellmask ); toc
250
0.01 404 251 disp( 'Region Based Active Contour Segmentation' ); tic
404 252 display = true;
404 253 maximum_iterations = 3000;
404 254 quit_tolerance = 0.00001;
255
256 %icaoberg 26/1/2014
257 %pass in parameter structure
40131.79 404 258 segimg = region_seg(image, mask, ...
0.03 404 259 maximum_iterations, 0.7, display, quit_tolerance, param ); toc
260
261 %icaoberg 26/1/2014
262 %this rearranges some files for reports
263 %needs cleanup
0.06 404 264 directory = [ pwd filesep 'showCurveAndPhi' ];
0.04 404 265 directory2 = [ param.preprocessingFolder filesep 'segmentation' ];
0.26 404 266 if ~exist( directory2 ); mkdir( directory2 ); end
267
404 268 try
0.31 404 269 if exist( directory )
270 files = dir( [ directory filesep 'iteration*' ] );
271 img = [];
272 for j=1:1:length(files)
273 file = [ directory filesep files(j).name ];
274 temp = imread( file );
275 img = [ img; temp ];
276 end
277
278 if isfield( param, 'output_filename' )
279 [ path, filename, ext ] = fileparts( param.output_filename );
280 output_filename = [ param.preprocessingFolder filesep ...
281 'segmentation' filesep filename '.png' ];
282 else
283 warning( 'Output filename not set. Inferring filename from number of files.' );
284 number_of_files = length( dir( [directory2 filesep 'img*.png'] ) ) + 1;
285 output_filename = [ param.preprocessingFolder filesep ...
286 'segmentation' filesep 'img' num2str(number_of_files) ];
287 end
288 imwrite( img, output_filename );
289 end
290
0.16 404 291 if exist( directory )
292 rmdir( directory, 's' );
293 end
294 catch
295 disp( 'Folder is empty. Removing directory' );
296 rmdir( directory, 's' );
297 end
298
0.02 404 299 clear directory
0.01 404 300 clear directory2
0.17 404 301 clear output_filename
302
10.42 404 303 segimg = ml_findmainobj(segimg);
2.26 404 304 segimg = tz_maskimg_3d(segimg, resizecellmask);
305
0.37 404 306 inds = find(sum(sum(segimg,1),2));
404 307 bot_seg = inds(1);
404 308 top_seg = inds(end);
309
0.01 404 310 bot_slice=max([bot_slice,bot_seg]);
404 311 top_slice=min([top_slice,top_seg]);
312
313 %devins 24/4/2014
314 %removed if statment that would not work if bot_slice and bot_seg were
315 %equal
404 316 if adherent %false, <param>
317 %D. Sullivan 3/21/13 check if image is right side up.
318 %since we expect the cell to generally get larger at the bottom for
319 %adherent cell lines, if the slope of the areas is positive we should
320 %flip the order
321
322 %get total cell area per slice
0.26 202 323 areas = squeeze(sum(sum(segimg)));
324 %eliminate ones that are not in the cell
0.01 202 325 cellareas = areas(areas~=0);
326 %P = coefficients P(1)*X+P(2)
0.45 202 327 P = polyfit([1:length(cellareas)],cellareas',1);
202 328 if P(1)>0
329 %set zflip flag
330 zflip = 1;
331 segimg = flipdim(segimg,3);
332 end
0.20 202 333 [bot_slice,top_slice]=fixadherent(segimg,bot_slice,top_slice);
202 334 end
0.02 404 335 clear image