Home > SuperSegger > frameLink > trackOptiStripSmall.m

trackOptiStripSmall

PURPOSE ^

trackOptiStripSmall : removes small regions and fills holes in the regions.

SYNOPSIS ^

function trackOptiStripSmall(dirname, CONST, disp_flag)

DESCRIPTION ^

 trackOptiStripSmall : removes small regions and fills holes in the regions.
 It removes regions anything with area below  CONST.trackOpti.MIN_AREA
 that are probably not real, typically bubbles, dust, or minicells.
 It then creates a new cell mask and new region fields and resaves the seg
 file.

 INPUT :
   dirname : seg folder eg. maindirectory/xy1/seg
   CONST : Constants file

 Copyright (C) 2016 Wiggins Lab
 Written by Stella Stylianidou & 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:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function trackOptiStripSmall(dirname, CONST, disp_flag)
0002 % trackOptiStripSmall : removes small regions and fills holes in the regions.
0003 % It removes regions anything with area below  CONST.trackOpti.MIN_AREA
0004 % that are probably not real, typically bubbles, dust, or minicells.
0005 % It then creates a new cell mask and new region fields and resaves the seg
0006 % file.
0007 %
0008 % INPUT :
0009 %   dirname : seg folder eg. maindirectory/xy1/seg
0010 %   CONST : Constants file
0011 %
0012 % Copyright (C) 2016 Wiggins Lab
0013 % Written by Stella Stylianidou & Paul Wiggins.
0014 % University of Washington, 2016
0015 % This file is part of SuperSegger.
0016 %
0017 % SuperSegger is free software: you can redistribute it and/or modify
0018 % it under the terms of the GNU General Public License as published by
0019 % the Free Software Foundation, either version 3 of the License, or
0020 % (at your option) any later version.
0021 %
0022 % SuperSegger is distributed in the hope that it will be useful,
0023 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0024 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0025 % GNU General Public License for more details.
0026 %
0027 % You should have received a copy of the GNU General Public License
0028 % along with SuperSegger.  If not, see <http://www.gnu.org/licenses/>.
0029 
0030 
0031 VERY_SMALL_AREA = CONST.trackOpti.MIN_AREA; % smaller that this is stripped
0032 MIN_AREA = CONST.trackOpti.MIN_AREA_NO_NEIGH; % smaller that this is stripped if no neighbors
0033 dirname = fixDir(dirname);
0034 contents=dir([dirname '*_seg.mat']);
0035 num_im = length(contents);
0036 
0037 if ~exist('disp_flag','var') || isempty(disp_flag)
0038     disp_flag = 0;
0039 end
0040 
0041 
0042 if CONST.parallel.show_status
0043     h = waitbar( 0, 'Strip small cells.');
0044     cleanup = onCleanup( @()( delete( h ) ) );
0045 else
0046     h = [];
0047 end
0048 SE = strel('disk',3);
0049 
0050 for i = 1:num_im;
0051     
0052     if CONST.parallel.show_status
0053         waitbar((num_im-i)/num_im,h,['Strip small cells--Frame: ',num2str(i),'/',num2str(num_im)]);
0054     end
0055     
0056     data_c = loaderInternal([dirname,contents(i).name]);  % load data
0057     
0058     % remove small area regions
0059     regs_label = bwlabel(data_c.mask_cell);
0060     props = regionprops( regs_label, 'Area' );
0061     area_props = [props(:).Area];
0062     small = find(area_props<=MIN_AREA);
0063     
0064     small_new = [];
0065     % only if they can not connect to other cells
0066     for j = 1 : numel(small)
0067         id = small(j);
0068         mask = imdilate(regs_label == id,SE);
0069         neighbors = unique(regs_label(mask));
0070         neighbors = neighbors(neighbors~=id);
0071         neighbors = neighbors(neighbors~=0);
0072         if isempty(neighbors) || (area_props(id) < VERY_SMALL_AREA)
0073             small_new = [small_new,id];
0074         end
0075     end
0076     
0077     
0078     % remove the small from the mask
0079     cellmask_small= ismember( regs_label,small_new );
0080     cellmask_nosmall = data_c.mask_cell ;
0081     cellmask_nosmall (cellmask_small) = 0;
0082     
0083     data_c.mask_bg (cellmask_small) =0;
0084     
0085     % remove segments in small regions
0086     dilatedMask = imdilate(cellmask_nosmall, strel('square',4));
0087     data_c.segs.segs_3n(~dilatedMask) = 0;
0088     data_c.segs.segs_good(~dilatedMask) = 0;
0089     data_c.segs.segs_bad(~dilatedMask) = 0;
0090     
0091     
0092     % filling the holes in each region separetely
0093     ss = size( data_c.phase );
0094     regs_label = bwlabel( cellmask_nosmall );
0095     props = regionprops( regs_label, {'Area','BoundingBox'} );
0096     num_props = numel(props);
0097     mask_new = false(ss);
0098     
0099     for ii = 1:num_props
0100         [xx,yy] = getBBpad(props(ii).BoundingBox, ss,1);
0101         mask = (regs_label(yy,xx)==ii);
0102         mask__ = bwmorph(bwmorph( mask, 'dilate'), 'erode' );
0103         mask__ = imfill(mask__,'holes');
0104         mask_tmp = mask_new(yy,xx);
0105         mask_tmp(mask__) = true;
0106         mask_new(yy,xx) = mask_tmp;
0107     end
0108     
0109     
0110     if disp_flag
0111         imshow(cat(3,ag( data_c.mask_cell),ag(mask_new),ag(mask_new)));
0112         pause;
0113     end
0114     
0115     data_c.mask_cell = mask_new;
0116     
0117     % remake the regions
0118     data_c = intMakeRegs( data_c, CONST);
0119     
0120     % save the updated *seg.mat file
0121     dataname=[dirname,contents(i).name];
0122     save(dataname,'-STRUCT','data_c');
0123     
0124     
0125 end
0126 
0127 if CONST.parallel.show_status
0128     close(h);
0129 end
0130 
0131 end
0132 
0133 
0134 function data = loaderInternal( filename )
0135 data = load( filename );
0136 end

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