contrib/brl/bseg/bbgm/bbgm_features.h
Go to the documentation of this file.
00001 // This is brl/bseg/bbgm/bbgm_features.h
00002 #ifndef bbgm_features_h_
00003 #define bbgm_features_h_
00004 //:
00005 // \file
00006 // \brief A set of features for probability image processing
00007 // \author J.L. Mundy
00008 // \date May 3, 2008
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   <none yet>
00013 // \endverbatim
00014 
00015 #include <brip/brip_rect_mask.h>
00016 #include <vgl/vgl_box_2d.h>
00017 #include <vgl/vgl_polygon.h>
00018 #include <vsl/vsl_binary_io.h>
00019 #include <vsl/vsl_set_io.h>
00020 #include <vcl_vector.h>
00021 #include <vcl_set.h>
00022 #include <vcl_iosfwd.h> // for std::ostream
00023 
00024 //================================================================
00025 //=======================  mask feature ===========================
00026 //================================================================
00027 
00028 //: A background-foreground mask to detect boundaries or ribbons or corners
00029 class bbgm_mask_feature
00030 {
00031  public:
00032   bbgm_mask_feature():
00033     mid_(static_cast<unsigned char>(brip_rect_mask::ntypes)),
00034     aid_(static_cast<unsigned char>(brip_rect_mask::nangles)), p_(0.0f)
00035     {id_ = uid_++;}
00036 
00037   bbgm_mask_feature(brip_rect_mask::mask_id mid, brip_rect_mask::ang_id aid):
00038     mid_(mid),  aid_(aid), p_(0.0f)
00039     {id_ = uid_++;}
00040 
00041   //: set the probability value if the input probability is higher
00042   void set_prob(brip_rect_mask::mask_id mid, brip_rect_mask::ang_id aid, float p)
00043   {
00044     if (p>p_) {
00045       mid_ = static_cast<unsigned char>(mid);
00046       aid_ = static_cast<unsigned char>(aid);
00047       p_=p;
00048     }
00049   }
00050 
00051   //: set the probability value unconditionally
00052   void force_prob(float p) {p_ = p;}
00053 
00054   //: the mask id corresponding to current probability
00055   brip_rect_mask::mask_id mask_id() const
00056     {return static_cast<brip_rect_mask::mask_id>(mid_);}
00057 
00058   //: the angle id for the current mask id
00059   brip_rect_mask::ang_id ang_id() const
00060     {return static_cast<brip_rect_mask::ang_id>(aid_);}
00061 
00062   unsigned ni() const;
00063 
00064   unsigned nj() const;
00065 
00066   //: probability for the current template mask and angle
00067   float operator () () const { return p_;}
00068 
00069   //: mask id
00070   unsigned id() const {return id_;}
00071 
00072   //: pixels corresponding to + elements of the mask centered on (i, j)
00073   vcl_vector<vgl_point_2d<unsigned short> > pixels(unsigned i, unsigned j);
00074 
00075   //===========================================================================
00076   // Binary I/O Methods
00077   //===========================================================================
00078 
00079   //: Return a string name
00080   // \note this is probably not portable
00081   virtual vcl_string is_a() const;
00082 
00083   virtual bbgm_mask_feature* clone() const;
00084 
00085   //: Return IO version number
00086   short version() const;
00087 
00088   //: Binary save self to stream.
00089   virtual void b_write(vsl_b_ostream &os) const;
00090 
00091   //: Binary load self from stream.
00092   virtual void b_read(vsl_b_istream &is);
00093 
00094  protected:
00095   static unsigned uid_; // a unique id
00096   unsigned id_;
00097   unsigned char mid_;
00098   unsigned char aid_;
00099   float p_;
00100 };
00101 
00102 
00103 void vsl_print_summary(vcl_ostream& os,
00104                        const bbgm_mask_feature& b);
00105 
00106 
00107 //: Binary save bbgm_features
00108 void vsl_b_write(vsl_b_ostream &os, const bbgm_mask_feature& b);
00109 
00110 
00111 //: Binary load bbgm_features
00112 void vsl_b_read(vsl_b_istream &is, bbgm_mask_feature& b);
00113 
00114 //================================================================
00115 //=======================  mask pair feature =====================
00116 //================================================================
00117 
00118 //: A pair of mask features with intervening foreground probability
00119 // The pair is stored at each paired mask location
00120 class bbgm_mask_pair_feature
00121 {
00122  public:
00123   bbgm_mask_pair_feature(): mid_(brip_rect_mask::ntypes),
00124     ang0_(brip_rect_mask::nangles), ang1_(brip_rect_mask::nangles),
00125     i0_(0),j0_(0),i1_(0),j1_(0),p_(0.0f) { id_ = uid_++; }
00126 
00127   void set_prob(unsigned short i0, unsigned short j0,
00128                 unsigned short i1, unsigned short j1, float p)
00129   {
00130     i0_ = i0; j0_ = j0;
00131     i1_ = i1; j1_ = j1;
00132     p_=p;
00133   }
00134   void set_mask_type(brip_rect_mask::mask_id mid) { mid_ = mid; }
00135   void set_angles(brip_rect_mask::ang_id ang0,
00136                   brip_rect_mask::ang_id ang1)
00137   {ang0_ = ang0; ang1_ = ang1;}
00138 
00139   //: set the unique id of each mask
00140   void set_ids( unsigned id0, unsigned id1) { id0_ = id0; id1_ = id1; }
00141 
00142   //: location of each mask in the pair
00143   void x0(unsigned short& i0, unsigned short& j0) const { i0 = i0_; j0 = j0_; }
00144   void x1(unsigned short& i1, unsigned short& j1) const { i1 = i1_; j1 = j1_; }
00145 
00146   //: the type of the mask pair
00147   brip_rect_mask::mask_id mask_id() const {return mid_;}
00148 
00149   //: the orientation of mask 0
00150   brip_rect_mask::ang_id ang0() const {return ang0_;}
00151 
00152   //: the orientation of mask 1
00153   brip_rect_mask::ang_id ang1() const {return ang1_;}
00154 
00155   //: the id of mask 0
00156   unsigned id0() const {return id0_;}
00157 
00158   //: the id of mask 1
00159   unsigned id1() const {return id1_;}
00160 
00161   void center(unsigned short& ic, unsigned short& jc) const
00162   {
00163     ic  = (unsigned short)((i0_+i1_)/2);
00164     jc  = (unsigned short)((j0_+j1_)/2);
00165   }
00166   //: probability for the mask pair
00167   float operator () () const { return p_;}
00168 
00169   //: pair id
00170   unsigned id() const {return id_;}
00171 
00172   //: The pixels in the pair (positive mask elements + path pixels)
00173   vcl_vector<vgl_point_2d<unsigned short> > pixels();
00174 
00175   //======================================================================
00176   // Binary I/O Methods
00177   //======================================================================
00178 
00179   //: Return a string name
00180   // \note this is probably not portable
00181   virtual vcl_string is_a() const;
00182 
00183   virtual bbgm_mask_pair_feature* clone() const;
00184 
00185   //: Return IO version number;
00186   short version() const;
00187 
00188   //: Binary save self to stream.
00189   virtual void b_write(vsl_b_ostream &os) const;
00190 
00191   //: Binary load self from stream.
00192   virtual void b_read(vsl_b_istream &is);
00193 
00194  protected:
00195   static unsigned uid_; //!< a unique id
00196   unsigned id_;
00197   brip_rect_mask::mask_id mid_;  //!< type of masks in the pair
00198   brip_rect_mask::ang_id ang0_;  //!< orientation of mask 0
00199   brip_rect_mask::ang_id ang1_;  //!< orientation of mask 1
00200   unsigned short i0_, j0_;       //!< first mask location of the mask pair
00201   unsigned short i1_, j1_;       //!< second mask location of the mask pair
00202   unsigned id0_, id1_;
00203   float p_;
00204 };
00205 
00206 
00207 void vsl_print_summary(vcl_ostream& os,
00208                        const bbgm_mask_pair_feature& b);
00209 
00210 
00211 //: Binary save bbgm_features
00212 void vsl_b_write(vsl_b_ostream &os, const bbgm_mask_pair_feature& b);
00213 
00214 
00215 //: Binary load bbgm_features
00216 void vsl_b_read(vsl_b_istream &is, bbgm_mask_pair_feature& b);
00217 
00218 //====================== global pair functions ==================
00219 
00220 bool pair_intersect(bbgm_mask_pair_feature const& mp0,
00221                     bbgm_mask_pair_feature const& mp1,
00222                     bool plus_intersect_only = true);
00223 
00224 //================================================================
00225 //=======================  pair group feature ====================
00226 //================================================================
00227 
00228 //: functor to order the bbgm_pair_group_feature set
00229 class fless
00230 {
00231  public:
00232   fless() {}
00233   bool operator ()(bbgm_mask_pair_feature const& fa,
00234                    bbgm_mask_pair_feature const& fb) {
00235     unsigned short ica, jca, icb, jcb;
00236     fa.center(ica, jca);
00237     fb.center(icb, jcb);
00238     if (ica!=icb)
00239       return ica<icb;
00240     return jca<jcb;
00241   }
00242 };
00243 
00244 //: A group of mask pair features with centers within a neighborhood
00245 // The set is defined by the pair center locations
00246 class bbgm_pair_group_feature
00247 {
00248  public:
00249   bbgm_pair_group_feature(): mid_(brip_rect_mask::ntypes), ci_(0), cj_(0), p_(0) { id_ = uid_++; }
00250   //: A group with a single pair
00251   bbgm_pair_group_feature(bbgm_mask_pair_feature const& mp);
00252   //: set probability and vertices
00253   void set_prob(vcl_set<bbgm_mask_pair_feature, fless >const& pairs, float p)
00254   {
00255     pairs_ = pairs;
00256     vcl_set<bbgm_mask_pair_feature, fless >::const_iterator pit = pairs.begin();
00257     float cif = 0.0f, cjf = 0.0f;
00258     unsigned np = 0;
00259     for (; pit != pairs.end(); ++pit, ++np)
00260     {
00261       unsigned short ci, cj;
00262       pit->center(ci, cj);
00263       cif += (float)ci;
00264       cjf += (float)cj;
00265     }
00266     if (!np) return;
00267     ci_ = static_cast<unsigned short>(cif/(float)np);
00268     cj_ = static_cast<unsigned short>(cjf/(float)np);
00269     p_=p;
00270   }
00271   //: set probability with existing vertices
00272   void set_prob(float p) { p_ = p; }
00273 
00274   //: set the mask type of the group
00275   void set_mask_id(brip_rect_mask::mask_id mid) { mid_ = mid; }
00276 
00277   unsigned n_pairs() const {return pairs_.size();}
00278 
00279   const vcl_set<bbgm_mask_pair_feature, fless>& pairs() const
00280   { return pairs_; }
00281 
00282   //: probability for the pair group
00283   float operator () () const { return p_;}
00284 
00285   unsigned id() const {return id_;}
00286 
00287   //: the type of the mask group
00288   brip_rect_mask::mask_id mask_id() const {return mid_;}
00289 
00290   void center(unsigned short& ci, unsigned short& cj) const
00291   { ci = ci_; cj = cj_; }
00292 
00293   //: Bounding box
00294   vgl_box_2d<unsigned> bounding_box() const;
00295 
00296   //: Convex hull
00297   vgl_polygon<double> convex_hull() const;
00298 
00299   //: List of pixels in group ( + mask elements and paths)
00300   vcl_vector<vgl_point_2d<unsigned short> > pixels();
00301 
00302   //===========================================================================
00303   // Binary I/O Methods
00304   //===========================================================================
00305 
00306   //: Return a string name
00307   // \note this is probably not portable
00308   virtual vcl_string is_a() const;
00309 
00310   virtual bbgm_pair_group_feature* clone() const;
00311 
00312   //: Return IO version number;
00313   short version() const;
00314 
00315   //: Binary save self to stream.
00316   virtual void b_write(vsl_b_ostream &os) const;
00317 
00318   //: Binary load self from stream.
00319   virtual void b_read(vsl_b_istream &is);
00320 
00321   virtual ~bbgm_pair_group_feature() {} // virtual destructor, because presence of virtual methods
00322  protected:
00323   static unsigned uid_; //!< a unique id
00324   unsigned id_;
00325   brip_rect_mask::mask_id mid_; //!< the mask type of the group
00326   unsigned short ci_;           //!< the center of the group
00327   unsigned short cj_;
00328   vcl_set<bbgm_mask_pair_feature, fless > pairs_;
00329   float p_;
00330 };
00331 
00332 
00333 void vsl_print_summary(vcl_ostream& os,
00334                        const bbgm_pair_group_feature& b);
00335 
00336 
00337 //: Binary save bbgm_features
00338 void vsl_b_write(vsl_b_ostream &os, const bbgm_pair_group_feature& b);
00339 
00340 
00341 //: Binary load bbgm_features
00342 void vsl_b_read(vsl_b_istream &is, bbgm_pair_group_feature& b);
00343 
00344 //====================== global pair_group functions ==================
00345 
00346 bbgm_pair_group_feature pair_group_merge(bbgm_pair_group_feature const& pg0,
00347                                          bbgm_pair_group_feature const& pg1,
00348                                          float p_path = 1.0f);
00349 
00350 bbgm_pair_group_feature
00351 pair_group_max_union(bbgm_pair_group_feature const& pg0,
00352                      bbgm_pair_group_feature const& pg1);
00353 
00354 
00355 #endif // bbgm_features to bbgm_features_h_