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

vimt_sample_profile_bilin.txx

Go to the documentation of this file.
00001 // This is mul/vimt/vimt_sample_profile_bilin.txx
00002 #ifndef vimt_sample_profile_bilin_txx_
00003 #define vimt_sample_profile_bilin_txx_
00004 //:
00005 //  \file
00006 //  \brief Profile sampling functions for 2D images
00007 //  \author Tim Cootes
00008 
00009 #include "vimt_sample_profile_bilin.h"
00010 #include <vil/vil_sample_profile_bilin.h>
00011 #include <vil/vil_bilin_interp.h>
00012 #include <vnl/vnl_vector.h>
00013 #include <vgl/vgl_vector_2d.h>
00014 
00015 inline bool vimt_profile_in_image(const vgl_point_2d<double>& p0,
00016                                   const vgl_point_2d<double>& p1,
00017                                   const vil_image_view_base& image)
00018 {
00019   return p0.x()>=0 && p0.x()<=image.ni()-1 &&
00020          p0.y()>=0 && p0.y()<=image.nj()-1 &&
00021          p1.x()>=0 && p1.x()<=image.ni()-1 &&
00022          p1.y()>=0 && p1.y()<=image.nj()-1;
00023 }
00024 
00025 //: Sample along profile, using bilinear interpolation.
00026 //  Profile points are p+i*u, where i=[0..n-1].
00027 //  Vector v is resized to n*np elements, where np=image.n_planes().
00028 //  v[0]..v[np-1] are the values from point p
00029 template <class imType, class vecType>
00030 void vimt_sample_profile_bilin(vnl_vector<vecType>& vec,
00031                                const vimt_image_2d_of<imType>& image,
00032                                const vgl_point_2d<double>& p0,
00033                                const vgl_vector_2d<double>& u,
00034                                int n)
00035 {
00036   // Check that all the profile points are within the image.
00037   vgl_point_2d<double> im_p0 = image.world2im()(p0);
00038   vgl_point_2d<double> im_p1 = image.world2im()(p0+(n-1)*u);
00039   int np = image.image().nplanes();
00040   vec.set_size(n*np);
00041   vecType *v = vec.data_block();
00042 
00043   if (image.world2im().form()!=vimt_transform_2d::Projective)
00044   {
00045     // Can do all work in image co-ordinates under an affine transformation
00046     double dx = (im_p1.x()-im_p0.x())/(n-1);
00047     double dy = (im_p1.y()-im_p0.y())/(n-1);
00048 
00049     // Sample along profile between im_p0 and im_p1
00050     vil_sample_profile_bilin(v,image.image(),im_p0.x(),im_p0.y(),dx,dy,n);
00051     return;
00052   }
00053 
00054   // Otherwise do more fiddly projective calculations
00055 
00056   vgl_point_2d<double> im_p, p=p0;
00057   const vimt_transform_2d& w2i = image.world2im();
00058 
00059   if (vimt_profile_in_image(im_p0,im_p1,image.image_base()))
00060   {
00061     if (np==1)
00062     {
00063       for (int i=0;i<n;++i,p+=u)
00064       {
00065         im_p = w2i(p);
00066         v[i] = vil_bilin_interp(image.image(),im_p.x(),im_p.y());
00067       }
00068     }
00069     else
00070     {
00071       for (int i=0;i<n;++i,p+=u)
00072       {
00073         im_p = w2i(p);
00074         for (int plane=0;plane<np;++plane,++v)
00075           *v = vil_bilin_interp(image.image(),im_p.x(),im_p.y(),plane);
00076       }
00077     }
00078   }
00079   else // Use safe interpolation, setting v to zero if outside the image:
00080   {
00081     if (np==1)
00082     {
00083       for (int i=0;i<n;++i,p+=u)
00084       {
00085         im_p = w2i(p);
00086         v[i] = vil_bilin_interp_safe(image.image(),im_p.x(),im_p.y());
00087       }
00088     }
00089     else
00090     {
00091       for (int i=0;i<n;++i,p+=u)
00092       {
00093         im_p = w2i(p);
00094         for (int plane=0;plane<np;++plane,++v)
00095           *v = vil_bilin_interp_safe(image.image(),im_p.x(),im_p.y(),plane);
00096       }
00097     }
00098   }
00099 }
00100 
00101 #define VIMT_SAMPLE_PROFILE_BILIN_INSTANTIATE( imType, vecType ) \
00102 template void vimt_sample_profile_bilin(vnl_vector<vecType >& v, \
00103                                         const vimt_image_2d_of<imType >& image, \
00104                                         const vgl_point_2d<double >& p, \
00105                                         const vgl_vector_2d<double >& u, \
00106                                         int n)
00107 
00108 #endif // vimt_sample_profile_bilin_txx_

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