contrib/brl/bseg/sdet/sdet_mrf_site_bp.h
Go to the documentation of this file.
00001 #ifndef sdet_mrf_site_bp_h_
00002 #define sdet_mrf_site_bp_h_
00003 //:
00004 // \file
00005 // \brief  A class for representing a site node in an MRF
00006 // \author J.L. Mundy
00007 // \date   26 March 2011
00008 //
00009 // Stores two sets of buffer arrays,one for messages received at the last step
00010 //  and one for messages coming in during the current iteration
00011 // the MRF has a 4-neighborhood (u, l, r, d) with the neighbor index
00012 // in the order (0 1 2 3).
00013 //
00014 // The data cost for each site is D(fp) = lambda_*(fp-x)^2, where x is the
00015 // observed data and fp is a site label.
00016 //
00017 // D(fp) is set to min(lambda_*(fp-x)^2, truncation_cost_);
00018 //
00019 //  Each site stores a pair of label buffers for each of the neighbors
00020 //  One buffer in the pair stores the message received on the last
00021 //  iteration (p), the other receives the current messages (c). On each
00022 //  iteration, the buffers are swapped. A pair of buffers is allocated for
00023 //  each of the (u, l, r, d) neighbors in the 4-connected neighborhood
00024 //  as shown below.
00025 //
00026 //          (c p)
00027 //            u
00028 //    (c,p) l x r (c,p)
00029 //            d
00030 //          (c,p)
00031 //
00032 #include <vbl/vbl_ref_count.h>
00033 #include <vcl_vector.h>
00034 class sdet_mrf_site_bp : public vbl_ref_count
00035 {
00036  public:
00037 
00038   sdet_mrf_site_bp(unsigned n_labels, float lambda, float truncation_cost);
00039   void switch_buffers() { prior_ = 1-prior_; }
00040   int prior() const { return prior_; }
00041   int current() const { return 1-prior_; }
00042   //: set the observed label
00043   void set_label(float obs_label) { obs_label_ = obs_label; }
00044 
00045   // === cost functions ===
00046 
00047   //:data cost due to observed continuous label value
00048   float D(unsigned fp);
00049 
00050   //:sum over stored prior messages, except the message from neighbor nq
00051   float M(unsigned nq, unsigned fp);
00052 
00053   //:total of D and M
00054   float h(unsigned nq, unsigned fp) { return D(fp) + M(nq, fp); }
00055 
00056   //:belief, sum of data cost and sum of all four prior messages
00057   float b(unsigned fp);
00058 
00059   //:the most probable label, label with minimum belief
00060   unsigned believed_label();
00061 
00062   //set the current message from neighbor nq
00063   void set_cur_message(unsigned nq, unsigned fp, float msg);
00064 
00065   //:the current message value
00066   float cur_message(unsigned nq, unsigned fp) const { return msg_[1-prior_][nq][fp]; }
00067 
00068   //:the prior message value
00069   float prior_message(unsigned nq, unsigned fp) const { return msg_[prior_][nq][fp]; }
00070 
00071   //:entire prior message
00072   vcl_vector<float> prior_message(unsigned nq);
00073 
00074   //:set prior message
00075   void set_prior_message(unsigned nq, vcl_vector<float>const& msg);
00076 
00077   //:clear messages
00078   void clear();
00079 
00080   //: print the value of the messages held in the prior queue.
00081   void print_prior_messages();
00082   void print_current_messages();
00083   void print_belief_vector();
00084 
00085  protected:
00086   // parameters for computing message values
00087   float lambda_;
00088   float truncation_cost_;
00089 
00090   //: this index is toggled to swap the buffers
00091   int prior_;
00092 
00093   //: typically 256
00094   unsigned n_labels_;
00095 
00096   //: currently 4, but might change in the future
00097   unsigned n_ngbh_;
00098 
00099   // define the neighbor index
00100   //     0
00101   //   1 x 2
00102   //     3
00103 
00104   //: a set of 2 message buffers, prior and current, one for each neighbor
00105   // (p, c)     n_ngbh_    n_labels_
00106   vcl_vector< vcl_vector<vcl_vector<short> > > msg_;
00107   // cut down storage using short (for byte images should be adequate)
00108 
00109   //: the label represented by the data
00110   float obs_label_;
00111 };
00112 
00113 #include "sdet_mrf_site_bp_sptr.h"
00114 #endif // sdet_mrf_site_bp_h_