AliceVision
Photogrammetric Computer Vision Framework
Undistortion.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2023 AliceVision contributors.
3 // This Source Code Form is subject to the terms of the Mozilla Public License,
4 // v. 2.0. If a copy of the MPL was not distributed with this file,
5 // You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #pragma once
8 
9 #include <aliceVision/camera/cameraCommon.hpp>
10 
11 #include <aliceVision/numeric/numeric.hpp>
12 
13 #include <vector>
14 #include <string>
15 #include <memory>
16 
17 namespace aliceVision {
18 namespace camera {
19 
30 {
31  public:
32  Undistortion(int width, int height)
33  {
34  _pixelAspectRatio = 1.0;
35  _isDesqueezed = false;
36  _isLocked = false;
37 
38  setSize(width, height);
39  setOffset({0.0, 0.0});
40  }
41 
42  virtual EUNDISTORTION getType() const = 0;
43 
44  virtual Undistortion* clone() const = 0;
45 
46  // not virtual as child classes do not hold any data
47  bool operator==(const Undistortion& other) const { return _undistortionParams == other._undistortionParams; }
48 
49  void setDesqueezed(bool isDesqueezed)
50  {
51  _isDesqueezed = isDesqueezed;
52 
53  double hh = _size.y();
54  if (!_isDesqueezed)
55  {
56  hh = _size.y() / _pixelAspectRatio;
57  }
58 
59  _diagonal = sqrt(_size.x() * _size.x() + hh * hh) * 0.5;
60  }
61 
62  bool isDesqueezed() const { return _isDesqueezed; }
63 
64  bool isLocked() const { return _isLocked; }
65 
66  void setLocked(bool lock) { _isLocked = lock; }
67 
68  void setOffset(const Vec2& offset) { _offset = offset; }
69 
70  void setSize(int width, int height)
71  {
72  double hh = height;
73  if (!_isDesqueezed)
74  {
75  hh = height / _pixelAspectRatio;
76  }
77 
78  _diagonal = sqrt(width * width + hh * hh) * 0.5;
79  _center = {width / 2, height / 2};
80  _size = {width, height};
81  }
82 
83  void setDiagonal(double diagonal)
84  {
85  // May be used for plates with a different size than lens grid
86  _diagonal = diagonal;
87  }
88 
89  void setPixelAspectRatio(double pixelAspectRatio)
90  {
91  _pixelAspectRatio = pixelAspectRatio;
92 
93  double hh = _size.y();
94  if (!_isDesqueezed)
95  {
96  hh = _size.y() / _pixelAspectRatio;
97  }
98 
99  _diagonal = sqrt(_size.x() * _size.x() + hh * hh) * 0.5;
100  }
101 
102  inline Vec2 getOffset() const { return _offset; }
103 
104  inline Vec2 getScaledOffset() const { return _offset / _diagonal; }
105 
106  Vec2 getSize() const { return _size; }
107 
108  Vec2 getCenter() const { return _center + _offset; }
109 
110  inline double getDiagonal() const { return _diagonal; }
111 
112  double getPixelAspectRatio() const { return _pixelAspectRatio; }
113 
114  const std::vector<double>& getParameters() const { return _undistortionParams; }
115 
116  void setParameters(const std::vector<double>& params)
117  {
118  if (_undistortionParams.size() != params.size())
119  {
120  return;
121  }
122 
123  for (std::size_t i = 0; i < _undistortionParams.size(); i++)
124  {
125  _undistortionParams[i] = params[i];
126  }
127  }
128 
129  std::size_t getUndistortionParametersCount() const { return _undistortionParams.size(); }
130 
131  Vec2 undistort(const Vec2& p) const;
132 
133  Eigen::Matrix<double, 2, Eigen::Dynamic> getDerivativeUndistortWrtParameters(const Vec2& p);
134 
135  Eigen::Matrix<double, 2, 2> getDerivativeUndistortWrtOffset(const Vec2& p);
136 
138  Vec2 inverse(const Vec2& p) const;
139 
140  virtual Vec2 inverseNormalized(const Vec2& p) const = 0;
141  virtual Vec2 undistortNormalized(const Vec2& p) const = 0;
142  virtual Eigen::Matrix<double, 2, Eigen::Dynamic> getDerivativeUndistortNormalizedwrtParameters(const Vec2& p) const = 0;
143  virtual Eigen::Matrix<double, 2, 2> getDerivativeUndistortNormalizedwrtPoint(const Vec2& p) const = 0;
144 
145  virtual ~Undistortion() = default;
146 
147  protected:
148  Vec2 _size;
149  Vec2 _center;
150  Vec2 _offset;
151  double _diagonal;
152  double _pixelAspectRatio;
153  bool _isDesqueezed;
154  std::vector<double> _undistortionParams{};
155  bool _isLocked;
156 };
157 
158 } // namespace camera
159 } // namespace aliceVision
aliceVision::camera::Undistortion
Abstract class to represent the inverse operation of a distortion model.
Definition: Undistortion.hpp:29
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::camera::Undistortion::inverse
Vec2 inverse(const Vec2 &p) const
add distortion (return p' such that undisto(p') = p)
Definition: Undistortion.cpp:109