contrib/brl/bseg/bbgm/bbgm_measure.h
Go to the documentation of this file.
00001 // This is brl/bseg/bbgm/bbgm_measure.h
00002 #ifndef bbgm_measure_h_
00003 #define bbgm_measure_h_
00004 //:
00005 // \file
00006 // \brief Measurement wrappers for distribution images and probability images
00007 // \author J.L. Mundy
00008 // \date February 10, 2008
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   <none yet>
00013 // \endverbatim
00014 
00015 #include <vcl_cassert.h>
00016 #include <vil/vil_image_view.h>
00017 #include <vbl/vbl_array_2d.h>
00018 #include "bbgm_image_of.h"
00019 #include "bbgm_planes_to_sample.h"
00020 
00021 //: measure a property (typically probability) requiring a tolerance, delta.
00022 // For probability, delta defines a square n-dimensional box over which
00023 // the probability density is integrated. For example, the measure_functor_
00024 // is the bsta_probability_functor.
00025 template <class dist_, class measure_functor_>
00026 void measure(bbgm_image_of<dist_>& dimg,
00027              const vil_image_view<typename dist_::math_type>& image,
00028              vil_image_view<typename dist_::math_type>& result,
00029              const measure_functor_& prop,
00030              typename dist_::math_type delta)
00031 {
00032   typedef typename dist_::vector_type vector_;
00033   typedef typename dist_::math_type T;
00034 
00035   const unsigned ni = dimg.ni();
00036   const unsigned nj = dimg.nj();
00037   const unsigned d_np = dist_::dimension;
00038 
00039   assert(image.ni() == ni);
00040   assert(image.nj() == nj);
00041   assert(image.nplanes() == d_np);
00042 
00043   result.set_size(ni,nj,1);
00044 
00045   const vcl_ptrdiff_t pstep = image.planestep();
00046 
00047   vector_ del(delta);
00048   typename bbgm_image_of<dist_>::iterator itr = dimg.begin();
00049   for ( unsigned j=0; j<nj; ++j)
00050     for ( unsigned i=0; i<ni; ++i, ++itr) {
00051       vector_ sample;
00052       const T* iptr = &image(i,j);
00053       bbgm_planes_to_sample<T, vector_, d_np>::apply(iptr, sample, pstep);
00054       T temp_val;
00055       vector_ r_min = sample-del, r_max = sample+del;
00056       prop(*itr, r_min, r_max, temp_val);
00057       result(i,j) = temp_val;
00058     }
00059 }
00060 
00061 //: measure the probability of background given a fixed foreground probability (pf) at each pixel.
00062 // Bayes rule gives
00063 //
00064 //               p(I|b)P(b)              p(I|b)(1-pf)          p(I|b)(1-pf)
00065 //  P(b) =  ---------------------- = -------------------- =  -----------------
00066 //          p(I|b)P(b) + p(I|f)P(f)  p(I|b)(1-pf)+ p(I|f)pf  p(I|b)(1-pf)+ pf
00067 //
00068 // the foreground probability is assumed to be uniform on the interval (0,1)
00069 //
00070 template <class dist_, class measure_functor_>
00071 void measure_bkground(bbgm_image_of<dist_>& dimg,
00072                       const vil_image_view<typename dist_::math_type>& image,
00073                       vil_image_view<typename dist_::math_type>& result,
00074                       const measure_functor_& prop,
00075                       typename dist_::math_type pf)
00076 {
00077   typedef typename dist_::vector_type vector_;
00078   typedef typename dist_::math_type T;
00079 
00080   const unsigned ni = dimg.ni();
00081   const unsigned nj = dimg.nj();
00082   const unsigned d_np = dist_::dimension;
00083 
00084   assert(image.ni() == ni);
00085   assert(image.nj() == nj);
00086   assert(image.nplanes() == d_np);
00087 
00088   result.set_size(ni,nj,1);
00089 
00090   const vcl_ptrdiff_t pstep = image.planestep();
00091 
00092   typename bbgm_image_of<dist_>::iterator itr = dimg.begin();
00093   for ( unsigned j=0; j<nj; ++j)
00094     for ( unsigned i=0; i<ni; ++i, ++itr) {
00095       vector_ sample;
00096       const T* iptr = &image(i,j);
00097       bbgm_planes_to_sample<T, vector_, d_np>::apply(iptr, sample, pstep);
00098       T temp_val;
00099       prop(*itr, sample, temp_val);
00100       temp_val = temp_val*(T(1)-pf);
00101       temp_val = (temp_val)/(temp_val + pf);
00102       result(i,j) = temp_val;
00103     }
00104 }
00105 
00106 //: measure a property (typically probability) requiring a tolerance, delta.
00107 // For probability, delta defines a square n-dimensional box over which
00108 // the probability density is integrated. For example, the measure_functor_
00109 // is the bsta_probability_functor. In this method the tolerance is
00110 // augmented by an additional value that varies with position in the image
00111 template <class dist_, class measure_functor_>
00112 void measure(bbgm_image_of<dist_>& dimg,
00113              const vil_image_view<typename dist_::math_type>& image,
00114              vil_image_view<typename dist_::math_type>& var,
00115              vil_image_view<typename dist_::math_type>& result,
00116              const measure_functor_& prop,
00117              typename dist_::math_type delta)
00118 {
00119   typedef typename dist_::vector_type vector_;
00120   typedef typename dist_::math_type T;
00121 
00122   const unsigned ni = dimg.ni();
00123   const unsigned nj = dimg.nj();
00124   const unsigned d_np = dist_::dimension;
00125 
00126   assert(image.ni() == ni);
00127   assert(image.nj() == nj);
00128   assert(image.nplanes() == d_np);
00129 
00130   result.set_size(ni,nj,1);
00131 
00132   const vcl_ptrdiff_t pstep = image.planestep();
00133 
00134   vector_ sample;
00135   typename bbgm_image_of<dist_>::iterator itr = dimg.begin();
00136   for ( unsigned j=0; j<nj; ++j)
00137     for ( unsigned i=0; i<ni; ++i, ++itr) {
00138       vector_ sample, var_val;
00139       const T* iptr = &image(i,j);
00140       bbgm_planes_to_sample<T, vector_, d_np>::apply(iptr, sample, pstep);
00141       const T* vptr = &var(i,j);
00142       bbgm_planes_to_sample<T, vector_, d_np>::apply(vptr, var_val, pstep);
00143       T temp_val;
00144       vector_ r_min = sample-delta-var_val, r_max = sample+delta+var_val;
00145       prop(*itr, r_min, r_max, temp_val);
00146       result(i,j) = temp_val;
00147     }
00148 }
00149 
00150 //: measure a property (typically probability) requiring a tolerance, delta.
00151 // For probability, delta defines a square n-dimensional box over which
00152 // the probability density is integrated. As an example, the measure_functor_
00153 // is the bsta_probability_functor. In this method, the covariance of the
00154 // distribution is augmented by and additional amount
00155 template <class dist_, class measure_functor_>
00156 void measure(bbgm_image_of<dist_>& dimg,
00157              const vil_image_view<typename dist_::math_type>& image,
00158              const vbl_array_2d<typename dist_::dist_type::covar_type> & covar,
00159              vil_image_view<typename dist_::math_type>& result,
00160              const measure_functor_& prop,
00161              const typename dist_::math_type delta)
00162 {
00163   typedef typename dist_::vector_type vector_;
00164   typedef typename dist_::math_type T;
00165   typedef typename dist_::dist_type::covar_type covar_t;
00166   const unsigned ni = dimg.ni();
00167   const unsigned nj = dimg.nj();
00168   const unsigned d_np = dist_::dimension;
00169 
00170   assert(image.ni() == ni);
00171   assert(image.nj() == nj);
00172   assert(image.nplanes() == d_np);
00173 
00174   result.set_size(ni,nj,1);
00175 
00176   const vcl_ptrdiff_t pstep = image.planestep();
00177 
00178   vector_ sample;
00179   vector_ del(delta);
00180   typename bbgm_image_of<dist_>::iterator itr = dimg.begin();
00181   for ( unsigned j=0; j<nj; ++j)
00182     for ( unsigned i=0; i<ni; ++i, ++itr) {
00183       vector_ sample;
00184       const T* iptr = &image(i,j);
00185       bbgm_planes_to_sample<T, vector_, d_np>::apply(iptr, sample, pstep);
00186       T temp_val;
00187       vector_ r_min = sample-del, r_max = sample+del;
00188       covar_t add_cov = covar[j][i];
00189       prop(*itr, r_min, r_max, add_cov, temp_val);
00190       result(i,j) = temp_val;
00191     }
00192 }
00193 
00194 //: measure a property (typically probability) requiring a tolerance.
00195 // The tolerance is defined by a minimum and maximum values
00196 // For probability, min and max defines a square n-dimensional box over which
00197 // the probability density is integrated. This functor assumes that the same
00198 // box applies to the entire distribution image. An example is probability of
00199 // area lying within an interval, where the distribution image is a
00200 // 2-d array of area probability densities.
00201 template <class dist_, class measure_functor_>
00202 void measure(bbgm_image_of<dist_>& dimg,
00203              vil_image_view<typename dist_::math_type>& result,
00204              const measure_functor_& prop,
00205              typename dist_::vector_type min_value,
00206              typename dist_::vector_type max_value)
00207 {
00208   typedef typename dist_::vector_type vector_;
00209   typedef typename dist_::math_type T;
00210 
00211   const unsigned ni = dimg.ni();
00212   const unsigned nj = dimg.nj();
00213 
00214   result.set_size(ni,nj,1);
00215 
00216   typename bbgm_image_of<dist_>::iterator itr = dimg.begin();
00217   for ( unsigned j=0; j<nj; ++j)
00218     for ( unsigned i=0; i<ni; ++i, ++itr) {
00219       T temp_val;
00220       prop(*itr, min_value, max_value, temp_val);
00221       result(i,j) = temp_val;
00222     }
00223 }
00224 
00225 #endif // bbgm_measure_h_