Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpCameraParameters.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Camera intrinsic parameters.
32  */
33 
41 #ifndef VP_CAMERA_PARAMETERS_H
42 #define VP_CAMERA_PARAMETERS_H
43 
44 #include <iostream>
45 #include <vector>
46 
47 #include <visp3/core/vpConfig.h>
48 #include <visp3/core/vpColVector.h>
49 #include <visp3/core/vpMatrix.h>
50 
51 #ifdef VISP_HAVE_NLOHMANN_JSON
52 #include<nlohmann/json.hpp>
53 #endif
54 
309 class VISP_EXPORT vpCameraParameters
310 {
313 
314 public:
315  typedef enum
316  {
319  ProjWithKannalaBrandtDistortion
320  } vpCameraParametersProjType;
321 
322  // generic functions
325  vpCameraParameters(double px, double py, double u0, double v0);
326  vpCameraParameters(double px, double py, double u0, double v0, double kud, double kdu);
327  vpCameraParameters(double px, double py, double u0, double v0, const std::vector<double> &distortion_coefficients);
328 
329  vpCameraParameters &operator=(const vpCameraParameters &c);
330  bool operator==(const vpCameraParameters &c) const;
331  bool operator!=(const vpCameraParameters &c) const;
332  virtual ~vpCameraParameters();
333 
334  void init();
335  void init(const vpCameraParameters &c);
336  void initFromCalibrationMatrix(const vpMatrix &_K);
337  void initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov, const double &vfov);
338  void initPersProjWithoutDistortion(double px, double py, double u0, double v0);
339  void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu);
340  void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0,
341  const std::vector<double> &distortion_coefficients);
342 
350  inline bool isFovComputed() const { return m_isFov; }
351 
352  void computeFov(const unsigned int &w, const unsigned int &h);
353 
361  inline double getHorizontalFovAngle() const
362  {
363  if (!m_isFov) {
364  std::cout << "Warning: The FOV is not computed, getHorizontalFovAngle() won't be significant." << std::endl;
365  }
366  return m_hFovAngle;
367  }
368 
376  inline double getVerticalFovAngle() const
377  {
378  if (!m_isFov) {
379  std::cout << "Warning: The FOV is not computed, getVerticalFovAngle() won't be significant." << std::endl;
380  }
381  return m_vFovAngle;
382  }
383 
396  inline std::vector<vpColVector> getFovNormals() const
397  {
398  if (!m_isFov) {
399  std::cout << "Warning: The FOV is not computed, getFovNormals() won't be significant." << std::endl;
400  }
401  return m_fovNormals;
402  }
403 
404  inline double get_px() const { return m_px; }
405  inline double get_px_inverse() const { return m_inv_px; }
406  inline double get_py_inverse() const { return m_inv_py; }
407  inline double get_py() const { return m_py; }
408  inline double get_u0() const { return m_u0; }
409  inline double get_v0() const { return m_v0; }
410  inline double get_kud() const { return m_kud; }
411  inline double get_kdu() const { return m_kdu; }
412  inline std::vector<double> getKannalaBrandtDistortionCoefficients() const { return m_dist_coefs; }
413 
414  inline vpCameraParametersProjType get_projModel() const { return m_projModel; }
415 
416  vpMatrix get_K() const;
417  vpMatrix get_K_inverse() const;
418 
419  void printParameters();
420  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpCameraParameters &cam);
421 
422 private:
423  static const double DEFAULT_U0_PARAMETER;
424  static const double DEFAULT_V0_PARAMETER;
425  static const double DEFAULT_PX_PARAMETER;
426  static const double DEFAULT_PY_PARAMETER;
427  static const double DEFAULT_KUD_PARAMETER;
428  static const double DEFAULT_KDU_PARAMETER;
429  static const vpCameraParametersProjType DEFAULT_PROJ_TYPE;
430 
431  double m_px, m_py;
432  double m_u0, m_v0;
433  double m_kud;
434  double m_kdu;
435  std::vector<double> m_dist_coefs;
436 
437  unsigned int m_width;
438  unsigned int m_height;
439  bool m_isFov;
440  double m_hFovAngle;
441  double m_vFovAngle;
442  std::vector<vpColVector> m_fovNormals;
443 
444  double m_inv_px, m_inv_py;
445 
446  vpCameraParametersProjType m_projModel;
447 #ifdef VISP_HAVE_NLOHMANN_JSON
448  friend void to_json(nlohmann::json &j, const vpCameraParameters &cam);
449  friend void from_json(const nlohmann::json &j, vpCameraParameters &cam);
450 #endif
451 };
452 
453 #ifdef VISP_HAVE_NLOHMANN_JSON
454 #include<nlohmann/json.hpp>
455 NLOHMANN_JSON_SERIALIZE_ENUM(vpCameraParameters::vpCameraParametersProjType, {
456  {vpCameraParameters::perspectiveProjWithoutDistortion, "perspectiveWithoutDistortion"},
457  {vpCameraParameters::perspectiveProjWithDistortion, "perspectiveWithDistortion"},
458  {vpCameraParameters::ProjWithKannalaBrandtDistortion, "kannalaBrandtDistortion"}
459  });
460 
467 inline void to_json(nlohmann::json &j, const vpCameraParameters &cam)
468 {
469  j["px"] = cam.m_px;
470  j["py"] = cam.m_py;
471  j["u0"] = cam.m_u0;
472  j["v0"] = cam.m_v0;
473  j["model"] = cam.m_projModel;
474 
475  switch (cam.m_projModel) {
477  {
478  j["kud"] = cam.m_kud;
479  j["kdu"] = cam.m_kdu;
480  break;
481  }
483  {
484  j["dist_coeffs"] = cam.m_dist_coefs;
485  break;
486  }
488  break;
489  default:
490  break;
491  }
492 }
493 
520 inline void from_json(const nlohmann::json &j, vpCameraParameters &cam)
521 {
522  const double px = j.at("px").get<double>();
523  const double py = j.at("py").get<double>();
524  const double u0 = j.at("u0").get<double>();
525  const double v0 = j.at("v0").get<double>();
527 
528  switch (model) {
530  {
531  cam.initPersProjWithoutDistortion(px, py, u0, v0);
532  break;
533  }
535  {
536  const double kud = j.at("kud").get<double>();
537  const double kdu = j.at("kdu").get<double>();
538  cam.initPersProjWithDistortion(px, py, u0, v0, kud, kdu);
539  break;
540  }
542  {
543  const std::vector<double> coeffs = j.at("dist_coeffs").get<std::vector<double>>();
544  cam.initProjWithKannalaBrandtDistortion(px, py, u0, v0, coeffs);
545  break;
546  }
547  default:
548  break;
549  }
550 }
551 #endif
552 END_VISP_NAMESPACE
553 #endif
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
std::vector< double > getKannalaBrandtDistortionCoefficients() const
bool isFovComputed() const
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ ProjWithKannalaBrandtDistortion
Projection with Kannala-Brandt distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
std::vector< vpColVector > getFovNormals() const
void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu)
double getHorizontalFovAngle() const
double getVerticalFovAngle() const
double get_px_inverse() const
double get_kdu() const
double get_py_inverse() const
vpCameraParametersProjType get_projModel() const
void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0, const std::vector< double > &distortion_coefficients)
double get_kud() const
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169