libeblearn
/home/rex/ebltrunk/core/libeblearn/include/bbox.h
00001 /***************************************************************************
00002  *   Copyright (C) 2010 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 BBOX_H_
00034 #define BBOX_H_
00035 
00036 #include "libidx.h"
00037 
00038 namespace ebl {
00039 
00048   enum t_bbox_saving { bbox_none = 0, bbox_all = 1,
00049                        bbox_eblearn = 2, bbox_caltech = 3,
00050                        bbox_class = 4};
00051 
00052   // bbox //////////////////////////////////////////////////////////////////////
00053 
00055   class EXPORT bbox : public rect<float>, public smart_pointer {
00056   public:
00058     bbox();
00060     bbox(float h0, float w0, float height, float width);
00062     bbox(const bbox &other);
00064     virtual ~bbox();
00065 
00067     static void init_instance_id();
00071     void new_instance_id();
00074     void accumulate(bbox &b);
00077     void mul(float d);
00078 
00079     // printing ////////////////////////////////////////////////////////////////
00080 
00083     string pretty(vector<string> *labels = NULL);
00084 
00085     // member variables ////////////////////////////////////////////////////////
00086   public:
00087     int         class_id;       
00088     float       confidence;     
00089     uint        nacc;           
00090     // original map //////////////////////////////////////////////
00091     using rect<float>::h0;      
00092     using rect<float>::w0;      
00093     using rect<float>::height;  
00094     using rect<float>::width;   
00095     // scale /////////////////////////////////////////////////
00096     double      scaleh;         
00097     double      scalew;         
00098     int         iscale_index;   
00099     int         oscale_index;   
00100     // input map /////////////////////////////////////////////////
00101     rect<float> i;        
00102     vector<rect<float> > mi;        
00103     rect<float> i0;        
00104     int iheight;        
00105     int iwidth;         
00106     // output map ////////////////////////////////////////////////
00107     rect<int> o;        
00108     int oheight;        
00109     int owidth;         
00110     svector<bbox> children; 
00111 
00112   protected:
00113     uint instance_id;    //<! A unique id for this bbox.
00114     static uint iid_cnt;    //<! A unique id for this bbox.
00115   };
00116 
00118   class EXPORT bbox_parts : public bbox {
00119   public:
00121     bbox_parts();
00123     bbox_parts(const bbox &other);
00125     bbox_parts(const bbox_parts &other);
00127     virtual ~bbox_parts();
00129     void add_part(const bbox &p);
00131     void add_part(const bbox_parts &p);
00134     bool share_parts(bbox_parts &bbp);
00136     vector<bbox_parts>& get_parts();
00137   protected:
00138     vector<bbox_parts> parts;
00139   };
00140 
00141   EXPORT bbox mean_bbox(vector<bbox*> &boxes, float bonus_per_bbox,
00142                         int classid);
00143 
00145   EXPORT std::ostream& operator<<(std::ostream& out, const bbox& b);
00147   EXPORT string& operator<<(string& s, const bbox& b);
00148 
00149   // bboxes ////////////////////////////////////////////////////////////////////
00150 
00153   class EXPORT bboxes : public svector<bbox> {
00154   public:
00157     bboxes(t_bbox_saving saving_type = bbox_all, string *outdir_ = NULL,
00158            std::ostream &out = std::cout, std::ostream &err = std::cerr);
00160     virtual ~bboxes();
00161 
00168     void new_group(idxdim &dims, string *name = NULL,
00169                           int index = -1);
00170 
00173     bboxes* get_group(const string &name);
00174 
00177     idxdim get_group_dims(const string &name);
00178 
00185     void add(bbox *b, idxdim &dims, string *name = NULL, int index = -1);
00192     void add(bbox &b, idxdim &dims, string *name = NULL, int index = -1);
00198     void add(bboxes &bbs, idxdim &dims, string *name = NULL, int index = -1);
00204     void add(vector<bbox> &bbs, idxdim &dims, string *name = NULL,
00205              int index = -1);
00207     bboxes exclude(bboxes &other);
00208 
00209     // loading /////////////////////////////////////////////////////////////////
00210 
00212     void load_eblearn(const string &filename);
00213 
00214     // saving //////////////////////////////////////////////////////////////////
00215 
00219     void save(string *dir = NULL);
00223     void save_eblearn(string *dir = NULL);
00227     void save_caltech(string *dir = NULL);
00231     void save_class(string *dir = NULL);
00232 
00233     // sorting /////////////////////////////////////////////////////////////////
00234 
00237     void sort_by_confidence();
00241     template <typename T>
00242       void sort_by_difference(svector<midx<T> >& inputs);
00244     bbox *get_most_confident();
00247     bboxes get_most_confidents();
00248 
00249     // scaling /////////////////////////////////////////////////////////////////
00250 
00252     void normalize_widths(float woverh);
00255     void scale_centered(float hfact, float wfact);
00256 
00257     // thresholding ////////////////////////////////////////////////////////////
00258 
00260     void threshold(float threshold);
00261 
00262     // printing ////////////////////////////////////////////////////////////////
00263 
00265     string describe();
00267     void print();
00269     void print_saving_type();
00272     string pretty(vector<string> *labels = NULL);
00275     string pretty_short(vector<string> &labels);
00278     string str();
00279 
00280     // member variables ////////////////////////////////////////////////////////
00281   private:
00282     std::ostream                *mout;  
00283     std::ostream                *merr;  
00284     vector<svector<bbox>*>       grouped_boxes;
00285     vector<string>               group_names;
00286     vector<idxdim>               group_dims;
00287     string                       outdir;
00288     t_bbox_saving                saving_type;
00289   };
00290 
00291 } // end namespace ebl
00292 
00293 #include "bbox.hpp"
00294 
00295 #endif /* BBOX_H_ */