Home > samsung-code > detSingle.m

detSingle

PURPOSE ^

DETSINGLE detects objects on a single image using an existing detection model

SYNOPSIS ^

function [Bs, Ss] = detSingle( I, det_model, gp_enabled, thresh, gp_thresh,bboxes, CategOfInterest4GP )

DESCRIPTION ^

 DETSINGLE detects objects on a single image using an existing detection model

 Usage:
   [Bs, Ss] = detSingle( I, det_model, gp_enabled, thresh, gp_thresh, ...
       bboxes, CategOfInterest4GP )
 
 INPUT:

   I: is an image matrix loaded by imread (e.g., I = imread('000220.jpg');).

   det_model: is the detection model loaded by detInit(...)

   gp_enabled: can be 0 or 1 (default)
       0 - Only selective search is used to propose regions
       1 - GP-based FTS will be applied afterward.

   thresh: threshold for pruning false postives, i.e., only boundling
   boxes with scores higher than thresh will be counted.
       Default value: 0

   gp_thresh: threshold for candidate region for the GP-based FTS.
       Default value: -1

   bboxes: can be M*4 maxtrix for intial bounding box coordinates, where 
     M is the number of initial bounding boxes. Each row should be in the
     form of [ymin, xmin, ymax, xmax].
   If bboxes is not specified or empty, Selective Search toolbox is called
   to generated bounding box proposals

   CategOfInterest4GP: can be a string cell array indicating which
     categories FTS should be applied. It is only useful when gp_enable==1
     By default FTS is applied to all the categories.
     E.g. CategOfInterest4GP = {'aeroplane','cow'}
     Remark: Whatever CategOfInterest4GP is specified, detection is done
     for all the categories.

 OUTPUT: Let N be the number of categories

   Bs : N-d cell array. Bs{i} is the coordinates of the detected bounding 
     boxes for the i-th category, which is a matrix with 4 columns. 
     Refer to "bboxes" (INPUT) for the format.

   Ss : N-d cell array. Ss{i} is a vector of the scores of the detected
     bounding boxes for the i-th category.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [Bs, Ss] = detSingle( I, det_model, gp_enabled, thresh, gp_thresh, ...
0002     bboxes, CategOfInterest4GP )
0003 % DETSINGLE detects objects on a single image using an existing detection model
0004 %
0005 % Usage:
0006 %   [Bs, Ss] = detSingle( I, det_model, gp_enabled, thresh, gp_thresh, ...
0007 %       bboxes, CategOfInterest4GP )
0008 %
0009 % INPUT:
0010 %
0011 %   I: is an image matrix loaded by imread (e.g., I = imread('000220.jpg');).
0012 %
0013 %   det_model: is the detection model loaded by detInit(...)
0014 %
0015 %   gp_enabled: can be 0 or 1 (default)
0016 %       0 - Only selective search is used to propose regions
0017 %       1 - GP-based FTS will be applied afterward.
0018 %
0019 %   thresh: threshold for pruning false postives, i.e., only boundling
0020 %   boxes with scores higher than thresh will be counted.
0021 %       Default value: 0
0022 %
0023 %   gp_thresh: threshold for candidate region for the GP-based FTS.
0024 %       Default value: -1
0025 %
0026 %   bboxes: can be M*4 maxtrix for intial bounding box coordinates, where
0027 %     M is the number of initial bounding boxes. Each row should be in the
0028 %     form of [ymin, xmin, ymax, xmax].
0029 %   If bboxes is not specified or empty, Selective Search toolbox is called
0030 %   to generated bounding box proposals
0031 %
0032 %   CategOfInterest4GP: can be a string cell array indicating which
0033 %     categories FTS should be applied. It is only useful when gp_enable==1
0034 %     By default FTS is applied to all the categories.
0035 %     E.g. CategOfInterest4GP = {'aeroplane','cow'}
0036 %     Remark: Whatever CategOfInterest4GP is specified, detection is done
0037 %     for all the categories.
0038 %
0039 % OUTPUT: Let N be the number of categories
0040 %
0041 %   Bs : N-d cell array. Bs{i} is the coordinates of the detected bounding
0042 %     boxes for the i-th category, which is a matrix with 4 columns.
0043 %     Refer to "bboxes" (INPUT) for the format.
0044 %
0045 %   Ss : N-d cell array. Ss{i} is a vector of the scores of the detected
0046 %     bounding boxes for the i-th category.
0047 %
0048 
0049 if ~exist('thresh','var') || isempty(thresh)
0050     thresh = -0;
0051 end
0052 
0053 if ~exist('gp_thresh','var') || isempty(gp_thresh)
0054     gp_thresh = -1;
0055 end
0056 
0057 if ~exist('bboxes','var')
0058     bboxes = [];
0059 end
0060 
0061 if ~exist('CategOfInterest4GP','var') || isempty(CategOfInterest4GP)
0062     CategIdOfInterest4GP = [];
0063 else
0064     [~,CategIdOfInterest4GP] = ismember( CategOfInterest4GP, det_model.categ_list );
0065 end
0066 
0067 t0=tic;
0068 
0069 persistent I0 Fcls0 Freg0 bboxes0
0070 
0071 if size(I,3)==1
0072     I = repmat( I, 1, 1, 3 );
0073 end
0074 
0075 if ~iscell(bboxes)
0076     if ~isequal(I0,I)
0077         [Fcls0, Freg0, bboxes0] = detSingleInitialFeatures( I, det_model, bboxes );
0078         I0 = I;
0079     end
0080 else
0081     bboxes0 = bboxes{1};
0082     Fcls0   = bboxes{2};
0083     Freg0   = bboxes{3};
0084 end
0085 
0086 % apply classifier
0087 fprintf('Apply classifier : '); tic
0088 S0 = ApplyClassifier(Fcls0, det_model.classifier);
0089 toc
0090 
0091 % bbox regression
0092 fprintf('BBox regression : '); tic
0093 B1 = ApplyBBoxRegressor( bboxes0, Freg0, det_model.bboxreg );
0094 B1 = mat2cell( B1, size(B1,1), size(B1,2), ones(size(B1,3),1) );
0095 
0096 S1 = cell(length( det_model.categ_list ),1);
0097 for c = 1:length( det_model.categ_list )
0098     S1{c} = S0(c,:).';
0099 end
0100 
0101 toc
0102 
0103 % gp searching
0104 if gp_enabled
0105     fprintf( 'GP searching: ' ); tic
0106     [newB, newS] = detSingleGP(I,bboxes0,S0,det_model,gp_thresh,CategIdOfInterest4GP);
0107     for c = 1:length( det_model.categ_list )
0108         if ~isempty( newB{c} )
0109             newFreg_c = features_from_bboxes( I, newB{c}, ...
0110                 det_model.cnn.canonical_patchsize, ...
0111                 det_model.cnn.padding, det_model.cnn.feat_func, ...
0112                 det_model.cnn.max_batch_num * det_model.cnn.batch_size );
0113             newB1c = ApplyBBoxRegressor( newB{c}, newFreg_c{2}, det_model.bboxreg );
0114             B1{c} = [B1{c};newB1c(:,:,c)];
0115             S1{c} = [S1{c};newS{c}];
0116         end
0117     end
0118     fprintf( ' ' );
0119     toc
0120 end
0121 
0122 % nms & thresh
0123 fprintf('NMS : '); tic
0124 
0125 if isscalar( thresh )
0126     thresh = repmat(thresh, 1, length( det_model.categ_list ));
0127 end
0128 
0129 Bs = cell(length( det_model.categ_list ),1);
0130 Ss = cell(length( det_model.categ_list ),1);
0131 
0132 for c = 1:length( det_model.categ_list )
0133     
0134     B1{c}(:,1) = max( B1{c}(:,1), 1 );
0135     B1{c}(:,2) = max( B1{c}(:,2), 1 );
0136     B1{c}(:,3) = min( B1{c}(:,3), size(I,1) );
0137     B1{c}(:,4) = min( B1{c}(:,4), size(I,2) );
0138     
0139     [Bs{c}, Ss{c}] = boxes_and_scores_after_nms( ...
0140         B1{c}, S1{c}, 0.3, [], [] );
0141     chosenIdxB = Ss{c}<thresh(c);
0142     Ss{c}(chosenIdxB)   = [];
0143     Bs{c}(chosenIdxB,:) = [];
0144 end
0145 toc
0146 
0147 fprintf( 'Total elapsed time : ' );
0148 toc(t0);
0149

Generated on Thu 18-Dec-2014 22:27:44 by m2html © 2005