core/vil/vil_warp.h
Go to the documentation of this file.
00001 // This is core/vil/vil_warp.h
00002 #ifndef vil_warp_h_
00003 #define vil_warp_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Warp an image.
00010 // \author awf@robots.ox.ac.uk
00011 // \date 04 Dec 00
00012 // \verbatim
00013 //  Modifications
00014 //   031201 IMS Convert to vil2. Used templates to simplify interface and code.
00015 // \endverbatim
00016 
00017 #include <vil/vil_fwd.h>
00018 #include <vcl_cassert.h>
00019 
00020 //: Warp an image under a 2D map.
00021 // The size of the output map and the mapper defines the region of
00022 // the input image to be scanned.
00023 // \param mapper, is the inverse of the mapping from the input image's
00024 // co-ordinate frame to the output image's frame.
00025 // i.e. out() = in(mapper(x,y)). It should be a functor with a signature
00026 // \code
00027 //    void mapper(double x_in, double y_in, double* x_out, double* y_out);
00028 // \endcode
00029 // \param interp, is an interpolator, with a signature similar to
00030 // \code
00031 //   S vil_bilin_interp_safe(const vil_image_view<T>&, double, double, unsigned)
00032 // \endcode
00033 //
00034 // Note that if you want to store a warp with an image to create a registered image,
00035 // the vimt library (in contrib/mul/vimt) provides efficient registered images
00036 // with transforms up to projective.
00037 //
00038 // \relatesalso vil_image_view
00039 template <class sType, class dType, class MapFunctor, class InterpFunctor>
00040 void vil_warp(const vil_image_view<sType>& in,
00041               vil_image_view<dType>& out,
00042               MapFunctor mapper,
00043               InterpFunctor interp)
00044 {
00045   unsigned const out_w = out.ni();
00046   unsigned const out_h = out.nj();
00047 
00048   assert(out.nplanes() == in.nplanes());
00049 
00050   for (unsigned p = 0; p < out.nplanes(); ++p)
00051   {
00052     for (unsigned oy = 0; oy < out_h; ++oy)
00053     {
00054       for (unsigned ox = 0; ox < out_w; ++ox)
00055       {
00056         // *** Find (ix, iy) from (ox,oy)
00057         double ix, iy;
00058         mapper(double(ox), double(oy), ix, iy);
00059         out(ox, oy, p) = dType(interp(in, ix, iy, p));
00060       }
00061     }
00062   }
00063 }
00064 
00065 #endif // vil_warp_h_