75 #ifndef SLIP_DISTORTIONCAMERA_HPP
76 #define SLIP_DISTORTIONCAMERA_HPP
89 #include <boost/serialization/access.hpp>
90 #include <boost/serialization/split_member.hpp>
91 #include <boost/serialization/version.hpp>
92 #include <boost/serialization/base_object.hpp>
96 template <
typename Type>
130 template<
class Archive>
131 void save(Archive & ar,
const unsigned int version)
const
135 template<
class Archive>
136 void load(Archive & ar,
const unsigned int version)
140 BOOST_SERIALIZATION_SPLIT_MEMBER()
146 template <
typename Type>
149 template <
typename Type>
150 std::ostream& operator<<(std::ostream & out, const DistortionCamera<Type>& c);
163 template <
typename Type>
226 friend std::ostream& operator<< <>(std::ostream & out,
const self& c);
240 void read(
const std::string& file)
254 slip::write_ascii_1d<typename slip::Vector<Type>::iterator,Type>((*calibration_vector_).begin(),(*calibration_vector_).end(),file);
311 Type den = ((*calibration_vector_)[6]*p[0] + (*calibration_vector_)[7]*p[1] + (*calibration_vector_)[8]*p[2] + (*calibration_vector_)[11]);
312 Type xr = ((*calibration_vector_)[0]*p[0] + (*calibration_vector_)[1]*p[1] + (*calibration_vector_)[2]*p[2] + (*calibration_vector_)[9])/den;
313 Type yr = ((*calibration_vector_)[3]*p[0] + (*calibration_vector_)[4]*p[1] + (*calibration_vector_)[5]*p[2] + (*calibration_vector_)[10])/den;
314 Type xr_p = xr-(*calibration_vector_)[12];
315 Type yr_p = yr-(*calibration_vector_)[13];
316 Type xr_p_2 = xr_p*xr_p;
317 Type yr_p_2 = yr_p*yr_p;
318 Type xr_p_yr_p = xr_p *yr_p;
319 Type rhoi2 = xr_p_2 + yr_p_2;
320 Type rhoi2_2 = rhoi2*rhoi2;
321 Type rhoi2_3 = rhoi2_2*rhoi2;
322 Type rad = ((*calibration_vector_)[14]*rhoi2 + (*calibration_vector_)[15]*rhoi2_2 + (*calibration_vector_)[16]*rhoi2_3);
325 +
static_cast<Type
>(2.0)*(*calibration_vector_)[17]*xr_p_yr_p
326 + (*calibration_vector_)[18]*(
static_cast<Type
>(3.0)*xr_p_2 + yr_p_2)
327 + (*calibration_vector_)[19]*rhoi2;
330 +
static_cast<Type
>(2.0)*(*calibration_vector_)[18]*xr_p*yr_p
331 + (*calibration_vector_)[17]*(
static_cast<Type
>(3.0)*yr_p_2 + xr_p_2)
332 + (*calibration_vector_)[20]*rhoi2;
398 y(0)=M[0][0]; y(1)=M[0][1]; y(2)=M[0][2]; y(9)=M[0][3];
399 y(3)=M[1][0]; y(4)=M[1][1]; y(5)=M[1][2]; y(10)=M[1][3];
400 y(6)=M[2][0]; y(7)=M[2][1]; y(8)=M[2][2]; y(11)=M[2][3];
401 std::fill(y_begin12,y_end,static_cast<Type>(0.0));
403 const std::size_t N = P.
rows();
404 const std::size_t _5N = 5 * N;
405 const std::size_t _5N_p_19 = _5N+19;
406 const std::size_t _5N_p_7 = _5N+7;
409 for(std::size_t i = 0; i<N; ++i)
413 adatad[2*N+i]=P[i][2];
414 adatad[3*N+i]=P[i][3];
415 adatad[4*N+i]=P[i][4];
426 std::copy(y_begin,y_begin12,adatad_begin_5N_p_7);
432 double chisq = 1000.0;
433 double new_chisq = 0.0;
446 std::copy(y_begin,y_begin12,calibration_vector_begin);
447 std::copy(p9_begin,p9_end,calibration_vector_begin12);
452 y(0)=M[0][0]; y(1)=M[0][1]; y(2)=M[0][2]; y(9)=M[0][3];
453 y(3)=M[1][0]; y(4)=M[1][1]; y(5)=M[1][2]; y(10)=M[1][3];
454 y(6)=M[2][0]; y(7)=M[2][1]; y(8)=M[2][2]; y(11)=M[2][3];
456 std::copy(y_begin,y_begin12,adatad_begin_5N_p_7);
458 if (
std::abs(((chisq - new_chisq)/chisq)) < eps)
473 std::cout<<
" Chisq final = "<<new_chisq<<
" iteration = "<<iter<<std::endl;
476 std::copy(y_begin,y_begin12,calibration_vector_begin);
477 std::copy(p9_begin,p9_end,calibration_vector_begin12);
504 const std::size_t P_rows = P.
rows();
505 for(std::size_t i = 0; i < P_rows; ++i)
525 Type U0=(*calibration_vector_)[12];Type V0=(*calibration_vector_)[13];
526 Type R1=(*calibration_vector_)[14];Type R2=(*calibration_vector_)[15];Type R3=(*calibration_vector_)[16];
527 Type D1=(*calibration_vector_)[17];Type D2=(*calibration_vector_)[18];
528 Type P1=(*calibration_vector_)[19];Type P2=(*calibration_vector_)[20];
535 Type two_x_p_y_p =
static_cast<Type
>(2.0)*x_p * y_p;
536 Type rhoi2 = x_p2 + y_p2;
537 Type rhoi2_2 = rhoi2*rhoi2;
538 Type rhoi2_3 = rhoi2_2*rhoi2;
539 Type rad=(R1*rhoi2 + R2*rhoi2_2 + R3*rhoi2_3);
540 Type xd = x + x_p*rad + D1*two_x_p_y_p + D2*(
static_cast<Type
>(3.0)*x_p2+y_p2) + P1*rhoi2;
541 Type yd = y + y_p*rad + D2*two_x_p_y_p + D1*(
static_cast<Type
>(3.0)*y_p2+x_p2) + P2*rhoi2;
556 template<
class Archive>
557 void save(Archive & ar,
const unsigned int version)
const
559 ar & boost::serialization::base_object<slip::CameraModel<Type> >(*this);
564 template<
class Archive>
565 void load(Archive & ar,
const unsigned int version)
567 ar & boost::serialization::base_object<slip::CameraModel<Type> >(*this);
572 BOOST_SERIALIZATION_SPLIT_MEMBER()
581 template<
typename Type>
582 std::ostream& operator<<(std::ostream & out, const DistortionCamera<Type>& c)
584 out<<*c.calibration_matrix_<<std::endl;
585 out<<c.calibration_distortions_->R1<<std::endl;
586 out<<c.calibration_distortions_->R2<<std::endl;
587 out<<c.calibration_distortions_->R3<<std::endl;
588 out<<c.calibration_distortions_->d1<<std::endl;
589 out<<c.calibration_distortions_->d2<<std::endl;
590 out<<c.calibration_distortions_->p1<<std::endl;
591 out<<c.calibration_distortions_->p2<<std::endl;
592 out<<*c.calibration_vector_;
598 #endif //SLIP_DISTORTIONCAMERA_HPP
Provides a class to modelize 3d points.
void read_ascii_1d(const std::string &file_path_name, Container1d &container)
Read a Container1d from an ASCII file.
self & operator=(const self &other)
Assign a CameraModel.
friend class boost::serialization::access
slip::Point2d< Type > invert_distortion_model(const slip::Point2d< Type > &pd, const slip::Vector< Type > &p)
Inverts distortion model using the Newton-method.
DistortionCamera(const self &rhs)
Constructs a copy of the DistortionCamera rhs.
iterator end()
Returns a read/write iterator that points one past the last element in the Vector. Iteration is done in ordinary element order.
void distort(const slip::Point2d< Type > &p, slip::Point2d< Type > &pd)
Distorts a image point using the parameters of the distortion camera model.
Provides a camera algorithms.
void undistort(const slip::Matrix< Type > &P, slip::Matrix< Type > &Q)
undistorts a vector list using the parameters of the distortion camera model
size_type cols() const
Returns the number of columns (second dimension size) in the Matrix.
DistortionCamera(slip::DistStruct< Type > *dist_struct)
Constructor of DistortionCamera.
generic derivative functor
void write(const std::string &file)
Write a calibration matrix to an ASCII file.
HyperVolume< T > abs(const HyperVolume< T > &M)
~DistortionCamera()
Destructor of the DistortionCamera.
Function to compute the backprojection of the 3d world point corresponding to the backprojection of a...
void marquardt(Function &fun, DerivativeFunction &df, slip::Vector< Real > &par, slip::Vector< Real > &r, Real &calchi2, const int maxiter=10000, const Real eps=static_cast< Real >(1e-6))
Optimization of function using the Levenberg-Marquardt algorithm.
This is a point2d class, a specialized version of Point<CoordType,DIM> with DIM = 2...
Define a DistortionCamera class.
slip::Point2d< Type > projection(const slip::Point3d< Type > &p)
Computes the projection of a 3d world point onto the image plane.
self & operator=(const self &rhs)
Assign a DistortionCamera.
size_type size() const
Returns the number of elements in the Vector.
Provides a simple implementation of the Levendberg-Marquardt algorithm.
void copy(_II first, _II last, _OI output_first)
Copy algorithm optimized for slip iterators.
DistortionCamera()
Default constructor of DistortionCamera.
slip::Vector< Type > * calibration_vector_
Numerical Vector class. This container statisfies the RandomAccessContainer concepts of the Standard ...
Provides an abstract class to model cameras.
slip::DistStruct< Type > * calibration_distortions_
Numerical matrix class. This container statisfies the BidirectionnalContainer concepts of the STL...
slip::Matrix< Type > * calibration_matrix_
friend class boost::serialization::access
void read(const std::string &file)
Read a calibration matrix from an ASCII file.
size_type rows() const
Returns the number of rows (first dimension size) in the Matrix.
Function to compute the camera model of distortion using the Levenberg-Marquadt algorithm.
void resize(const size_type d1, const size_type d2, const T &val=T())
Resizes a Matrix.
void getpars_DLT_norm(const slip::Matrix< Type > &P, slip::Matrix< Type > &Mat)
Get calibration parameters using the DLT.
Provides a class to manipulate Numerical Matrix.
Provides a class to modelize 2d points.
void compute(const slip::Matrix< Type > &P)
Computes the parameters of the distortion camera model.
DistStruct(const DistStruct< Type > &rhs)
slip::Point3d< Type > backprojection(const slip::Point2d< Type > &p2, const Type &z)
Computes the 3d world point corresponding to the backprojection of an image point.
iterator begin()
Returns a read/write iterator that points to the first element in the Vector. Iteration is done in or...
Define an abstract CameraModel class.
This is a point3d class, a specialized version of Point<CoordType,DIM> with DIM = 3...