18 #include <itkMutexLockHolder.h>
23 : m_Valid(false), m_Mutex(
itk::FastMutexLock::
New())
34 , m_Mutex(
itk::FastMutexLock::
New())
47 this->GetDistorsionCoeffsAsPoint4D() &&
49 this->GetFocalPoint() &&
51 == this->GetPrincipalPoint();
67 vnl_matrix_fixed<mitk::ScalarType, 3, 3>
70 vnl_matrix_fixed<mitk::ScalarType, 3, 3> mat;
75 mat(0,0) = m_CameraMatrix.at<
double>(0,0);
76 mat(1,1) = m_CameraMatrix.at<
double>(1,1);
78 mat(0,2) = m_CameraMatrix.at<
double>(0,2);
79 mat(1,2) = m_CameraMatrix.at<
double>(1,2);
86 const vnl_matrix_fixed<mitk::ScalarType, 3, 3>& _CameraMatrix )
89 m_CameraMatrix.at<
double>(0,0) = _CameraMatrix(0,0);
90 m_CameraMatrix.at<
double>(1,1) = _CameraMatrix(1,1);
92 m_CameraMatrix.at<
double>(0,2) = _CameraMatrix(0,2);
93 m_CameraMatrix.at<
double>(1,2) = _CameraMatrix(1,2);
96 vnl_matrix_fixed<mitk::ScalarType, 3, 4>
99 vnl_matrix_fixed<mitk::ScalarType, 3, 4> mat;
101 mat.update( this->GetVnlCameraMatrix().as_matrix() );
107 ,
const cv::Mat& _DistorsionCoeffs)
111 if( _CameraMatrix.cols != 3 || _CameraMatrix.rows != 3)
112 throw std::invalid_argument(
"Wrong format of camera matrix. Should be 3x3"
116 _DistorsionCoeffs.rows == 1,
"Wrong format of distorsion coefficients"
117 " vector. Should be 5x1 double.");
119 m_CameraMatrix = _CameraMatrix.clone();
120 m_DistorsionCoeffs = _DistorsionCoeffs.clone();
134 m_CameraMatrix.at<
double>(0,0) = focalPoint[0];
135 m_CameraMatrix.at<
double>(1,1) = focalPoint[1];
137 m_CameraMatrix.at<
double>(0,2) = principalPoint[0];
138 m_CameraMatrix.at<
double>(1,2) = principalPoint[1];
140 m_DistorsionCoeffs.at<
double>(0,0) = distortionCoefficients[0];
141 m_DistorsionCoeffs.at<
double>(0,1) = distortionCoefficients[1];
142 m_DistorsionCoeffs.at<
double>(0,2) = distortionCoefficients[2];
143 m_DistorsionCoeffs.at<
double>(0,3) = distortionCoefficients[3];
152 m_CameraMatrix.at<
double>(0,0) = x;
153 m_CameraMatrix.at<
double>(1,1) = y;
162 m_CameraMatrix.at<
double>(0,2) = x;
163 m_CameraMatrix.at<
double>(1,2) = y;
169 double p1,
double p2 )
175 m_DistorsionCoeffs.at<
double>(0,0) = k1;
176 m_DistorsionCoeffs.at<
double>(0,1) = k2;
177 m_DistorsionCoeffs.at<
double>(0,2) = p1;
178 m_DistorsionCoeffs.at<
double>(0,3) = p2;
186 return m_CameraMatrix.clone();
192 return m_DistorsionCoeffs.clone();
204 std::ostringstream s; s.precision(12);
205 const cv::Mat& CameraMatrix = m_CameraMatrix;
206 const cv::Mat& DistorsionCoeffs = m_DistorsionCoeffs;
208 s.str(
""); s << this->GetNameOfClass() <<
": ";
209 s <<
"fx = " << CameraMatrix.at<
double>(0,0);
210 s <<
", fy = " << CameraMatrix.at<
double>(1,1);
211 s <<
", cx = " << CameraMatrix.at<
double>(0,2);
212 s <<
", cy = " << CameraMatrix.at<
double>(1,2);
214 s <<
", k1 = " << DistorsionCoeffs.at<
double>(0,0);
215 s <<
", k2 = " << DistorsionCoeffs.at<
double>(0,1);
216 s <<
", p1 = " << DistorsionCoeffs.at<
double>(0,2);
217 s <<
", p2 = " << DistorsionCoeffs.at<
double>(0,3);
226 elem->SetValue(this->GetNameOfClass());
227 std::ostringstream s; s.precision(12);
228 const cv::Mat& CameraMatrix = m_CameraMatrix;
229 s.str(
""); s << CameraMatrix.at<
double>(0,0);
230 elem->SetAttribute(
"fx", s.str() );
231 s.str(
""); s << CameraMatrix.at<
double>(1,1);
232 elem->SetAttribute(
"fy", s.str() );
233 s.str(
""); s << CameraMatrix.at<
double>(0,2);
234 elem->SetAttribute(
"cx", s.str() );
235 s.str(
""); s << CameraMatrix.at<
double>(1,2);
236 elem->SetAttribute(
"cy", s.str() );
238 const cv::Mat& DistorsionCoeffs = m_DistorsionCoeffs;
239 s.str(
""); s << DistorsionCoeffs.at<
double>(0,0);
240 elem->SetAttribute(
"k1", s.str() );
241 s.str(
""); s << DistorsionCoeffs.at<
double>(0,1);
242 elem->SetAttribute(
"k2", s.str() );
243 s.str(
""); s << DistorsionCoeffs.at<
double>(0,2);
244 elem->SetAttribute(
"p1", s.str() );
245 s.str(
""); s << DistorsionCoeffs.at<
double>(0,3);
246 elem->SetAttribute(
"p2", s.str() );
247 elem->SetAttribute(
"Valid", m_Valid);
255 assert( elem->ValueStr() ==
"results" );
256 cv::Mat CameraMatrix = cv::Mat::zeros(3, 3, cv::DataType<double>::type);
257 CameraMatrix.at<
double>(2,2) = 1.0;
258 cv::Mat DistorsionCoeffs = cv::Mat::zeros(1, 5, cv::DataType<double>::type);
260 TiXmlElement* focus_lenXElem = elem->FirstChildElement(
"focus_lenX");
262 CameraMatrix.at<
double>(0,0) = atof( focus_lenXElem->GetText() );
264 TiXmlElement* focus_lenYElem = elem->FirstChildElement(
"focus_lenY");
266 CameraMatrix.at<
double>(1,1) = atof( focus_lenYElem->GetText() );
268 TiXmlElement* PrincipalXElem = elem->FirstChildElement(
"PrincipalX");
270 CameraMatrix.at<
double>(0,2) = atof( PrincipalXElem->GetText() );
272 TiXmlElement* PrincipalYElem = elem->FirstChildElement(
"PrincipalY");
274 CameraMatrix.at<
double>(1,2) = atof( PrincipalYElem->GetText() );
278 TiXmlElement* Dist1Elem = elem->FirstChildElement(
"Dist1");
280 DistorsionCoeffs.at<
double>(0,0) = atof( Dist1Elem->GetText() );
282 TiXmlElement* Dist2Elem = elem->FirstChildElement(
"Dist2");
284 DistorsionCoeffs.at<
double>(0,1) = atof( Dist2Elem->GetText() );
286 TiXmlElement* Dist3Elem = elem->FirstChildElement(
"Dist3");
288 DistorsionCoeffs.at<
double>(0,2) = atof( Dist3Elem->GetText() );
290 TiXmlElement* Dist4Elem = elem->FirstChildElement(
"Dist4");
292 DistorsionCoeffs.at<
double>(0,3) = atof( Dist4Elem->GetText() );
295 elem->QueryIntAttribute(
"Valid", &valid);
299 m_Valid =
static_cast<bool>(valid);
300 m_CameraMatrix = CameraMatrix;
301 m_DistorsionCoeffs = DistorsionCoeffs;
312 if(elem->QueryStringAttribute(
"file", &filename) == TIXML_SUCCESS)
314 this->FromXMLFile(filename);
317 else if(strcmp(elem->Value(),
"CalibrationProject") == 0)
319 this->FromGMLCalibrationXML(elem->FirstChildElement(
"results"));
324 if(strcmp(elem->Value(), this->GetNameOfClass()) != 0)
325 elem = elem->FirstChildElement(this->GetNameOfClass());
327 std::ostringstream err;
329 cv::Mat CameraMatrix = cv::Mat::zeros(3, 3, cv::DataType<double>::type);
330 CameraMatrix.at<
double>(2,2) = 1.0;
332 if(elem->QueryDoubleAttribute(
"fx", &val) == TIXML_SUCCESS)
333 CameraMatrix.at<
double>(0,0) = val;
337 if(elem->QueryDoubleAttribute(
"fy", &val) == TIXML_SUCCESS)
338 CameraMatrix.at<
double>(1,1) = val;
342 if(elem->QueryDoubleAttribute(
"cx", &val) == TIXML_SUCCESS)
343 CameraMatrix.at<
double>(0,2) = val;
347 if(elem->QueryDoubleAttribute(
"cy", &val) == TIXML_SUCCESS)
348 CameraMatrix.at<
double>(1,2) = val;
353 endodebug(
"creating DistorsionCoeffs from XML file")
354 cv::Mat DistorsionCoeffs = cv::Mat::zeros(1, 5, cv::DataType<double>::type);
355 if(elem->QueryDoubleAttribute(
"k1", &val) == TIXML_SUCCESS)
356 DistorsionCoeffs.at<
double>(0,0) = val;
360 if(elem->QueryDoubleAttribute(
"k2", &val) == TIXML_SUCCESS)
361 DistorsionCoeffs.at<
double>(0,1) = val;
365 if(elem->QueryDoubleAttribute(
"p1", &val) == TIXML_SUCCESS)
366 DistorsionCoeffs.at<
double>(0,2) = val;
370 if(elem->QueryDoubleAttribute(
"p2", &val) == TIXML_SUCCESS)
371 DistorsionCoeffs.at<
double>(0,3) = val;
375 DistorsionCoeffs.at<
double>(0,4) = 0.0;
382 std::string errorStr = err.str();
383 int errLength = errorStr.length();
386 errorStr = errorStr.substr(0, errLength-2);
387 errorStr.append(
" not found");
388 throw std::invalid_argument(err.str());
392 elem->QueryIntAttribute(
"Valid", &valid);
396 m_Valid =
static_cast<bool>(valid);
397 m_CameraMatrix = CameraMatrix;
398 m_DistorsionCoeffs = DistorsionCoeffs;
407 double FocalLengthX = m_CameraMatrix.at<
double>(0,0);
414 double FocalLengthY = m_CameraMatrix.at<
double>(1,1);;
420 double PrincipalPointX = m_CameraMatrix.at<
double>(0,2);
421 return PrincipalPointX;
426 double PrincipalPointY = m_CameraMatrix.at<
double>(1,2);
427 return PrincipalPointY;
434 coeffs[0] = m_DistorsionCoeffs.at<
double>(0,0);
435 coeffs[1] = m_DistorsionCoeffs.at<
double>(0,1);
436 coeffs[2] = m_DistorsionCoeffs.at<
double>(0,2);
437 coeffs[3] = m_DistorsionCoeffs.at<
double>(0,3);
445 p[0] = this->GetFocalLengthX();
446 p[1] = this->GetFocalLengthY();
454 p[0] = this->GetPrincipalPointX();
455 p[1] = this->GetPrincipalPointY();
460 vnl_vector_fixed<mitk::ScalarType, 2>
463 vnl_vector_fixed<mitk::ScalarType, 2> vec;
464 vec[0] = this->GetFocalLengthX();
465 vec[1] = this->GetFocalLengthY();
469 vnl_vector_fixed<mitk::ScalarType, 2>
472 vnl_vector_fixed<mitk::ScalarType, 2> vec;
473 vec[0] = this->GetPrincipalPointX();
474 vec[1] = this->GetPrincipalPointY();
486 return this->ToString();
490 const std::string& varName)
492 std::ostringstream s;
493 s << varName <<
" = [" << this->GetFocalLengthX() <<
" 0 "
494 << this->GetPrincipalPointX() <<
"; 0 " <<
495 this->GetFocalLengthY() <<
" " << this->GetPrincipalPointY() <<
";"
509 result->UnRegister();
double GetPrincipalPointX() const
itk::SmartPointer< Self > Pointer
void FromXML(TiXmlElement *elem) override
double GetPrincipalPointY() const
MITKCORE_EXPORT std::ostream & operator<<(std::ostream &o, DataNode::Pointer &dtn)
class representing camera intrinsics and related functions
vnl_matrix_fixed< mitk::ScalarType, 3, 3 > GetVnlCameraMatrix() const
void FromGMLCalibrationXML(TiXmlElement *elem)
std::string ToOctaveString(const std::string &varName="CameraIntrinsics")
std::string ToString() const
#define endoAssertMsg(a, msg)
DataCollection - Class to facilitate loading/accessing structured data.
virtual ~CameraIntrinsics()
vnl_vector_fixed< mitk::ScalarType, 2 > GetPrincipalPointAsVnlVector() const
double GetFocalLengthX() const
void SetFocalLength(double x, double y)
void SetIntrinsics(const cv::Mat &_CameraMatrix, const cv::Mat &_DistorsionCoeffs)
double GetFocalLengthY() const
mitk::Point4D GetDistorsionCoeffsAsPoint4D() const
static const std::string filename
mitk::Point3D GetPrincipalPoint() const
void SetDistorsionCoeffs(double k1, double k2, double p1, double p2)
cv::Mat m_DistorsionCoeffs
void SetValid(bool valid)
bool Equals(const CameraIntrinsics *other) const
vnl_vector_fixed< mitk::ScalarType, 2 > GetFocalPointAsVnlVector() const
void SetPrincipalPoint(double x, double y)
interface for all classes able to write themselves to XML files
void Copy(const CameraIntrinsics *other)
cv::Mat GetDistorsionCoeffs()
void ToXML(TiXmlElement *elem) const override
vnl_matrix_fixed< mitk::ScalarType, 3, 4 > GetVnlCameraMatrix3x4() const
mitk::Point3D GetFocalPoint() const
void SetCameraMatrix(const vnl_matrix_fixed< mitk::ScalarType, 3, 3 > &_CameraMatrix)
cv::Mat GetCameraMatrix() const
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.