vpgl_lens_warp_mapper.h
Go to the documentation of this file.
1 // This is core/vpgl/algo/vpgl_lens_warp_mapper.h
2 #ifndef vpgl_lens_warp_mapper_h_
3 #define vpgl_lens_warp_mapper_h_
4 //:
5 // \file
6 // \brief A lens distortion adaptor for vil_warp
7 // \author Matt Leotta
8 // \date August 22, 2005
9 
11 #include <vil/vil_image_view.h>
12 #include <vgl/vgl_point_2d.h>
13 #include <vgl/vgl_vector_2d.h>
14 #include <vgl/vgl_box_2d.h>
15 #include <vgl/vgl_homg_point_2d.h>
16 #include <cassert>
17 #ifdef _MSC_VER
18 # include <vcl_msvc_warnings.h>
19 #endif
20 
21 //: Compute a bounding box in the distorted space for an existing box
22 // This is computed by sampling along the box boundary edges
23 // \note in general this is not the inverse of vpgl_lens_unwarp_bounds
24 template <class DataT, class BoxT>
27  const vgl_box_2d<BoxT>& box,
28  BoxT step_size = BoxT(1))
29 {
30  vgl_box_2d<BoxT> new_box;
31  for (BoxT x=box.min_x(); x<=box.max_x(); x+=step_size){
33  new_box.add(vgl_point_2d<BoxT>( BoxT(p1.x()), BoxT(p1.y()) ));
35  new_box.add(vgl_point_2d<BoxT>( BoxT(p2.x()), BoxT(p2.y()) ));
36  }
37  for (BoxT y=box.min_y(); y<=box.max_y(); y+=step_size){
39  new_box.add(vgl_point_2d<BoxT>( BoxT(p1.x()), BoxT(p1.y()) ));
41  new_box.add(vgl_point_2d<BoxT>( BoxT(p2.x()), BoxT(p2.y()) ));;
42  }
43  return new_box;
44 }
45 
46 //: Compute a bounding box for an existing box in the distorted space
47 // This is computed by sampling along the box boundary edges
48 // \note in general this is not the inverse of vpgl_lens_warp_bounds
49 template <class DataT, class BoxT>
52  const vgl_box_2d<BoxT>& box,
53  BoxT step_size = BoxT(1))
54 {
55  vgl_box_2d<BoxT> new_box;
56  for (BoxT x=box.min_x(); x<=box.max_x(); x+=step_size){
58  new_box.add(vgl_point_2d<BoxT>( BoxT(p1.x()), BoxT(p1.y()) ));
60  new_box.add(vgl_point_2d<BoxT>( BoxT(p2.x()), BoxT(p2.y()) ));
61  }
62  for (BoxT y=box.min_y(); y<=box.max_y(); y+=step_size){
64  new_box.add(vgl_point_2d<BoxT>( BoxT(p1.x()), BoxT(p1.y()) ));
66  new_box.add(vgl_point_2d<BoxT>( BoxT(p2.x()), BoxT(p2.y()) ));;
67  }
68  return new_box;
69 }
70 
71 
72 //: A warping function to apply lens distortion to an image
73 // This function automatically sets the translation of the distortion function
74 // and computes the appropriate image size such that all distorted pixel
75 // lie in the resulting image
76 template <class sType, class dType, class T, class InterpFunctor>
77 vil_image_view<dType>
78 vpgl_lens_warp_resize(const vil_image_view<sType>& in,
79  dType /*out_dummy*/,
81  InterpFunctor interp)
82 {
83  vgl_box_2d<int> bounds = vpgl_lens_warp_bounds(ld, vgl_box_2d<int>(0,in.ni(),0,in.nj()));
84  vgl_vector_2d<T> offset(T(-bounds.min_x()), T(-bounds.min_y()));
85  ld.set_translation( offset, true );
86  vil_image_view<dType> out(bounds.width(), bounds.height(), in.nplanes());
87  vpgl_lens_warp(in, out, ld, interp);
88  return out;
89 }
90 
91 
92 //: A version of vil_warp specialized for lens warping with iterative undistort
93 // The algorithm uses the result at the previous pixel as an initial guess
94 // which should reduce iterations until convergence
95 template <class sType, class dType, class T, class InterpFunctor>
96 void vpgl_lens_warp(const vil_image_view<sType>& in,
97  vil_image_view<dType>& out,
98  const vpgl_lens_distortion<T>& ld,
99  InterpFunctor interp)
100 {
101  unsigned const out_w = out.ni();
102  unsigned const out_h = out.nj();
103 
104  assert(out.nplanes() == in.nplanes());
105 
106  vgl_homg_point_2d<T> init(T(0),T(0));
107  for (unsigned oy = 0; oy < out_h; ++oy)
108  {
109  vgl_homg_point_2d<T> unwarp_pt = init;
110  for (unsigned ox = 0; ox < out_w; ++ox)
111  {
112  // *** Find (ix, iy) from (ox,oy)
113  double ix, iy;
114  unwarp_pt = ld.undistort(vgl_homg_point_2d<T>(ox,oy),&unwarp_pt);
115  if (oy == 0) init = unwarp_pt;
116  ix = unwarp_pt.x()/unwarp_pt.w();
117  iy = unwarp_pt.y()/unwarp_pt.w();
118  for (unsigned p = 0; p < out.nplanes(); ++p)
119  {
120  out(ox, oy, p) = dType(interp(in, ix, iy, p));
121  }
122  }
123  }
124 }
125 
126 
127 //: A warping function to apply lens distortion to an image
128 // This function automatically sets the translation of the distortion function
129 // and computes the appropriate image size such that all distorted pixel
130 // lie in the resulting image
131 template <class sType, class dType, class T, class InterpFunctor>
132 vil_image_view<dType>
133 vpgl_lens_unwarp_resize(const vil_image_view<sType>& in,
134  dType /*out_dummy*/,
136  InterpFunctor interp)
137 {
139  vgl_vector_2d<T> offset(T(-bounds.min_x()), T(-bounds.min_y()));
140  ld.set_translation( offset, false );
141  vil_image_view<dType> out(bounds.width(), bounds.height(), in.nplanes());
142  vpgl_lens_unwarp(in, out, ld, interp);
143  return out;
144 }
145 
146 
147 //: A version of vil_warp specialized for lens unwarping
148 template <class sType, class dType, class T, class InterpFunctor>
149 void vpgl_lens_unwarp(const vil_image_view<sType>& in,
150  vil_image_view<dType>& out,
151  const vpgl_lens_distortion<T>& ld,
152  InterpFunctor interp)
153 {
154  unsigned const out_w = out.ni();
155  unsigned const out_h = out.nj();
156 
157  assert(out.nplanes() == in.nplanes());
158 
159  for (unsigned oy = 0; oy < out_h; ++oy)
160  {
161  for (unsigned ox = 0; ox < out_w; ++ox)
162  {
164  for (unsigned p = 0; p < out.nplanes(); ++p)
165  {
166  out(ox, oy, p) = dType(interp(in, pt.x(), pt.y(), p));
167  }
168  }
169  }
170 }
171 
172 #endif // vpgl_lens_warp_mapper_h_
Type min_x() const
virtual vgl_homg_point_2d< T > undistort(const vgl_homg_point_2d< T > &point, const vgl_homg_point_2d< T > *init=nullptr) const
Return the original point that was distorted to this location (inverse of distort).
An abstract base class for all lens distortions.
void vpgl_lens_warp(const vil_image_view< sType > &in, vil_image_view< dType > &out, const vpgl_lens_distortion< T > &ld, InterpFunctor interp)
A version of vil_warp specialized for lens warping with iterative undistort.
vil_image_view< dType > vpgl_lens_unwarp_resize(const vil_image_view< sType > &in, dType, vpgl_lens_distortion< T > &ld, InterpFunctor interp)
A warping function to apply lens distortion to an image.
vgl_box_2d< BoxT > vpgl_lens_unwarp_bounds(const vpgl_lens_distortion< DataT > &lens, const vgl_box_2d< BoxT > &box, BoxT step_size=BoxT(1))
Compute a bounding box for an existing box in the distorted space.
vil_image_view< dType > vpgl_lens_warp_resize(const vil_image_view< sType > &in, dType, vpgl_lens_distortion< T > &ld, InterpFunctor interp)
A warping function to apply lens distortion to an image.
virtual vgl_homg_point_2d< T > distort(const vgl_homg_point_2d< T > &point) const =0
Distort a projected point on the image plane.
Type min_y() const
void add(vgl_point_2d< Type > const &p)
vgl_box_2d< BoxT > vpgl_lens_warp_bounds(const vpgl_lens_distortion< DataT > &lens, const vgl_box_2d< BoxT > &box, BoxT step_size=BoxT(1))
Compute a bounding box in the distorted space for an existing box.
void bounds(vbl_array_1d< T > const &in, T &min, T &max)
Type max_y() const
forward declare vgl_homg_point_2d<T> and vgl_vector_2d<T>.
Type max_x() const
virtual void set_translation(const vgl_vector_2d< T > &offset, bool after=true)=0
Set a translation to apply before of after distortion.
void vpgl_lens_unwarp(const vil_image_view< sType > &in, vil_image_view< dType > &out, const vpgl_lens_distortion< T > &ld, InterpFunctor interp)
A version of vil_warp specialized for lens unwarping.