contrib/brl/bseg/sdet/sdet_mrf_site_bp.cxx
Go to the documentation of this file.
00001 #include "sdet_mrf_site_bp.h"
00002 //:
00003 // \file
00004 
00005 #include <vnl/vnl_numeric_traits.h>
00006 #include <vcl_iostream.h>
00007 #include <vcl_string.h>
00008 #include <vcl_cmath.h>
00009 
00010 sdet_mrf_site_bp::sdet_mrf_site_bp(unsigned n_labels, float lambda, float truncation_cost)
00011   : lambda_(lambda), truncation_cost_(truncation_cost), prior_(0),
00012     n_labels_(n_labels), n_ngbh_(4), obs_label_(0.0f)
00013 {
00014   msg_.resize(2);
00015   msg_[0].resize(n_ngbh_);
00016   msg_[1].resize(n_ngbh_);
00017   for (unsigned n = 0; n<n_ngbh_; ++n)
00018   {
00019     msg_[0][n].resize(n_labels_, 0);
00020     msg_[1][n].resize(n_labels_, 0);
00021   }
00022 }
00023 
00024 // the mrf data term
00025 float sdet_mrf_site_bp::D(unsigned fp)
00026 {
00027   float tmp = (static_cast<float>(fp) - obs_label_);
00028   tmp *= tmp;
00029   if (tmp<truncation_cost_)
00030     return lambda_*tmp;
00031   return lambda_*truncation_cost_;
00032 }
00033 
00034 // the sum of prior messages for fp except for q
00035 // normalized by the number of incoming neighbors
00036 float sdet_mrf_site_bp::M(unsigned nq, unsigned fp)
00037 {
00038   float sum = 0.0f;
00039   for (unsigned n = 0; n<n_ngbh_; ++n)
00040     if (n!=nq)
00041       sum += msg_[prior_][n][fp];
00042   return sum;
00043 }
00044 
00045 void sdet_mrf_site_bp::set_cur_message(unsigned nq, unsigned fp, float msg)
00046 {
00047   msg_[1-prior_][nq][fp] = static_cast<short>(msg);
00048 }
00049 
00050 vcl_vector<float> sdet_mrf_site_bp::prior_message(unsigned nq)
00051 {
00052   vcl_vector<float> temp;
00053   for (unsigned i = 0; i< msg_[prior_][nq].size(); i++)
00054   temp.push_back(msg_[prior_][nq][i]);
00055   return temp;
00056 }
00057 
00058 void sdet_mrf_site_bp::print_prior_messages()
00059 {
00060   for (unsigned n = 0; n<4; ++n){
00061     vcl_cout << "Prior message from neighbor " << n << '\n';
00062     for (unsigned fp = 0; fp<n_labels_; ++fp)
00063       vcl_cout << msg_[prior_][n][fp] << ' ';
00064     vcl_cout << "\n\n";
00065   }
00066 }
00067 
00068 void sdet_mrf_site_bp::print_current_messages()
00069 {
00070   for (unsigned n = 0; n<4; ++n){
00071     vcl_cout << "Current message from neighbor " << n << '\n';
00072     for (unsigned fp = 0; fp<n_labels_; ++fp)
00073       vcl_cout << msg_[1-prior_][n][fp] << ' ';
00074     vcl_cout << "\n\n";
00075   }
00076 }
00077 
00078 float sdet_mrf_site_bp::b(unsigned fp)
00079 {
00080   float sum = 0.0f;
00081   for (unsigned n = 0; n<4; ++n)
00082     sum += msg_[prior_][n][fp];
00083   return D(fp) + sum;
00084 }
00085 
00086 unsigned sdet_mrf_site_bp::believed_label()
00087 {
00088   unsigned lmax =0;
00089   float mmin = vnl_numeric_traits<float>::maxval;
00090   for (unsigned d = 0; d<n_labels_; ++d)
00091     if (b(d)<mmin){
00092       mmin = b(d);
00093       lmax = d;
00094     }
00095   return lmax;
00096 }
00097 
00098 void sdet_mrf_site_bp::print_belief_vector()
00099 {
00100   vcl_cout << "Belief\n";
00101   for (unsigned fp = 0; fp<n_labels_; ++fp) {
00102     vcl_cout << this->b(fp) << ' ';
00103   }
00104   vcl_cout << '\n';
00105 }
00106 
00107 void sdet_mrf_site_bp::clear()
00108 {
00109   for (unsigned p = 0; p<=1; ++p)
00110     for (unsigned n = 0; n<4; ++n)
00111       for (unsigned l =0; l<n_labels_; ++l)
00112         msg_[p][n][l] = 0;
00113 }
00114 
00115 //set the prior message
00116 void sdet_mrf_site_bp::set_prior_message(unsigned nq,
00117                                          vcl_vector<float>const& msg)
00118 {
00119   for (unsigned i = 0; i<msg.size(); ++i)
00120     msg_[prior_][nq][i]=static_cast<short>(msg[i]);
00121 }