Home > SuperSegger > trainingConstants > makeBadRegions.m

makeBadRegions

PURPOSE ^

makeBadRegions : creates bad regions to train the software on region shape

SYNOPSIS ^

function makeBadRegions(dirname,CONST)

DESCRIPTION ^

 makeBadRegions : creates bad regions to train the software on region shape
 Creates *_mod.mat files in the seg directory with bad regions (turns on
 and off random segments) and assigns them a bad score (0).

 INPUT :
       dirname : directory that contains seg.mat files
       CONST : segmentation constants


 Copyright (C) 2016 Wiggins Lab 
 Written by Paul Wiggins, Stella Stylianidou.
 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 makeBadRegions(dirname,CONST)
0002 % makeBadRegions : creates bad regions to train the software on region shape
0003 % Creates *_mod.mat files in the seg directory with bad regions (turns on
0004 % and off random segments) and assigns them a bad score (0).
0005 %
0006 % INPUT :
0007 %       dirname : directory that contains seg.mat files
0008 %       CONST : segmentation constants
0009 %
0010 %
0011 % Copyright (C) 2016 Wiggins Lab
0012 % Written by Paul Wiggins, Stella Stylianidou.
0013 % University of Washington, 2016
0014 % This file is part of SuperSegger.
0015 %
0016 % SuperSegger is free software: you can redistribute it and/or modify
0017 % it under the terms of the GNU General Public License as published by
0018 % the Free Software Foundation, either version 3 of the License, or
0019 % (at your option) any later version.
0020 %
0021 % SuperSegger is distributed in the hope that it will be useful,
0022 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0023 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0024 % GNU General Public License for more details.
0025 %
0026 % You should have received a copy of the GNU General Public License
0027 % along with SuperSegger.  If not, see <http://www.gnu.org/licenses/>.
0028 
0029 dirname = fixDir(dirname);
0030 contents=dir([dirname,'*_seg.mat']);
0031 num_im = length(contents);
0032 num_files = 3;
0033 h = waitbar( 0, 'Creating bad region examples for training.' );
0034 cleanup = onCleanup( @()( delete( h ) ) );
0035 for i = 1 : num_im % go through all the images
0036     try
0037     waitbar(i/num_im,h);
0038     catch
0039     end
0040     dataname = [dirname,contents(i).name];
0041     data = load(dataname);
0042     
0043     % if there are no regions it makes regions from the segments
0044     %if ~isfield( data, 'regs' ); - i will just remake them for now!
0045     %data = intMakeRegs( data, CONST, [],1);
0046     save(dataname,'-STRUCT','data');
0047     for j = 1 : num_files
0048         data_ = intModRegions( data, CONST );
0049         datamodname=[dirname,contents(i).name(1:end-4),'_',sprintf('%02d',j),'_mod.mat'];
0050         save(datamodname,'-STRUCT','data_');
0051     end
0052     
0053 end
0054 try
0055     close(h);
0056 catch
0057 end
0058 end
0059 
0060 
0061 function [data] = intModRegions ( data,CONST )
0062 % intModRegions ; modifies regions to create bad regions
0063 
0064 % fraction of segments to be modified to create bad regions
0065 FRACTION_SEG_MOD = 0.3;
0066 num_segs = numel(data.segs.score);
0067 num_mod  = ceil( num_segs*FRACTION_SEG_MOD );
0068 mod_list = unique(ceil(rand(1,num_mod)*num_segs));
0069 mod_map = logical(data.mask_cell)*0;
0070 
0071 scoreMask = false(size(data.phase));
0072 for ii = 1:data.regs.num_regs
0073     [xx,yy] = getBB( data.regs.props(ii).BoundingBox );
0074     scoreMask(yy,xx) = scoreMask(yy,xx) | logical( data.regs.score(ii) & (data.regs.regs_label(yy,xx)==ii) );
0075 end
0076 
0077 
0078 % find the indices of regions that have bad score
0079 try
0080     ind_bad_regs = find(data.regs.score == 0 ); % find bad scores
0081     ind_bad_regs = reshape(ind_bad_regs, 1, numel(ind_bad_regs));
0082     mask_bad_regs = false(size(data.phase));
0083 catch ME
0084     printError(ME);
0085 end
0086 
0087 for ii = ind_bad_regs % go through the bad regions and create a mask
0088     [xx,yy] = getBB( data.regs.props(ii).BoundingBox );
0089     mask_bad_regs(yy,xx) = logical( mask_bad_regs(yy,xx) + (data.regs.regs_label(yy,xx)==ii) );
0090 end
0091 
0092 if ~ isempty( mod_list )
0093     for ii = mod_list % segments to be modified
0094         % xx and yy location of segment in image
0095         [xx,yy] = getBB( data.segs.props(ii).BoundingBox );
0096         
0097         if ~isnan(data.segs.score(ii))
0098              if data.segs.score(ii) %  % score of the segment is 1
0099                 data.segs.score(ii) = 0; % set to 0
0100                 data.segs.segs_good(yy,xx) ...
0101                     = double(~~(data.segs.segs_good(yy,xx)...
0102                     - double(data.segs.segs_label(yy,xx)==ii)));
0103                 data.segs.segs_bad(yy,xx) = ...
0104                     double(~~(data.segs.segs_bad(yy,xx)...
0105                     +double(data.segs.segs_label(yy,xx)==ii)));
0106              else % score of the segment is 0
0107                 data.segs.score(ii) = 1;
0108                 data.segs.segs_good(yy,xx) = ...
0109                     double(~~(data.segs.segs_good(yy,xx)+...
0110                     double(data.segs.segs_label(yy,xx)==ii)));
0111                 data.segs.segs_bad(yy,xx) = ...
0112                     double(~~(data.segs.segs_bad(yy,xx)-...
0113                     double(data.segs.segs_label(yy,xx)==ii)));
0114 
0115             end
0116             
0117             % image of modified segments
0118             mod_map (yy,xx) = (data.segs.segs_label(yy,xx)==ii);
0119             %imshow(ag(mod_map));
0120             
0121         end
0122     end
0123     
0124 end
0125 
0126 % new cell mask with switched segments
0127 data.mask_cell = double((data.mask_bg - data.segs.segs_good - data.segs.segs_3n)>0);
0128 sqr3 = strel( 'square', 3 );
0129 mod_map = imdilate( mod_map, sqr3 );
0130 
0131 % make new regions using the new cell mask from modified segments
0132 
0133 % sets all scores to 1
0134 ss = size( data.mask_cell );
0135 NUM_INFO = CONST.regionScoreFun.NUM_INFO;
0136 data.regs.regs_label = bwlabel( data.mask_cell );
0137 data.regs.num_regs = max( data.regs.regs_label(:) );
0138 data.regs.props = regionprops( data.regs.regs_label, ...
0139     'BoundingBox','Orientation','Centroid','Area');
0140 data.regs.score  = ones( data.regs.num_regs, 1 );
0141 data.regs.scoreRaw = ones( data.regs.num_regs, 1 );
0142 data.regs.info = zeros( data.regs.num_regs, NUM_INFO );
0143 
0144 for ii = 1:data.regs.num_regs
0145     
0146     [xx,yy] = getBBpad( data.regs.props(ii).BoundingBox, ss, 1);
0147     mask = data.regs.regs_label(yy,xx)==ii;
0148     data.regs.info(ii,:) = CONST.regionScoreFun.props( mask, data.regs.props(ii));
0149     
0150 end
0151 
0152 E = CONST.regionScoreFun.E;
0153 data.regs.scoreRaw = CONST.regionScoreFun.fun(data.regs.info, E)';
0154 
0155 bad_regs_before = unique( data.regs.regs_label( logical(mask_bad_regs) ) );
0156 bad_regs_before = bad_regs_before(logical(bad_regs_before));
0157 data.regs.score(bad_regs_before) = 0;
0158 mod_regs = unique( data.regs.regs_label( logical(mod_map) ) );
0159 mod_regs = mod_regs(logical(mod_regs));
0160 % sets scores of all regions which had modified segments to 0
0161 data.regs.score(mod_regs) = 0;
0162 
0163 end
0164

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