Isis 3 Programmer Reference
RosettaOsirisCameraDistortionMap.cpp
Go to the documentation of this file.
1 
25 
26 using namespace std;
27 
28 namespace Isis {
38  RosettaOsirisCameraDistortionMap::RosettaOsirisCameraDistortionMap(Camera *parent) :
39  CameraDistortionMap(parent) {
40  // Initialize to the identity transform
43  m_toUnDistortedX(0, 1) = 1.0;
44  m_toUnDistortedY(1, 0) = 1.0;
45 
46  m_boresightSample = 0.0;
47  m_boresightLine = 0.0;
48  m_pixelPitch = 1.0;
49  }
50 
83  bool RosettaOsirisCameraDistortionMap::SetFocalPlane(const double dx, const double dy) {
84  p_focalPlaneX = dx;
85  p_focalPlaneY = dy;
86 
87  // The equations are in pixel coordinates so convert
88  double dxPixel = focalXToLine(dx);
89  double dyPixel = focalYToSample(dy);
90 
92  xTerms(0) = 1.0;
93  xTerms(1) = dxPixel;
94  xTerms(2) = dxPixel*dxPixel;
95  xTerms(3) = dxPixel*dxPixel*dxPixel;
96 
98  yTerms(0) = 1.0;
99  yTerms(1) = dyPixel;
100  yTerms(2) = dyPixel*dyPixel;
101  yTerms(3) = dyPixel*dyPixel*dyPixel;
102 
103  double ux = LinearAlgebra::dotProduct(
105  yTerms);
106  double uy = LinearAlgebra::dotProduct(
108  yTerms);
109 
112 
113  return true;
114  }
115 
150  bool RosettaOsirisCameraDistortionMap::SetUndistortedFocalPlane(const double ux, const double uy) {
151  // image coordinates prior to introducing distortion
154 
155  // The equations are in pixel coordinates so convert
156  double uxPixel = focalXToLine(ux);
157  double uyPixel = focalYToSample(uy);
158 
159  // Loop setup
160  double tolerance = 1e-7;
161  int iteration = 1;
162  int maxIterations = 20;
163  bool done = false;
164 
165  LinearAlgebra::Vector undistortedCoordinate = LinearAlgebra::zeroVector(2);
166  undistortedCoordinate(0) = uxPixel;
167  undistortedCoordinate(1) = uyPixel;
168  // Use the undistorted coordinate as the initial point
169  LinearAlgebra::Vector distortedCoordinate = undistortedCoordinate;
170 
175 
179  LinearAlgebra::Matrix negInvJacobian = LinearAlgebra::zeroMatrix(2, 2);
180 
181  // Compute the term vectors
182  // These are recomputed at the END of each iteration, so compute them
183  // before the first iteration
184  xTerms(0) = 1.0;
185  xTerms(1) = distortedCoordinate(0);
186  xTerms(2) = distortedCoordinate(0)*distortedCoordinate(0);
187  xTerms(3) = distortedCoordinate(0)*distortedCoordinate(0)*distortedCoordinate(0);
188 
189  yTerms(0) = 1.0;
190  yTerms(1) = distortedCoordinate(1);
191  yTerms(2) = distortedCoordinate(1)*distortedCoordinate(1);
192  yTerms(3) = distortedCoordinate(1)*distortedCoordinate(1)*distortedCoordinate(1);
193 
194  delXTerms(1) = 1.0;
195  delXTerms(2) = 2.0*distortedCoordinate(0);
196  delXTerms(3) = 3.0*distortedCoordinate(0)*distortedCoordinate(0);
197 
198  delYTerms(1) = 1.0;
199  delYTerms(2) = 2.0*distortedCoordinate(1);
200  delYTerms(3) = 3.0*distortedCoordinate(1)*distortedCoordinate(1);
201 
202  // Compute the object function, the distance between the redistorted and undistorted
203  objectFuncValue(0) = undistortedCoordinate(0)
205  yTerms );
206  objectFuncValue(1) = undistortedCoordinate(1)
208  yTerms );
209 
210  while(!done) {
211 
212  // Compute the negative jacobian
213  // The variable terms and object function value have already been
214  // computed priort to checking for convergence at the end of the
215  // previous iteration. These are not needed to check for convergence
216  // so, compute them now.
217  negJacobian(0, 0) = LinearAlgebra::dotProduct( LinearAlgebra::multiply(m_toUnDistortedX, delXTerms),
218  yTerms );
220  delYTerms );
221  negJacobian(1, 0) = LinearAlgebra::dotProduct( LinearAlgebra::multiply(m_toUnDistortedY, delXTerms),
222  yTerms );
224  delYTerms );
225 
226  // Invert the negative jacobian
227  // If it is not invertible, then fail
228  double det = negJacobian(0, 0) * negJacobian(1, 1) - negJacobian(1, 0) * negJacobian(0, 1);
229  if ( fabs(det) < 1e-15 ) {
230  return false;
231  }
232  negInvJacobian(0, 0) = negJacobian(1, 1) / det;
233  negInvJacobian(0, 1) = - negJacobian(0, 1) / det;
234  negInvJacobian(1, 0) = - negJacobian(1, 0) / det;
235  negInvJacobian(1, 1) = negJacobian(0, 0) / det;
236 
237  // Compute the update step
238  updateStep = LinearAlgebra::multiply(negInvJacobian, objectFuncValue);
239 
240  // Update and check for convergence
241  distortedCoordinate += updateStep;
242 
243  // Compute the term vectors
244  xTerms(0) = 1.0;
245  xTerms(1) = distortedCoordinate(0);
246  xTerms(2) = distortedCoordinate(0)*distortedCoordinate(0);
247  xTerms(3) = distortedCoordinate(0)*distortedCoordinate(0)*distortedCoordinate(0);
248 
249  yTerms(0) = 1.0;
250  yTerms(1) = distortedCoordinate(1);
251  yTerms(2) = distortedCoordinate(1)*distortedCoordinate(1);
252  yTerms(3) = distortedCoordinate(1)*distortedCoordinate(1)*distortedCoordinate(1);
253 
254  delXTerms(1) = 1.0;
255  delXTerms(2) = 2.0*distortedCoordinate(0);
256  delXTerms(3) = 3.0*distortedCoordinate(0)*distortedCoordinate(0);
257 
258  delYTerms(1) = 1.0;
259  delYTerms(2) = 2.0*distortedCoordinate(1);
260  delYTerms(3) = 3.0*distortedCoordinate(1)*distortedCoordinate(1);
261 
262  // Compute the object function, the distance between the redistorted and undistorted
263  objectFuncValue(0) = undistortedCoordinate(0)
265  yTerms );
266  objectFuncValue(1) = undistortedCoordinate(1)
268  yTerms );
269 
270  if ( LinearAlgebra::magnitude(objectFuncValue) < tolerance ||
271  iteration > maxIterations ) {
272  done = true;
273  }
274  iteration++;
275  }
276 
277  p_focalPlaneX = lineToFocalX( distortedCoordinate(0) );
278  p_focalPlaneY = sampleToFocalY( distortedCoordinate(1) );
279 
280  return true;
281  }
282 
283 
290  m_toUnDistortedX = xMat;
291  }
292 
293 
300  m_toUnDistortedY = yMat;
301  }
302 
303 
311  void RosettaOsirisCameraDistortionMap::setBoresight(double sample, double line) {
312  m_boresightSample = sample;
313  m_boresightLine = line;
314  }
315 
316 
324  m_pixelPitch = pitch;
325  }
326 
327 
336  return ( x / m_pixelPitch + m_boresightLine );
337  }
338 
339 
348  return ( y / m_pixelPitch + m_boresightSample);
349  }
350 
351 
360  return ( ( line - m_boresightLine ) * m_pixelPitch );
361  }
362 
363 
372  return ( ( sample - m_boresightSample) * m_pixelPitch );
373  }
374 }
double p_focalPlaneX
Distorted focal plane x.
void setUnDistortedXMatrix(LinearAlgebra::Matrix xMat)
Set the matrix for converting from distorted to undistorted samples.
static Matrix multiply(const Matrix &matrix1, const Matrix &matrix2)
Returns the product of two matrices.
static double magnitude(const Vector &vector)
Computes the magnitude (i.e., the length) of the given vector using the Euclidean norm (L2 norm)...
Namespace for the standard library.
boost::numeric::ublas::matrix< double > Matrix
Definition for an Isis::LinearAlgebra::Matrix of doubles.
static Matrix zeroMatrix(int rows, int columns)
Returns a matrix with given dimensions that is filled with zeroes.
static double dotProduct(const Vector &vector1, const Vector &vector2)
Computes the dot product of the given vectors.
void setPixelPitch(double pitch)
Set the pixel pitch for converting from focal plane coordinates to pixel coordinates.
virtual bool SetFocalPlane(const double dx, const double dy)
Compute undistorted focal plane x/y given a distorted focal plane x/y.
double p_undistortedFocalPlaneX
Undistorted focal plane x.
void setBoresight(double sample, double line)
Set the boresight location for converting from focal plane coordinates to pixel coordinates.
boost::numeric::ublas::vector< double > Vector
Definition for an Isis::LinearAlgebra::Vector of doubles.
LinearAlgebra::Matrix m_toUnDistortedX
Matrix for computing undistorted X coordinates.
double lineToFocalX(double line)
Convert pixel space line coordinate to a focal plane x coordinate.
double sampleToFocalY(double sample)
Convert pixel space sample coordinate to a focal plane y coordinate.
double focalXToLine(double x)
Convert a focal plane x coordinate to a pixel space line coordinate.
static Vector zeroVector(int size)
Returns a vector of given length that is filled with zeroes.
Distort/undistort focal plane coordinates.
double m_pixelPitch
Camera pixel pitch for converting focal plane coordinates to pixel coordinates.
void setUnDistortedYMatrix(LinearAlgebra::Matrix yMat)
Set the matrix for converting from distorted to undistorted lines.
double m_boresightSample
Camera boresight sample coordinate for converting focal plane coordinates to pixel coordinates...
LinearAlgebra::Matrix m_toUnDistortedY
Matrix for computing undistorted Y coordinates.
double p_focalPlaneY
Distorted focal plane y.
double p_undistortedFocalPlaneY
Undistorted focal plane y.
virtual bool SetUndistortedFocalPlane(const double ux, const double uy)
Compute distorted focal plane x/y given an undistorted focal plane x/y.
double focalYToSample(double y)
Convert a focal plane y coordinate to a pixel space sample coordinate.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
double m_boresightLine
Camera boresight line coordinate for converting focal plane coordinates to pixel coordinates.