26 #include <Geometry/Projection.hh>
27 #include <Geometry/ProjectionParametersFactory.hh>
28 #include <Geometry/ProjectionParametersZoom.hh>
29 #include <Geometry/ProjectionParametersZoom.hh>
30 #include <Base/Common/FileHandling.hh>
39 vecParameters_.clear();
44 BIASASSERT(vecParameters_.empty());
45 vecParameters_.push_back(p.
Clone());
50 BIASASSERT(vecParameters_.empty());
56 for (
unsigned int i=0; i<vecParameters_.size(); i++)
57 delete vecParameters_[i];
58 vecParameters_.clear();
65 if (XMLRead(filename)==0)
return 0;
66 if (ReadFromCamParamFile(filename, silent)==0)
return 0;
68 if (ReadFromBBC(filename, silent)==0) {
69 cout <<
"Read projection from BBC freed data: "<< *
this <<endl;
74 if (P.
Load(filename)) {
75 double x = P.
GetK()[0][2];
76 double y = P.
GetK()[1][2];
77 int width = 8*(int)(rint(x+2.0)/4);
78 int height = 8*(int)(rint(y+2.0)/4);
79 cout<<
"Read from PMatrix, image size is UNKNOWN, GUESSING: "<<width
84 vecParameters_.clear();
85 vecParameters_.push_back(PP.
Clone());
90 #ifndef BIAS_HAVE_XML2
91 BIASERR(
"Missing XML support in BIAS, can only read BBC FreeD-data.");
100 const bool silent ) {
103 vector<CameraData> vcd;
106 return InitFromCameraData(vcd);
108 #endif // BIAS_HAVE_XML2
113 vector<CameraData> vcd;
116 silent)!=0)
return -1;
117 return InitFromCameraData(vcd);
122 vecParameters_.clear();
125 for (
unsigned int i=0; i<vcd.size(); i++) {
126 vecParameters_.push_back(factory.
Create(vcd[i]));
135 int width,
int height,
137 const vector<double>&
138 UndistortionCoefficients)
148 BIASASSERT(UndistortionCoefficients.size() == 2);
150 UndistortionCoefficients[1]);
153 BIASASSERT(UndistortionCoefficients.size() == 4);
155 UndistortionCoefficients[1],
156 UndistortionCoefficients[2],
157 UndistortionCoefficients[3]);
160 BIASASSERT(UndistortionCoefficients.size() == 5);
162 UndistortionCoefficients[1],
163 UndistortionCoefficients[2],
164 UndistortionCoefficients[3],
165 UndistortionCoefficients[4]);
168 BIASASSERT(UndistortionCoefficients.size() == 4);
170 UndistortionCoefficients[1],
171 UndistortionCoefficients[2],
172 UndistortionCoefficients[3]);
175 BIASASSERT(UndistortionCoefficients.size() == 3);
177 UndistortionCoefficients[1],
178 UndistortionCoefficients[2]);
181 BIASASSERT(UndistortionCoefficients.size() == 3);
183 UndistortionCoefficients[1],
184 UndistortionCoefficients[2]);
196 vecParameters_.push_back(p);
202 if(vecParameters_.size() == 0)
203 vecParameters_.push_back(p);
208 vecParameters_[0]->GetPose());
212 vecParameters_.push_back(p);
219 const unsigned int width,
const int height,
220 const double principalX,
const double principalY,
235 vector<double> AngleCorrX, AngleCorrY;
238 AngleCorrX.push_back(0.0);
239 AngleCorrY.push_back(0.0);
240 AngleCorrX.push_back(M_PI_2);
241 AngleCorrY.push_back(M_PI_2);
251 bool IgnoreDistortion)
const
253 BIASASSERT(vecParameters_.size()>cam && vecParameters_[cam]);
256 vecParameters_[cam]->GetExternals().
Mult(X, x);
257 return vecParameters_[cam]->ProjectLocal(x, IgnoreDistortion);
259 HomgPoint3D Xl = vecParameters_[0]->GetPose().GlobalToLocal(X);
260 return vecParameters_[cam]->Project(Xl, IgnoreDistortion);
262 BIASERR(
"To be implemented");
276 bool theyDiffer =
false;
277 for(
unsigned int i=0; i<vecParameters_.size() && !theyDiffer; i++)
278 theyDiffer = vecParameters_[i]->DoExtrinsicsDiffer(p.
vecParameters_[i]);
292 bool theyDiffer =
false;
293 for(
unsigned int i=0; i<vecParameters_.size() && !theyDiffer; i++ )
294 theyDiffer = vecParameters_[i]->DoIntrinsicsDiffer(p.
vecParameters_[i]);
303 bool IgnoreDistortion)
const
305 BIASASSERT(vecParameters_.size()>cam && vecParameters_[cam]);
308 HomgPoint3D Xr = vecParameters_[0]->GetPose().GlobalToLocal(X);
311 HomgPoint3D Xl = vecParameters_[cam]->GetPose().GlobalToLocal(Xr);
314 return vecParameters_[cam]->DoesPointProjectIntoImageLocal(Xl.
GetEuclidean(),x,IgnoreDistortion);
317 return vecParameters_[cam]->DoesPointProjectIntoImage(X, x,
327 bool IgnoreDistortion)
const
329 BIASASSERT(vecParameters_.size()>cam && vecParameters_[cam]);
331 UnProjectToRayLocal(pos, origin, x, 0, cam, IgnoreDistortion);
332 GetR().Mult(x, rayDir);
333 origin += vecParameters_[cam]->GetC();
340 bool IgnoreDistortion)
const
342 BIASASSERT(vecParameters_.size()>cam && vecParameters_[cam]);
344 UnProjectToRayLocal(pos, tmp, x, 0, cam, IgnoreDistortion);
345 GetR().Mult(x, rayDir);
352 bool IgnoreDistortion)
const
355 UnProjectToRay(pos, p, opticalray, cam, IgnoreDistortion);
356 if(opticalray.
NormL2()==0.0) {
358 BIASERR(
"returned ray has zero length!");
363 return (GetAbsoluteC(cam) + depth*opticalray);
370 unsigned int camSystem,
372 bool IgnoreDistortion)
const
374 BIASASSERT(vecParameters_.size()>cam && vecParameters_[cam]);
375 BIASASSERT(vecParameters_.size()>camSystem && vecParameters_[camSystem]);
378 vecParameters_[cam]->UnProjectToPointLocal(pos,
381 if(camSystem != cam) {
385 Pose CamRigPose = vecParameters_[cam]->GetPose();
391 Pose TargetRigPose = vecParameters_[camSystem]->GetPose();
404 const double& ,
unsigned int camSystem,
405 unsigned int cam,
bool IgnoreDistortion,
406 bool Normalize)
const
408 BIASASSERT(vecParameters_.size()>cam && vecParameters_[cam]);
409 if (camSystem != cam) {
410 BIASERR(
"Not implemented.");
413 return vecParameters_[cam]->UnProjectCovLocal(pos, cov2D,
414 IgnoreDistortion, Normalize);
422 unsigned int camSystem,
424 bool IgnoreDistortion)
const
426 BIASASSERT(vecParameters_.size()>camSystem && vecParameters_[camSystem]);
427 BIASASSERT(vecParameters_.size()>cam && vecParameters_[cam]);
430 vecParameters_[cam]->UnProjectLocal(pos, p, x, IgnoreDistortion);
432 if(camSystem != cam) {
438 p = COrigin + ROrigin * p;
443 RMatrix RTarget = GetR(camSystem);
459 #ifdef BIAS_HAVE_XML2
462 double& Version)
const{
463 TopLevelTag =
"Projection";
473 if(identifier_.size()>0) {
477 std::vector<ProjectionParametersBase* >::const_iterator it;
478 for (it=vecParameters_.begin(); it!=vecParameters_.end(); it++) {
479 (**it).XMLGetClassName(TopLevelName, Version);
482 (*it)->XMLOut(theNode, XMLObject);
492 for (
unsigned int i=0; i<vecParameters_.size(); i++)
493 delete vecParameters_[i];
494 vecParameters_.clear();
497 xmlNodePtr childNode;
499 if (childNode == NULL)
return 0;
500 while (childNode!=NULL) {
501 string nodename = XMLObject.
getNodeName(childNode);
502 if (nodename.compare(
"Identifier")==0) {
507 if(pp->
XMLIn(childNode, XMLObject)!=0)
509 vecParameters_.push_back(pp);
513 if (vecParameters_.size()>0) {
520 #endif // BIAS_HAVE_XML2
526 BIASASSERT(cam<vecParameters_.size() && vecParameters_[cam]);
527 return vecParameters_[cam]->GetQ();
void AddRelativeCamera(ProjectionParametersBase *ppb)
Add a camera to the projection,the coordinates of the pose are given in relative Coordinates, first cam is origin.
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
virtual void SetPrincipal(const double x, const double y)
Set principal point (in pixels relative to top left corner).
void GetEuclidean(Vector3< HOMGPOINT3D_TYPE > &dest) const
calculate affine coordinates of this and write them to dest affine coordinates are projective coordin...
int ReadFromBBC(const std::string &filename, const bool silent=false)
read BBC's FreeD data from file and constructs a perspective projection (may have radial distortion) ...
KMATRIX_TYPE GetFy() const
xmlNodePtr getNextChild()
Get the next child of the parent specified in the last getFirstChild() call, the class remembers the ...
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
void SetDistortionType(BIAS_ProjParaPersp_DISTORTION_TYPE distype)
Set type of distortion parameters.
Matrix3x3< double > UnProjectToCovLocal(const HomgPoint2D &pos, const Matrix3x3< double > &cov2D, const double &depth, unsigned int camSystem=0, unsigned int cam=0, bool IgnoreDistortion=false, bool Normalize=false) const
calculate 3D point and associated covariance by unprojection
bool Load(const std::string &filename)
overload Load because it has to invalidate decomposition! JW 09/2003
virtual int XMLIn(const xmlNodePtr Node, XMLIO &XMLObject)
specialization of XML read function for BIAS::Projection.
virtual const bool DoExtrinsicsDiffer(const BIAS::Projection &p) const
Checks whether extrinsic parameters are different.
ProjectionParametersBase * Create(const std::string &className)
Creates a projection parameters object by a class name.
int InitFromCameraData(const std::vector< CameraData > &vcd)
initializes projection from cameradata object
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual void SetPrincipal(const double x, const double y)
Set principal point in pixels relative to top left corner, virtual overload to recalculate K_...
virtual void SetImageSize(const unsigned int w, const unsigned int h)
Set image dimensions (in pixels).
virtual ProjectionParametersBase * Clone() const =0
Covariant virtual copy constructor used in BIAS::Projection.
void Homogenize()
homogenize class data member elements to W==1 by divison by W
static int ReadRigData(const std::string &filename, std::vector< CameraData > &vcd, const bool silent=false)
Read a rig parameter file and store the data in (*this) the Camera-, Center-, RMatrix-, and KMatrix-Vectors are reset from the file.
std::vector< ProjectionParametersBase * > vecParameters_
the intrinsics (like K, ...) of the camera usually only one only in rig mode there may be many...
virtual ~Projection()
destructor for initialization with a ProjectionParametersBase object
virtual void SetP(const PMatrix &P)
set from P
void SetUndistortion(double kc1, double kc2)
void CreateSpherical(const Pose &pose, const unsigned int width, const int height, const double principalX, const double principalY, const double radius, const double aspect=1.0)
push_back a new sperical cam to this
static std::string GetCwd()
Return current working directory as string.
virtual int XMLIn(const xmlNodePtr Node, XMLIO &XMLObject)
Specialization of XML read function.
BIAS::Quaternion< double > GetQ(unsigned int cam=0) const
return rotation quaternion of camera with index cam if cam>0 this is a relative pose in the coordinat...
virtual void SetPose(const BIAS::Pose pose)
Set pose from pose object.
void UnProjectToRayLocal(const HomgPoint2D &pos, Vector3< double > &origin, Vector3< double > &rayDir, unsigned int camSystem=0, unsigned int cam=0, bool IgnoreDistortion=false) const
calculates a 3D ray in a local camera coordinate system specified by camSystem, which belongs to the ...
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
void addContent(const xmlNodePtr Node, const std::string &Content)
Add content to a node.
KMATRIX_TYPE GetHy() const
virtual int XMLGetClassName(std::string &TopLevelTag, double &Version) const
specialization of XML block name function for BIAS::Projection.
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
void UnProjectToRay(const HomgPoint2D &pos, Vector3< double > &origin, Vector3< double > &rayDir, unsigned int cam=0, bool IgnoreDistortion=false) const
calculates the viewing ray in the global coordinate frame (not the rig) from the camera center which ...
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
This class hides the underlying projection model, like projection matrix, spherical camera...
void SetUndistortionInverseRad(double kc1, double kc2, double kc3, double kc4)
virtual ProjectionParametersPerspective * Clone() const
covariant virtual copy constructor for use in Projection
void SetUndistortionInverseRad3(double kc1, double kc2, double kc3)
void Mult(const T &scalar, Vector3< T > &dest) const
xmlNodePtr getFirstChild(const xmlNodePtr ParentNode)
Get the first child of a given parent, or NULL for no childs.
KMATRIX_TYPE GetFx() const
void AddAbsoluteCamera(ProjectionParametersBase *ppb)
Add a camera to the projection,the coordinates of the pose are given in absolute Coordinates, they are transformed to relative coordinates to the first camera internally.
virtual int XMLOut(const xmlNodePtr Node, XMLIO &XMLObject) const
specialization of XML write function for BIAS::Projection.
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
KMATRIX_TYPE GetHx() const
virtual bool DoesPointProjectIntoImage(const BIAS::HomgPoint3D &X, BIAS::HomgPoint2D &x, unsigned int cam=0, bool IgnoreDistortion=false) const
Checks if 3D point projects into specified image and returns belonging 2D image point.
virtual const bool DoIntrinsicsDiffer(const BIAS::Projection &p) const
Checks whether intrinsic parameters are different.
void SetUndistortionRad3(double kc1, double kc2, double kc3)
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Vector3< double > UnProjectToPoint(const HomgPoint2D &pos, double depth, unsigned int cam=0, bool IgnoreDistortion=false) const
calculates a 3D point in the global (not the rig) coordinate system, which belongs to the image posit...
void SetUndistortionBrown(double kc1, double kc2, double kc3, double kc4, double r0)
Set the lens undistortion parameters including the root of the polynomial.
BIAS_ProjParaPersp_DISTORTION_TYPE
std::string getNodeName(const xmlNodePtr Node) const
Get the name of a given Node.
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
virtual void SetAspectratio(const double AspectRatio)
Set CCD aspect ratio (i.e.
void CreatePerspective(const BIAS::Pose &pose, const BIAS::KMatrix &K, int width, int height, BIAS_ProjParaPersp_DISTORTION_TYPE radDistType=DISTYPE_DEF, const std::vector< double > &UndistortionCoefficients=std::vector< double >(4, 0.0))
Create a perspective camera and add to projection.
describes a projective 3D -> 2D mapping in homogenous coordinates
void SetFocalLengthAndAspect(double f, double AspectRatio)
Set the current camera focal length in pixel and the a spect ratio.
virtual HomgPoint2D Project(const HomgPoint3D &X, unsigned int cam=0, bool IgnoreDistortion=false) const
returns the 2d projection of X in camera cam, where X is given in the global coordinate frame (not th...
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual const BIAS::Pose & GetPose() const
Return complete pose object.
Vector3< T > & Normalize()
normalize this vector to length 1
static int ReadFromBBC(const std::string &filename, CameraData &cd, const double &addppx=0.0, const double &addppy=0.0, const bool silent=false)
read bbc's freeD date from a file
std::string getNodeContentString(const xmlNodePtr Node) const
Get the content of a given Node.
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Vector3< double > UnProjectToPointLocal(const HomgPoint2D &pos, const double &depth, unsigned int camSystem=0, unsigned int cam=0, bool IgnoreDistortion=false) const
calculates a 3D point in a local camera coordinate system specified by camSystem, which belongs to th...
int ReadFromCamParamFile(const std::string &filename, const bool silent=false)
Reads a camera data XML file and constructs the ProjectionParametersBase-pointer, using the Projectio...
int GetK(KMatrix &K)
calibration matrix