contrib/brl/bseg/bbgm/bbgm_detect.h
Go to the documentation of this file.
00001 // This is brl/bseg/bbgm/bbgm_detect.h
00002 #ifndef bbgm_detect_h_
00003 #define bbgm_detect_h_
00004 //:
00005 // \file
00006 // \brief Detection wrappers in distribution images
00007 // \author Matt Leotta (mleotta@lems.brown.edu)
00008 // \date October 3, 2005
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   (none yet)
00013 // \endverbatim
00014 
00015 #include <vpdl/vpdt/vpdt_field_traits.h>
00016 #include <vil/algo/vil_structuring_element.h>
00017 #include <vil/vil_image_view.h>
00018 #include "bbgm_image_of.h"
00019 #include "bbgm_planes_to_sample.h"
00020 #include <bsta/bsta_detector_mixture.h>
00021 #include <vcl_cassert.h>
00022 
00023 //: For each pixel, detect at all \a se neighbors in bbgm_image
00024 //  \returns true if detection succeeds at any neighbor
00025 template <class dist_, class detector_, class dT>
00026 void detect(bbgm_image_of<dist_>& dimg,
00027             const vil_image_view<dT>& data,
00028             vil_image_view<bool>& result,
00029             const detector_& detector,
00030             const vil_structuring_element& se)
00031 {
00032   typedef typename dist_::field_type F;
00033 
00034   const unsigned ni = dimg.ni();
00035   const unsigned nj = dimg.nj();
00036   const unsigned d_np = vpdt_field_traits<F>::dimension;
00037   assert(data.ni() == ni);
00038   assert(data.nj() == nj);
00039   assert(data.nplanes() == d_np);
00040 
00041   result.set_size(ni,nj,1);
00042   const vcl_ptrdiff_t r_istep = result.istep();
00043   const vcl_ptrdiff_t r_jstep = result.jstep();
00044   const vcl_ptrdiff_t d_istep = data.istep();
00045   const vcl_ptrdiff_t d_jstep = data.jstep();
00046   const vcl_ptrdiff_t d_pstep = data.planestep();
00047 
00048   const unsigned size_se = se.p_i().size();
00049 
00050   bool temp_val;
00051   F sample;
00052   bool* r_row = result.top_left_ptr();
00053   const dT* d_row = data.top_left_ptr();
00054   for (unsigned int j=0; j<nj; ++j, d_row+=d_jstep, r_row+=r_jstep){
00055     bool* r_col = r_row;
00056     const dT* d_col = d_row;
00057     for (unsigned int i=0; i<ni; ++i, d_col+=d_istep, r_col+=r_istep){
00058       bool& detected = *r_col;
00059       const dT* d_plane = d_col;
00060       for (unsigned int k=0; k<d_np; ++k, d_plane+=d_pstep)
00061         sample[k] = *d_plane;
00062       detected = false;
00063       for (unsigned int k=0; k<size_se; ++k){
00064         int ri = static_cast<int>(i)+se.p_i()[k];
00065         int rj = static_cast<int>(j)+se.p_j()[k];
00066         if (ri < 0 || ri >= static_cast<int>(ni) ||
00067             rj < 0 || rj >= static_cast<int>(nj) )
00068           continue;
00069         if (detector(dimg(ri,rj), sample, temp_val) && temp_val){
00070           detected = true;
00071           break;
00072         }
00073       }
00074     }
00075   }
00076 }
00077 
00078 //: For each masked pixel, detect at all \a se neighbors in bbgm_image
00079 // \returns true if detection succeeds at any neighbor
00080 template <class dist_, class detector_, class dT>
00081 void detect_masked(bbgm_image_of<dist_>& dimg,
00082                    const vil_image_view<dT>& data,
00083                    vil_image_view<bool>& result,
00084                    const detector_& detector,
00085                    const vil_structuring_element& se,
00086                    const vil_image_view<bool>& mask)
00087 {
00088   typedef typename dist_::field_type F;
00089 
00090   const unsigned ni = dimg.ni();
00091   const unsigned nj = dimg.nj();
00092   const unsigned d_np = vpdt_field_traits<F>::dimension;
00093   assert(data.ni() == ni);
00094   assert(data.nj() == nj);
00095   assert(data.nplanes() == d_np);
00096   assert(mask.ni() == ni);
00097   assert(mask.nj() == nj);
00098 
00099   result.set_size(ni,nj,1);
00100   const vcl_ptrdiff_t r_istep = result.istep();
00101   const vcl_ptrdiff_t r_jstep = result.jstep();
00102   const vcl_ptrdiff_t d_istep = data.istep();
00103   const vcl_ptrdiff_t d_jstep = data.jstep();
00104   const vcl_ptrdiff_t d_pstep = data.planestep();
00105   const vcl_ptrdiff_t m_istep = mask.istep();
00106   const vcl_ptrdiff_t m_jstep = mask.jstep();
00107 
00108   const unsigned size_se = se.p_i().size();
00109 
00110   bool temp_val;
00111   F sample;
00112   bool* r_row = result.top_left_ptr();
00113   const dT* d_row = data.top_left_ptr();
00114   const bool* m_row = mask.top_left_ptr();
00115   for (unsigned int j=0; j<nj; ++j, d_row+=d_jstep, r_row+=r_jstep, m_row+=m_jstep){
00116     bool* r_col = r_row;
00117     const dT* d_col = d_row;
00118     const bool* m_col = m_row;
00119     for (unsigned int i=0; i<ni; ++i, d_col+=d_istep, r_col+=r_istep, m_col+=m_istep){
00120       if (!*m_col)
00121         continue;
00122       bool& detected = *r_col;
00123       const dT* d_plane = d_col;
00124       for (unsigned int k=0; k<d_np; ++k, d_plane+=d_pstep)
00125         sample[k] = *d_plane;
00126       detected = false;
00127       for (unsigned int k=0; k<size_se; ++k){
00128         int ri = static_cast<int>(i)+se.p_i()[k];
00129         int rj = static_cast<int>(j)+se.p_j()[k];
00130         if (ri < 0 || ri >= ni || rj < 0 || rj >= nj)
00131           continue;
00132         if (detector(dimg(ri,rj), sample, temp_val) && temp_val){
00133           detected = true;
00134           break;
00135         }
00136       }
00137     }
00138   }
00139 }
00140 
00141 
00142 template <class dist_, class detector_>
00143 void detect(bbgm_image_of<dist_>& dimg,
00144             const vil_image_view<typename dist_::math_type>& image,
00145             vil_image_view<bool>& result,
00146             const detector_& detector,
00147             int rad)
00148 {
00149     typedef typename dist_::vector_type vector_;
00150     typedef typename dist_::math_type T;
00151 
00152     const unsigned ni = dimg.ni();
00153     const unsigned nj = dimg.nj();
00154     const unsigned d_np = dist_::dimension;
00155     assert(image.ni() == ni);
00156     assert(image.nj() == nj);
00157     assert(image.nplanes() == d_np);
00158 
00159     result.set_size(ni,nj,1);
00160 
00161     const vcl_ptrdiff_t r_istep = result.istep();
00162     const vcl_ptrdiff_t r_jstep = result.jstep();
00163     const vcl_ptrdiff_t d_istep = image.istep();
00164     const vcl_ptrdiff_t d_jstep = image.jstep();
00165     const vcl_ptrdiff_t d_pstep = image.planestep();
00166 
00167     bool temp_val;
00168     vector_ sample;
00169 
00170     typename bbgm_image_of<dist_>::iterator itr = dimg.begin();
00171     bool* r_row = result.top_left_ptr();
00172     const T* d_row = image.top_left_ptr();
00173     for ( int j=0; j<nj; ++j, d_row+=d_jstep, r_row+=r_jstep){
00174         bool* r_col = r_row;
00175         const T* d_col = d_row;
00176         for ( int i=0; i<ni; ++i, d_col+=d_istep, r_col+=r_istep, ++itr){
00177             bool flag=false;
00178             for (int l=-rad;l<=rad;l++)
00179             {
00180                 for (int k=-rad;k<=rad;k++)
00181                 {
00182                     if (l+i>=0 && l+i<ni && k+j>=0 && k+j<nj)
00183                     {
00184                         const T * d_plane=&image(l+i,k+j);
00185                         bbgm_planes_to_sample<T,vector_,dist_::dimension>::apply(d_plane,sample,d_pstep);
00186                         if (detector(*itr, sample, temp_val))
00187                             if (temp_val)
00188                                 flag=true;
00189                     }
00190                 }
00191             }
00192             *r_col=flag;
00193         }
00194     }
00195 }
00196 
00197 
00198 template <class dist_, class detector_>
00199 void detect_masked(bbgm_image_of<dist_>& dimg,
00200                    const vil_image_view<typename dist_::math_type>& image,
00201                    vil_image_view<bool>& result,
00202                    const detector_& detector,
00203                    int rad,vil_image_view<bool>& mask)
00204 {
00205     typedef typename dist_::vector_type vector_;
00206     typedef typename dist_::math_type T;
00207 
00208     const unsigned ni = dimg.ni();
00209     const unsigned nj = dimg.nj();
00210     const unsigned d_np = dist_::dimension;
00211     assert(image.ni() == ni);
00212     assert(image.nj() == nj);
00213     assert(image.nplanes() == d_np);
00214 
00215     result.set_size(ni,nj,1);
00216 
00217     const vcl_ptrdiff_t r_istep = result.istep();
00218     const vcl_ptrdiff_t r_jstep = result.jstep();
00219     const vcl_ptrdiff_t d_istep = image.istep();
00220     const vcl_ptrdiff_t d_jstep = image.jstep();
00221     const vcl_ptrdiff_t d_pstep = image.planestep();
00222 
00223     const vcl_ptrdiff_t m_istep = mask.istep();
00224     const vcl_ptrdiff_t m_jstep = mask.jstep();
00225 
00226 
00227     bool temp_val;
00228     vector_ sample;
00229 
00230     typename bbgm_image_of<dist_>::iterator itr = dimg.begin();
00231     bool* r_row = result.top_left_ptr();
00232     const T* d_row = image.top_left_ptr();
00233     bool * m_row = mask.top_left_ptr();
00234 
00235     for ( int j=0; j<nj; ++j, d_row+=d_jstep, r_row+=r_jstep,m_row+=m_jstep){
00236         bool* r_col = r_row;
00237         const T* d_col = d_row;
00238         bool * m_col = m_row;
00239 
00240         for ( int i=0; i<ni; ++i, d_col+=d_istep, r_col+=r_istep,m_col+=m_istep, ++itr)
00241         {
00242             if (*m_col)
00243             {
00244             bool flag=false;
00245             for (int l=-rad;l<=rad;l++)
00246             {
00247                 for (int k=-rad;k<=rad;k++)
00248                 {
00249                     if (l+i>=0 && l+i<ni && k+j>=0 && k+j<nj)
00250                     {
00251                         if (mask(l+i,k+j))
00252                         {
00253                         const T * d_plane=&image(l+i,k+j);
00254 
00255                         bbgm_planes_to_sample<T,vector_,dist_::dimension>::apply(d_plane,sample,d_pstep);
00256                         if (detector(*itr, sample, temp_val))
00257                             if (temp_val)
00258                                 flag=true;
00259                         }
00260                     }
00261                 }
00262             }
00263             *r_col=flag;
00264         }
00265             else
00266                 *r_col=true;
00267         }
00268     }
00269 }
00270 
00271 
00272 template <class dist_, class detector_, class rT>
00273 void detect_masked(bbgm_image_of<dist_>& dimg,
00274                    const vil_image_view<typename dist_::math_type>& image,
00275                    vil_image_view<rT>& result,
00276                    vil_image_view<rT>& mask,
00277                    const detector_& detector)
00278 {
00279     typedef typename dist_::vector_type vector_;
00280     typedef typename dist_::math_type T;
00281 
00282     const unsigned ni = dimg.ni();
00283     const unsigned nj = dimg.nj();
00284     const unsigned d_np = dist_::dimension;
00285     assert(image.ni() == ni);
00286     assert(image.nj() == nj);
00287     assert(image.nplanes() == d_np);
00288 
00289     result.set_size(ni,nj,1);
00290 
00291     const vcl_ptrdiff_t r_istep = result.istep();
00292     const vcl_ptrdiff_t r_jstep = result.jstep();
00293 
00294     const vcl_ptrdiff_t m_istep = mask.istep();
00295     const vcl_ptrdiff_t m_jstep = mask.jstep();
00296 
00297     const vcl_ptrdiff_t d_istep = image.istep();
00298     const vcl_ptrdiff_t d_jstep = image.jstep();
00299     const vcl_ptrdiff_t d_pstep = image.planestep();
00300 
00301     rT temp_val;
00302     vector_ sample;
00303 
00304     typename bbgm_image_of<dist_>::iterator itr = dimg.begin();
00305     rT* r_row = result.top_left_ptr();
00306     rT* m_row = mask.top_left_ptr();
00307     const T* d_row = image.top_left_ptr();
00308     for (unsigned int j=0; j<nj; ++j, d_row+=d_jstep, r_row+=r_jstep,m_row+=m_jstep){
00309         rT* r_col = r_row;
00310         rT* m_col = m_row;
00311         const T* d_col = d_row;
00312         for (unsigned int i=0; i<ni; ++i, d_col+=d_istep, r_col+=r_istep,m_col+=m_istep, ++itr){
00313             const T* d_plane = d_col;
00314             if (*m_col)
00315             {
00316             bbgm_planes_to_sample<T,vector_,dist_::dimension>::apply(d_plane,sample,d_pstep);
00317             if (detector(*itr, sample, temp_val))
00318                 *r_col =temp_val;
00319             }
00320     }
00321   }
00322 }
00323 
00324 #endif // bbgm_detect_h_