nonMaximumSuppression.cc
Go to the documentation of this file.
00001 //
00002 //    nonMaximumSuppression - this nonMaximumSuppression is a re-implementation
00003 //                            of the NMS as proposed by Rosten. However, this
00004 //                            implementation is complete and about 40% faster
00005 //                            than the OpenCV-version
00006 //
00007 //    Copyright (C) 2010  Elmar Mair
00008 //
00009 //    This program is free software: you can redistribute it and/or modify
00010 //    it under the terms of the GNU General Public License as published by
00011 //    the Free Software Foundation, either version 3 of the License, or
00012 //    (at your option) any later version.
00013 //
00014 //    This program is distributed in the hope that it will be useful,
00015 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //    GNU General Public License for more details.
00018 //
00019 //    You should have received a copy of the GNU General Public License
00020 //    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #include <stdlib.h>
00023 #include "cvWrapper.h"
00024 #include "AstDetector.h"
00025 
00026 using namespace std;
00027 using namespace agast;
00028 
00029 void AstDetector::nonMaximumSuppression(const std::vector<CvPoint>& corners_all,
00030                 std::vector<CvPoint>& corners_nms)
00031 {
00032         int currCorner_ind;
00033         int lastRow=0, next_lastRow=0;
00034         vector<CvPoint>::const_iterator currCorner;
00035         int lastRowCorner_ind=0, next_lastRowCorner_ind=0;
00036         vector<int>::iterator nmsFlags_p;
00037         vector<CvPoint>::iterator currCorner_nms;
00038         int j;
00039         int numCorners_all=corners_all.size();
00040         int nMaxCorners=corners_nms.capacity();
00041 
00042         currCorner=corners_all.begin();
00043 
00044         if(numCorners_all > nMaxCorners)
00045         {
00046                 if(nMaxCorners==0)
00047                 {
00048                         nMaxCorners=512 > numCorners_all ? 512 : numCorners_all;
00049                         corners_nms.reserve(nMaxCorners);
00050                         nmsFlags.reserve(nMaxCorners);
00051                 }
00052                 else
00053                 {
00054                         nMaxCorners *=2;
00055                         if(numCorners_all > nMaxCorners)
00056                                 nMaxCorners = numCorners_all;
00057                         corners_nms.reserve(nMaxCorners);
00058                         nmsFlags.reserve(nMaxCorners);
00059                 }
00060         }
00061         corners_nms.resize(numCorners_all);
00062         nmsFlags.resize(numCorners_all);
00063 
00064         nmsFlags_p=nmsFlags.begin();
00065         currCorner_nms=corners_nms.begin();
00066 
00067         //set all flags to MAXIMUM
00068         for(j=numCorners_all; j>0; j--)
00069                 *nmsFlags_p++=-1;
00070         nmsFlags_p=nmsFlags.begin();
00071 
00072         for(currCorner_ind=0; currCorner_ind<numCorners_all; currCorner_ind++)
00073         {
00074                 int t;
00075 
00076                 //check above
00077                 if(lastRow+1 < currCorner->y)
00078                 {
00079                         lastRow=next_lastRow;
00080                         lastRowCorner_ind=next_lastRowCorner_ind;
00081                 }
00082                 if(next_lastRow!=currCorner->y)
00083                 {
00084                         next_lastRow=currCorner->y;
00085                         next_lastRowCorner_ind=currCorner_ind;
00086                 }
00087                 if(lastRow+1==currCorner->y)
00088                 {
00089                         //find the corner above the current one
00090                         while((corners_all[lastRowCorner_ind].x < currCorner->x) && (corners_all[lastRowCorner_ind].y == lastRow))
00091                                 lastRowCorner_ind++;
00092 
00093                         if( (corners_all[lastRowCorner_ind].x == currCorner->x) && (lastRowCorner_ind!=currCorner_ind) )
00094                         {
00095                                 int t=lastRowCorner_ind;
00096                                 while(nmsFlags[t]!=-1) //find the maximum in this block
00097                                         t=nmsFlags[t];
00098 
00099                                 if( scores[currCorner_ind] < scores[t] )
00100                                 {
00101                                         nmsFlags[currCorner_ind]=t;
00102                                 }
00103                                 else
00104                                         nmsFlags[t]=currCorner_ind;
00105                         }
00106                 }
00107 
00108                 //check left
00109                 t=currCorner_ind-1;
00110                 if( (currCorner_ind!=0) && (corners_all[t].y == currCorner->y) && (corners_all[t].x+1 == currCorner->x) )
00111                 {
00112                         int currCornerMaxAbove_ind=nmsFlags[currCorner_ind];
00113 
00114                         while(nmsFlags[t]!=-1) //find the maximum in that area
00115                                 t=nmsFlags[t];
00116 
00117                         if(currCornerMaxAbove_ind==-1) //no maximum above
00118                         {
00119                                 if(t!=currCorner_ind)
00120                                 {
00121                                         if( scores[currCorner_ind] < scores[t] )
00122                                                 nmsFlags[currCorner_ind]=t;
00123                                         else
00124                                                 nmsFlags[t]=currCorner_ind;
00125                                 }
00126                         }
00127                         else    //maximum above
00128                         {
00129                                 if(t!=currCornerMaxAbove_ind)
00130                                 {
00131                                         if(scores[currCornerMaxAbove_ind] < scores[t])
00132                                         {
00133                                                 nmsFlags[currCornerMaxAbove_ind]=t;
00134                                                 nmsFlags[currCorner_ind]=t;
00135                                         }
00136                                         else
00137                                         {
00138                                                 nmsFlags[t]=currCornerMaxAbove_ind;
00139                                                 nmsFlags[currCorner_ind]=currCornerMaxAbove_ind;
00140                                         }
00141                                 }
00142                         }
00143                 }
00144 
00145                 currCorner++;
00146         }
00147 
00148         //collecting maximum corners
00149         corners_nms.resize(0);
00150         for(currCorner_ind=0; currCorner_ind<numCorners_all; currCorner_ind++)
00151         {
00152                 if(*nmsFlags_p++ == -1)
00153                         corners_nms.push_back(corners_all[currCorner_ind]);
00154         }
00155 }
00156 


ptam
Author(s): Stephan Weiss, Markus Achtelik, Simon Lynen
autogenerated on Tue Jan 7 2014 11:12:22