libeblearn
/home/rex/ebltrunk/core/libeblearn/include/nms.h
00001 /***************************************************************************
00002  *   Copyright (C) 2012 by Pierre Sermanet *
00003  *   pierre.sermanet@gmail.com *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source
00014  *       Initiative (http://www.opensource.org) must display the
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef NMS_H_
00034 #define NMS_H_
00035 
00036 #include "bbox.h"
00037 
00038 using namespace std;
00039 
00040 namespace ebl {
00041 
00042   // nms types /////////////////////////////////////////////////////////////////
00043 
00045   enum t_nms { nms_none           = 0,  // no nms
00046                nms_overlap        = 1,  // traditional nms
00047                nms_voting         = 2,  // voting nms
00048                nms_voting_overlap = 3   // voting followed by traditional
00049   };
00050 
00051   // nms ///////////////////////////////////////////////////////////////////////
00052 
00055   class nms {
00056   public:
00070     nms(float threshold, float max_overlap, float max_hcenter_dist,
00071         float max_wcenter_dist, float pre_hfact, float pre_wfact,
00072         float post_hfact, float post_wfact, float woverh,
00073         std::ostream &out = std::cout, std::ostream &err = std::cerr);
00075     virtual ~nms();
00076 
00078     virtual void fprop(bboxes &in, bboxes &out);
00080     virtual string describe();
00081 
00082     // friends /////////////////////////////////////////////////////////////////
00083     friend class voting_nms;
00084 
00085   protected:
00086     // internal methods ////////////////////////////////////////////////////////
00087 
00089     virtual void process(bboxes &in, bboxes &out);
00093     virtual void traditional_nms(bboxes &in, bboxes &out);
00094 
00098     void prune_overlap(bboxes &raw_bboxes, bboxes &prune_bboxes,
00099                        float max_match,
00100                        bool same_class_only = false,
00101                        float min_hcenter_dist = 0.0,
00102                        float min_wcenter_dist = 0.0,
00103                        float threshold = 0.0,
00104                        float same_scale_mhd = 0.0, float same_scale_mwd = 0.0);
00105 
00106     // member variables ////////////////////////////////////////////////////////
00107   protected:
00108     std::ostream &mout; 
00109     std::ostream &merr; 
00110     float        threshold;     
00111     float        max_overlap;   
00112     float        max_hcenter_dist;      
00113     float        max_wcenter_dist;      
00114     float        pre_hfact; 
00115     float        pre_wfact; 
00116     float        post_hfact; 
00117     float        post_wfact; 
00118     float        woverh; 
00119     bool         same_class_matching_only; 
00120   };
00121 
00122   // voting nms ////////////////////////////////////////////////////////////////
00123 
00125   class voting_nms : public nms {
00126   public:
00129     voting_nms(float threshold, float max_overlap, float max_hcenter_dist,
00130                float max_wcenter_dist, float pre_hfact, float pre_wfact,
00131                float post_hfact, float post_wfact, float woverh,
00132                std::ostream &out = std::cout, std::ostream &err = std::cerr);
00135     voting_nms(float threshold, float max_overlap, float max_hcenter_dist,
00136                float max_wcenter_dist, float pre_hfact, float pre_wfact,
00137                float post_hfact, float post_wfact, float woverh,
00138                float vote_max_overlap, float vote_max_hcd, float vote_max_wcd,
00139                std::ostream &out = std::cout, std::ostream &err = std::cerr);
00141     virtual ~voting_nms();
00142 
00144     virtual string describe();
00145 
00146   protected:
00147     // internal methods ////////////////////////////////////////////////////////
00148 
00150     virtual void process(bboxes &in, bboxes &out);
00152     virtual void vote_nms(bboxes &in, bboxes &out);
00153 
00154     void dfs(bboxes &bb, vector<bool> &explored, uint i, float match,
00155              float max_center_dist, bboxes &comp);
00157     void merge_votes(bboxes &bb);
00159     void prune_votes(bboxes &in, bboxes &out);
00160 
00161     // member variables ////////////////////////////////////////////////////////
00162   protected:
00163     nms   *tnms; 
00164   };
00165 
00166 } // end namespace ebl
00167 
00168 #endif /* NMS_H_ */