contrib/mul/vil3d/algo/vil3d_find_peaks.h
Go to the documentation of this file.
00001 // This is mul/vil3d/algo/vil3d_find_peaks.h
00002 #ifndef vil3d_find_peaks_h_
00003 #define vil3d_find_peaks_h_
00004 //:
00005 // \file
00006 // \brief Find peaks in image
00007 // \author Tim Cootes
00008 
00009 #include <vil3d/vil3d_image_view.h>
00010 #include <vgl/vgl_point_3d.h>
00011 #include <vcl_vector.h>
00012 
00013 //: True if value v is strictly above 8 neighbours of *im in i and j
00014 template <class T>
00015 inline bool vil3d_is_above_8nbrs(T v, const T* im, vcl_ptrdiff_t i_step, vcl_ptrdiff_t j_step)
00016 {
00017   if (v<=im[i_step]) return false;
00018   if (v<=im[-i_step]) return false;
00019   if (v<=im[j_step]) return false;
00020   if (v<=im[-j_step]) return false;
00021   if (v<=im[i_step+j_step]) return false;
00022   if (v<=im[i_step-j_step]) return false;
00023   if (v<=im[j_step-i_step]) return false;
00024   if (v<=im[-i_step-j_step]) return false;
00025   return true;
00026 }
00027 
00028 //: True if *im is strictly above 26 neighbours in i,j,k
00029 template <class T>
00030 inline bool vil3d_is_peak26(const T* im, vcl_ptrdiff_t i_step,
00031                             vcl_ptrdiff_t j_step, vcl_ptrdiff_t k_step)
00032 {
00033   if (!vil3d_is_above_8nbrs(im[0],im,i_step,j_step)) return false;
00034   if (*im<=im[k_step]) return false;
00035   if (!vil3d_is_above_8nbrs(im[0],im+k_step,i_step,j_step)) return false;
00036   if (*im<=im[-k_step]) return false;
00037   if (!vil3d_is_above_8nbrs(im[0],im-k_step,i_step,j_step)) return false;
00038   return true;
00039 }
00040 
00041 //: Return position of all points in image strictly above their 26 neighbours
00042 //  Compute position of all local peaks (pi[k],pj[k]) above given threshold value.
00043 // \param clear_list  If true (the default) then empty lists before adding new examples
00044 template <class T>
00045 inline void vil3d_find_peaks_26(vcl_vector<vgl_point_3d<int> >& peaks,
00046                                const vil3d_image_view<T>& image,
00047                                const T& min_thresh,
00048                                bool clear_list=true)
00049 {
00050   if (clear_list) peaks.resize(0);
00051 
00052   const unsigned ni1=image.ni()-1,nj1=image.nj()-1,nk1=image.nk()-1;
00053   const vcl_ptrdiff_t istep = image.istep(),jstep=image.jstep(),kstep=image.kstep();
00054   const T* plane = image.origin_ptr()+istep+jstep+kstep;
00055   for (unsigned k=1;k<nk1;++k,plane+=kstep)
00056   {
00057     const T* row = plane;
00058     for (unsigned j=1;j<nj1;++j,row+=jstep)
00059     {
00060       const T* pixel = row;
00061       for (unsigned i=1;i<ni1;++i,pixel+=istep)
00062         if (*pixel>=min_thresh && vil3d_is_peak26(pixel,istep,jstep,kstep))
00063         { peaks.push_back(vgl_point_3d<int>(i,j,k)); }
00064     }
00065   }
00066 }
00067 
00068 #endif // vil3d_find_peaks_h_