00001
00002 #ifndef vimt_find_peaks_h_
00003 #define vimt_find_peaks_h_
00004
00005
00006
00007
00008
00009 #include <vimt/vimt_image_2d_of.h>
00010
00011
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
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;
00035 return true;
00036 }
00037
00038
00039
00040
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
00059
00060
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
00084
00085
00086
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
00111
00112
00113
00114
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
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
00145
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
00166
00167
00168
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
00194
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
00218
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_