Visual Servoing Platform  version 3.6.1 under development (2024-04-18)
vpCameraParameters.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 _vpCameraParameters_h_
42 #define _vpCameraParameters_h_
43 
44 #include <vector>
45 
46 #include <visp3/core/vpColVector.h>
47 #include <visp3/core/vpConfig.h>
48 #include <visp3/core/vpDebug.h>
49 #include <visp3/core/vpMatrix.h>
50 
51 #ifdef VISP_HAVE_NLOHMANN_JSON
52 #include<nlohmann/json.hpp>
53 #endif
54 
303 class VISP_EXPORT vpCameraParameters
304 {
307 
308 public:
309  typedef enum
310  {
313  ProjWithKannalaBrandtDistortion
314  } vpCameraParametersProjType;
315 
316  // generic functions
319  vpCameraParameters(double px, double py, double u0, double v0);
320  vpCameraParameters(double px, double py, double u0, double v0, double kud, double kdu);
321  vpCameraParameters(double px, double py, double u0, double v0, const std::vector<double> &distortion_coefficients);
322 
323  vpCameraParameters &operator=(const vpCameraParameters &c);
324  bool operator==(const vpCameraParameters &c) const;
325  bool operator!=(const vpCameraParameters &c) const;
326  virtual ~vpCameraParameters();
327 
328  void init();
329  void init(const vpCameraParameters &c);
330  void initFromCalibrationMatrix(const vpMatrix &_K);
331  void initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov, const double &vfov);
332  void initPersProjWithoutDistortion(double px, double py, double u0, double v0);
333  void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu);
334  void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0,
335  const std::vector<double> &distortion_coefficients);
336 
344  inline bool isFovComputed() const { return m_isFov; }
345 
346  void computeFov(const unsigned int &w, const unsigned int &h);
347 
355  inline double getHorizontalFovAngle() const
356  {
357  if (!m_isFov) {
358  vpTRACE("Warning: The FOV is not computed, getHorizontalFovAngle() "
359  "won't be significant.");
360  }
361  return m_hFovAngle;
362  }
363 
371  inline double getVerticalFovAngle() const
372  {
373  if (!m_isFov) {
374  vpTRACE("Warning: The FOV is not computed, getVerticalFovAngle() won't "
375  "be significant.");
376  }
377  return m_vFovAngle;
378  }
379 
392  inline std::vector<vpColVector> getFovNormals() const
393  {
394  if (!m_isFov) {
395  vpTRACE("Warning: The FOV is not computed, getFovNormals() won't be "
396  "significant.");
397  }
398  return m_fovNormals;
399  }
400 
401  inline double get_px() const { return m_px; }
402  inline double get_px_inverse() const { return m_inv_px; }
403  inline double get_py_inverse() const { return m_inv_py; }
404  inline double get_py() const { return m_py; }
405  inline double get_u0() const { return m_u0; }
406  inline double get_v0() const { return m_v0; }
407  inline double get_kud() const { return m_kud; }
408  inline double get_kdu() const { return m_kdu; }
409  inline std::vector<double> getKannalaBrandtDistortionCoefficients() const { return m_dist_coefs; }
410 
411  inline vpCameraParametersProjType get_projModel() const { return m_projModel; }
412 
413  vpMatrix get_K() const;
414  vpMatrix get_K_inverse() const;
415 
416  void printParameters();
417  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpCameraParameters &cam);
418 
419 private:
420  static const double DEFAULT_U0_PARAMETER;
421  static const double DEFAULT_V0_PARAMETER;
422  static const double DEFAULT_PX_PARAMETER;
423  static const double DEFAULT_PY_PARAMETER;
424  static const double DEFAULT_KUD_PARAMETER;
425  static const double DEFAULT_KDU_PARAMETER;
426  static const vpCameraParametersProjType DEFAULT_PROJ_TYPE;
427 
428  double m_px, m_py;
429  double m_u0, m_v0;
430  double m_kud;
431  double m_kdu;
432  std::vector<double> m_dist_coefs;
433 
434  unsigned int m_width;
435  unsigned int m_height;
436  bool m_isFov;
437  double m_hFovAngle;
438  double m_vFovAngle;
439  std::vector<vpColVector> m_fovNormals;
440 
441  double m_inv_px, m_inv_py;
442 
443  vpCameraParametersProjType m_projModel;
444 #ifdef VISP_HAVE_NLOHMANN_JSON
445  friend void to_json(nlohmann::json &j, const vpCameraParameters &cam);
446  friend void from_json(const nlohmann::json &j, vpCameraParameters &cam);
447 #endif
448 };
449 
450 #ifdef VISP_HAVE_NLOHMANN_JSON
451 #include<nlohmann/json.hpp>
452 NLOHMANN_JSON_SERIALIZE_ENUM(vpCameraParameters::vpCameraParametersProjType, {
453  {vpCameraParameters::perspectiveProjWithoutDistortion, "perspectiveWithoutDistortion"},
454  {vpCameraParameters::perspectiveProjWithDistortion, "perspectiveWithDistortion"},
455  {vpCameraParameters::ProjWithKannalaBrandtDistortion, "kannalaBrandtDistortion"}
456  });
457 
464 inline void to_json(nlohmann::json &j, const vpCameraParameters &cam)
465 {
466  j["px"] = cam.m_px;
467  j["py"] = cam.m_py;
468  j["u0"] = cam.m_u0;
469  j["v0"] = cam.m_v0;
470  j["model"] = cam.m_projModel;
471 
472  switch (cam.m_projModel) {
474  {
475  j["kud"] = cam.m_kud;
476  j["kdu"] = cam.m_kdu;
477  break;
478  }
480  {
481  j["dist_coeffs"] = cam.m_dist_coefs;
482  break;
483  }
485  break;
486  default:
487  break;
488  }
489 }
490 
517 inline void from_json(const nlohmann::json &j, vpCameraParameters &cam)
518 {
519  const double px = j.at("px").get<double>();
520  const double py = j.at("py").get<double>();
521  const double u0 = j.at("u0").get<double>();
522  const double v0 = j.at("v0").get<double>();
524 
525  switch (model) {
527  {
528  cam.initPersProjWithoutDistortion(px, py, u0, v0);
529  break;
530  }
532  {
533  const double kud = j.at("kud").get<double>();
534  const double kdu = j.at("kdu").get<double>();
535  cam.initPersProjWithDistortion(px, py, u0, v0, kud, kdu);
536  break;
537  }
539  {
540  const std::vector<double> coeffs = j.at("dist_coeffs").get<std::vector<double>>();
541  cam.initProjWithKannalaBrandtDistortion(px, py, u0, v0, coeffs);
542  break;
543  }
544  }
545 }
546 #endif
547 
548 #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:146
#define vpTRACE
Definition: vpDebug.h:405