vpgl_radial_distortion.h
Go to the documentation of this file.
1 // This is core/vpgl/vpgl_radial_distortion.h
2 #ifndef vpgl_radial_distortion_h_
3 #define vpgl_radial_distortion_h_
4 //:
5 // \file
6 // \brief An abstract base class for radial lens distortions.
7 // \author Matt Leotta
8 // \date August 19, 2005
9 //
10 // A radial lens distortion is a 2D warping of the image plane that is radial symmetric
11 // about some center of distortion. It is assumed that the map is bijective,
12 // though a closed form solution for the inverse may not exist in general.
13 // A default iterative solver is implemented to solve this numerically.
14 
15 #include "vpgl_lens_distortion.h"
16 #include <vgl/vgl_point_2d.h>
17 #include <vgl/vgl_vector_2d.h>
18 #include <vgl/vgl_homg_point_2d.h>
19 
20 //: A base class for radial lens distortions
21 template <class T>
23 {
24  public:
25  //: Constructor
26  vpgl_radial_distortion(const vgl_point_2d<T>& center, bool has_deriv=false)
28 
29  //: Constructor
31  const vgl_point_2d<T>& new_center, bool has_deriv=false)
32  : center_(center), distorted_center_(new_center), has_derivative_(has_deriv) {}
33 
34  //: Distort a projected point on the image plane
35  // Calls the pure virtual radial distortion function
36  vgl_homg_point_2d<T> distort( const vgl_homg_point_2d<T>& point ) const override;
37 
38  //: Return the original point that was distorted to this location (inverse of distort)
39  // \param init is an initial guess at the solution for the iterative solver
40  // if \p init is NULL then \p point is used as the initial guess
41  // calls the radial undistortion function
43  const vgl_homg_point_2d<T>* init=nullptr) const override;
44 
45  //: Distort a radial length
46  // \retval a scale factor such that
47  // \code
48  // distort_pt = center + distort_radius(radius)*(pt - center)
49  // \endcode
50  virtual T distort_radius( T radius ) const = 0;
51 
52  //: Return the inverse of distort function
53  // \param init is an initial guess at the solution for the iterative solver
54  // if \p init is NULL then \p radius is used as the initial guess
55  virtual T undistort_radius( T radius, const T* init=nullptr) const;
56 
57  //: Compute the derivative of the distort_radius function
58  // \note implementing this function is optional but it may improve the convergence
59  // rate of the undistort function if iterative solving is used
60  // Set \p has_derivative_ to true if you define this function
61  virtual T distort_radius_deriv( T radius ) const
62  {
63  T eps = T(0.001);
64  return (distort_radius(radius) - distort_radius(radius-eps)) / eps;
65  }
66 
67  //: Set a translation to apply before of after distortion
68  // This is needed when distorting an image to translate the resulting image
69  // such that all points have positive indices
70  void set_translation(const vgl_vector_2d<T>& offset, bool after = true) override
71  {
72  if (after)
73  distorted_center_ += offset;
74  else
75  center_ += offset;
76  }
77 
78  //: Returns the center of distortion
79  vgl_point_2d<T> center() const { return center_; }
80  //: Returns the center of distortion in the distorted image
82 
83  //: Set the center of distortion
84  void set_center(const vgl_point_2d<T>& c) { center_ = c; }
85  //: Set the center of distortion in the distorted image
87 
88  protected:
89  //: The center of distortion
91 
92  //: The center of distortion in the distorted space
94 
96 };
97 
98 #endif // vpgl_radial_distortion_h_
A base class for radial lens distortions.
vgl_point_2d< T > distorted_center() const
Returns the center of distortion in the distorted image.
vpgl_radial_distortion(const vgl_point_2d< T > &center, bool has_deriv=false)
Constructor.
vgl_homg_point_2d< T > distort(const vgl_homg_point_2d< T > &point) const override
Distort a projected point on the image plane.
vgl_homg_point_2d< T > undistort(const vgl_homg_point_2d< T > &point, const vgl_homg_point_2d< T > *init=nullptr) const override
Return the original point that was distorted to this location (inverse of distort).
An abstract base class for all lens distortions.
void set_translation(const vgl_vector_2d< T > &offset, bool after=true) override
Set a translation to apply before of after distortion.
vpgl_radial_distortion(const vgl_point_2d< T > &center, const vgl_point_2d< T > &new_center, bool has_deriv=false)
Constructor.
vgl_point_2d< T > center() const
Returns the center of distortion.
vgl_point_2d< T > center_
The center of distortion.
virtual T undistort_radius(T radius, const T *init=nullptr) const
Return the inverse of distort function.
vgl_point_2d< T > distorted_center_
The center of distortion in the distorted space.
virtual T distort_radius_deriv(T radius) const
Compute the derivative of the distort_radius function.
void set_center(const vgl_point_2d< T > &c)
Set the center of distortion.
forward declare vgl_homg_point_2d<T> and vgl_vector_2d<T>.
void set_distorted_center(const vgl_point_2d< T > &dc)
Set the center of distortion in the distorted image.
virtual T distort_radius(T radius) const =0
Distort a radial length.