Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

vimt_find_peaks.h

Go to the documentation of this file.
00001 // This is mul/vimt/algo/vimt_find_peaks.h
00002 #ifndef vimt_find_peaks_h_
00003 #define vimt_find_peaks_h_
00004 //:
00005 // \file
00006 // \brief Find peaks in image
00007 // \author Tim Cootes, VP (Sept03)
00008 
00009 #include <vimt/vimt_image_2d_of.h>
00010 
00011 //: True if pixel at *im is strictly above 8 neighbours
00012 template <class T>
00013 inline bool vimt_is_peak_3x3(const T* im, vcl_ptrdiff_t i_step, vcl_ptrdiff_t j_step)
00014 {
00015   T v = *im;
00016   return v>im[i_step] &&
00017          v>im[-i_step] &&
00018          v>im[j_step] &&
00019          v>im[-j_step] &&
00020          v>im[i_step+j_step] &&
00021          v>im[i_step-j_step] &&
00022          v>im[j_step-i_step] &&
00023          v>im[-i_step-j_step];
00024 }
00025 
00026 //: True if pixel at *im is strictly above its neighbours in a 2*radius+1 neighbourhood
00027 template <class T>
00028 inline bool vimt_is_peak(const T* im, int radius, vcl_ptrdiff_t i_step, vcl_ptrdiff_t j_step)
00029 {
00030   T v = *im;
00031   for (int i=-radius; i<radius+1; i++)
00032     for (int j=-radius; j<radius+1; j++)
00033       if (i!=0 || j!=0)
00034         if (v<=im[i_step*i+j_step*j]) return false;      // One of the
00035   return true;
00036 }
00037 
00038 
00039 //: Return image co-ordinates of all points in image strictly above their 8 neighbours
00040 // \param clear_list: If true (the default) then empty list before adding new examples
00041 template <class T>
00042 inline void vimt_find_image_peaks_3x3(vcl_vector<vgl_point_2d<unsigned> >& peaks,
00043                                       const vil_image_view<T>& image,
00044                                       unsigned plane=0, bool clear_list=true)
00045 {
00046   if (clear_list) peaks.resize(0);
00047   unsigned ni=image.ni(),nj=image.nj();
00048   vcl_ptrdiff_t istep = image.istep(),jstep=image.jstep();
00049   const T* row = image.top_left_ptr()+plane*image.planestep()+istep+jstep;
00050   for (unsigned j=1;j<nj-1;++j,row+=jstep)
00051   {
00052     const T* pixel = row;
00053     for (unsigned i=1;i<ni-1;++i,pixel+=istep)
00054       if (vimt_is_peak_3x3(pixel,istep,jstep)) peaks.push_back(vgl_point_2d<unsigned>(i,j));
00055   }
00056 }
00057 
00058 //: Return image co-ordinates of all points in image strictly above their 8 neighbours
00059 // \param peak_value: Value at peak
00060 // \param clear_list: If true (the default) then empty list before adding new examples
00061 template <class T>
00062 inline void vimt_find_image_peaks_3x3(vcl_vector<vgl_point_2d<unsigned> >& peaks,
00063                                       vcl_vector<T>& peak_value,
00064                                       const vil_image_view<T>& image,
00065                                       unsigned plane=0, bool clear_list=true)
00066 {
00067   if (clear_list) { peaks.resize(0); peak_value.resize(0); }
00068   unsigned ni=image.ni(),nj=image.nj();
00069   vcl_ptrdiff_t istep = image.istep(),jstep=image.jstep();
00070   const T* row = image.top_left_ptr()+plane*image.planestep()+istep+jstep;
00071   for (unsigned j=1;j<nj-1;++j,row+=jstep)
00072   {
00073     const T* pixel = row;
00074     for (unsigned i=1;i<ni-1;++i,pixel+=istep)
00075       if (vimt_is_peak_3x3(pixel,istep,jstep))
00076       {
00077         peaks.push_back(vgl_point_2d<unsigned>(i,j));
00078         peak_value.push_back(*pixel);
00079       }
00080   }
00081 }
00082 
00083 //: Return image co-ordinates of all points in image strictly above their neighbours
00084 //  In a 2*radius+1 x 2*radius+1 neighbourhood of pixels (e.g. r=2 equivalent to 5x5; default: r=1)
00085 // \param peak_value: Value at peak
00086 // \param clear_list: If true (the default) then empty list before adding new examples
00087 template <class T>
00088 inline void vimt_find_image_peaks(vcl_vector<vgl_point_2d<unsigned> >& peaks,
00089                                   vcl_vector<T>& peak_value,
00090                                   const vil_image_view<T>& image,
00091                                   unsigned radius=1,
00092                                   unsigned plane=0, bool clear_list=true)
00093 {
00094   if (clear_list) { peaks.resize(0); peak_value.resize(0); }
00095   unsigned ni=image.ni(),nj=image.nj();
00096   vcl_ptrdiff_t istep = image.istep(),jstep=image.jstep();
00097   const T* row = image.top_left_ptr()+plane*image.planestep()+radius*istep+radius*jstep;
00098   for (unsigned j=radius;j<nj-radius;++j,row+=jstep)
00099   {
00100     const T* pixel = row;
00101     for (unsigned i=radius;i<ni-radius;++i,pixel+=istep)
00102       if (vimt_is_peak(pixel,radius,istep,jstep))
00103       {
00104         peaks.push_back(vgl_point_2d<unsigned>(i,j));
00105         peak_value.push_back(*pixel);
00106       }
00107   }
00108 }
00109 
00110 //: Return image co-ordinates of all points in image strictly above their neighbours
00111 //  In a 2*radius+1 x 2*radius+1 neighbourhood of pixels (e.g. r=2 equivalent to 5x5; default: r=1)
00112 //  Additionally, only peaks of the value higher than threshold (thresh) are returned.
00113 // \param peak_value: Value at peak
00114 // \param clear_list: If true (the default) then empty list before adding new examples
00115 template <class T>
00116 inline void vimt_find_image_peaks(vcl_vector<vgl_point_2d<unsigned> >& peaks,
00117                                   vcl_vector<T>& peak_value,
00118                                   const vil_image_view<T>& image,
00119                                   T thresh, unsigned radius=1,
00120                                   unsigned plane=0, bool clear_list=true)
00121 {
00122   if (clear_list) { peaks.resize(0); peak_value.resize(0); }
00123   unsigned ni=image.ni(),nj=image.nj();
00124   vcl_ptrdiff_t istep = image.istep(),jstep=image.jstep();
00125   // Getting to the location of the starting point in the image (radius,radius)
00126   const T* row = image.top_left_ptr()+plane*image.planestep()+radius*istep+radius*jstep;
00127   for (unsigned j=radius;j<nj-radius;++j,row+=jstep)
00128   {
00129     const T* pixel = row;
00130     for (unsigned i=radius;i<ni-radius;++i,pixel+=istep)
00131     {
00132       if (*pixel>thresh)
00133       {
00134         if (vimt_is_peak(pixel,radius,istep,jstep))
00135         {
00136           peaks.push_back(vgl_point_2d<unsigned>(i,j));
00137           peak_value.push_back(*pixel);
00138         }
00139       }
00140     }
00141   }
00142 }
00143 
00144 //: Return world co-ordinates of all points in image strictly above their 8 neighbours
00145 // \param clear_list: If true (the default) then empty list before adding new examples
00146 template <class T>
00147 inline void vimt_find_world_peaks_3x3(vcl_vector<vgl_point_2d<double> >& peaks,
00148                                       const vimt_image_2d_of<T>& image,
00149                                       unsigned plane=0, bool clear_list=true)
00150 {
00151   if (clear_list) peaks.resize(0);
00152   const vil_image_view<T>& im = image.image();
00153   vimt_transform_2d im2w = image.world2im().inverse();
00154   unsigned ni=im.ni(),nj=im.nj();
00155   vcl_ptrdiff_t istep = im.istep(),jstep=im.jstep();
00156   const T* row = im.top_left_ptr()+plane*im.planestep()+istep+jstep;
00157   for (unsigned j=1;j<nj-1;++j,row+=jstep)
00158   {
00159     const T* pixel = row;
00160     for (unsigned i=1;i<ni-1;++i,pixel+=istep)
00161       if (vimt_is_peak_3x3(pixel,istep,jstep)) peaks.push_back(im2w(i,j));
00162   }
00163 }
00164 
00165 //: Return world co-ordinates of all points in image strictly above their 8 neighbours
00166 // \param peak_pos: Position of each peak
00167 // \param peak_value: Value at peak
00168 // \param clear_list: If true (the default) then empty list before adding new examples
00169 template <class T>
00170 inline void vimt_find_world_peaks_3x3(vcl_vector<vgl_point_2d<double> >& peak_pos,
00171                                       vcl_vector<T>& peak_value,
00172                                       const vimt_image_2d_of<T>& image,
00173                                       unsigned plane=0, bool clear_list=true)
00174 {
00175   if (clear_list) { peak_pos.resize(0); peak_value.resize(0); }
00176   const vil_image_view<T>& im = image.image();
00177   vimt_transform_2d im2w = image.world2im().inverse();
00178   unsigned ni=im.ni(),nj=im.nj();
00179   vcl_ptrdiff_t istep = im.istep(),jstep=im.jstep();
00180   const T* row = im.top_left_ptr()+plane*im.planestep()+istep+jstep;
00181   for (unsigned j=1;j<nj-1;++j,row+=jstep)
00182   {
00183     const T* pixel = row;
00184     for (unsigned i=1;i<ni-1;++i,pixel+=istep)
00185       if (vimt_is_peak_3x3(pixel,istep,jstep))
00186       {
00187         peak_pos.push_back(im2w(i,j));
00188         peak_value.push_back(*pixel);
00189       }
00190   }
00191 }
00192 
00193 //: Return image co-ordinates of maximum value in image
00194 //  (Or first one found if multiple equivalent maxima)
00195 template <class T>
00196 inline
00197 vgl_point_2d<unsigned> vimt_find_max(const vil_image_view<T>& im, unsigned plane=0)
00198 {
00199   vgl_point_2d<unsigned> p(0,0);
00200   T max_val = im(0,0,plane);
00201   unsigned ni=im.ni(),nj=im.nj();
00202   vcl_ptrdiff_t istep = im.istep(),jstep=im.jstep();
00203   const T* row = im.top_left_ptr()+plane*im.planestep();
00204   for (unsigned j=0;j<nj;++j,row+=jstep)
00205   {
00206     const T* pixel = row;
00207     for (unsigned i=0;i<ni;++i,pixel+=istep)
00208       if (*pixel>max_val)
00209       {
00210         max_val = *pixel;
00211         p = vgl_point_2d<unsigned>(i,j);
00212       }
00213   }
00214   return p;
00215 }
00216 
00217 //: Return world co-ordinates of maximum value in image
00218 //  (Or first one found if multiple equivalent maxima)
00219 template <class T>
00220 inline
00221 vgl_point_2d<double> vimt_find_max(const vimt_image_2d_of<T>& image,unsigned plane=0)
00222 {
00223   vgl_point_2d<unsigned> im_p = vimt_find_max(image.image(),plane);
00224   return image.world2im().inverse()(im_p.x(),im_p.y());
00225 }
00226 
00227 
00228 #endif // vimt_find_peaks_h_

Generated on Thu Jan 10 14:43:58 2008 for contrib/mul/vimt by  doxygen 1.4.4