Visual Servoing Platform  version 3.5.1 under development (2023-06-06)
vpCameraParameters.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Camera intrinsic parameters.
33  *
34 *****************************************************************************/
35 
43 #ifndef vpCameraParameters_H
44 #define vpCameraParameters_H
45 
46 #include <vector>
47 
48 #include <visp3/core/vpColVector.h>
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpMatrix.h>
52 
53 #ifdef VISP_HAVE_NLOHMANN_JSON
54 #include<nlohmann/json.hpp>
55 #endif
56 
305 class VISP_EXPORT vpCameraParameters
306 {
309 
310 public:
311  typedef enum
312  {
315  ProjWithKannalaBrandtDistortion
316  } vpCameraParametersProjType;
317 
318  // generic functions
321  vpCameraParameters(double px, double py, double u0, double v0);
322  vpCameraParameters(double px, double py, double u0, double v0, double kud, double kdu);
323  vpCameraParameters(double px, double py, double u0, double v0, const std::vector<double> &distortion_coefficients);
324 
325  vpCameraParameters &operator=(const vpCameraParameters &c);
326  bool operator==(const vpCameraParameters &c) const;
327  bool operator!=(const vpCameraParameters &c) const;
328  virtual ~vpCameraParameters();
329 
330  void init();
331  void init(const vpCameraParameters &c);
332  void initFromCalibrationMatrix(const vpMatrix &_K);
333  void initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov, const double &vfov);
334  void initPersProjWithoutDistortion(double px, double py, double u0, double v0);
335  void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu);
336  void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0,
337  const std::vector<double> &distortion_coefficients);
338 
346  inline bool isFovComputed() const { return isFov; }
347 
348  void computeFov(const unsigned int &w, const unsigned int &h);
349 
357  inline double getHorizontalFovAngle() const
358  {
359  if (!isFov) {
360  vpTRACE("Warning: The FOV is not computed, getHorizontalFovAngle() "
361  "won't be significant.");
362  }
363  return m_hFovAngle;
364  }
365 
373  inline double getVerticalFovAngle() const
374  {
375  if (!isFov) {
376  vpTRACE("Warning: The FOV is not computed, getVerticalFovAngle() won't "
377  "be significant.");
378  }
379  return m_vFovAngle;
380  }
381 
394  inline std::vector<vpColVector> getFovNormals() const
395  {
396  if (!isFov) {
397  vpTRACE("Warning: The FOV is not computed, getFovNormals() won't be "
398  "significant.");
399  }
400  return fovNormals;
401  }
402 
403  inline double get_px() const { return px; }
404  inline double get_px_inverse() const { return inv_px; }
405  inline double get_py_inverse() const { return inv_py; }
406  inline double get_py() const { return py; }
407  inline double get_u0() const { return u0; }
408  inline double get_v0() const { return v0; }
409  inline double get_kud() const { return kud; }
410  inline double get_kdu() const { return kdu; }
411  inline std::vector<double> getKannalaBrandtDistortionCoefficients() const { return m_dist_coefs; }
412 
413  inline vpCameraParametersProjType get_projModel() const { return projModel; }
414 
415  vpMatrix get_K() const;
416  vpMatrix get_K_inverse() const;
417 
418  void printParameters();
419  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpCameraParameters &cam);
420 
421 private:
422  static const double DEFAULT_U0_PARAMETER;
423  static const double DEFAULT_V0_PARAMETER;
424  static const double DEFAULT_PX_PARAMETER;
425  static const double DEFAULT_PY_PARAMETER;
426  static const double DEFAULT_KUD_PARAMETER;
427  static const double DEFAULT_KDU_PARAMETER;
428  static const vpCameraParametersProjType DEFAULT_PROJ_TYPE;
429 
430  double px, py;
431  double u0, v0;
432  double kud;
433  double kdu;
434  std::vector<double> m_dist_coefs;
435 
436  unsigned int width;
437  unsigned int height;
438  bool isFov;
439  double m_hFovAngle;
440  double m_vFovAngle;
441  std::vector<vpColVector> fovNormals;
442 
443  double inv_px, inv_py;
444 
445  vpCameraParametersProjType projModel;
446 #ifdef VISP_HAVE_NLOHMANN_JSON
447  friend void to_json(nlohmann::json &j, const vpCameraParameters &cam);
448  friend void from_json(const nlohmann::json &j, vpCameraParameters &cam);
449 #endif
450 };
451 
452 #ifdef VISP_HAVE_NLOHMANN_JSON
453 #include<nlohmann/json.hpp>
454 NLOHMANN_JSON_SERIALIZE_ENUM(vpCameraParameters::vpCameraParametersProjType, {
455  {vpCameraParameters::perspectiveProjWithoutDistortion, "perspectiveWithoutDistortion"},
456  {vpCameraParameters::perspectiveProjWithDistortion, "perspectiveWithDistortion"},
457  {vpCameraParameters::ProjWithKannalaBrandtDistortion, "kannalaBrandtDistortion"}
458  });
466 inline void to_json(nlohmann::json &j, const vpCameraParameters &cam)
467 {
468  j["px"] = cam.px;
469  j["py"] = cam.py;
470  j["u0"] = cam.u0;
471  j["v0"] = cam.v0;
472  j["model"] = cam.projModel;
473 
474  switch (cam.projModel) {
476  {
477  j["kud"] = cam.kud;
478  j["kdu"] = cam.kdu;
479  break;
480  }
482  {
483  j["dist_coeffs"] = cam.m_dist_coefs;
484  break;
485  }
486  default:
487  break;
488  }
489 }
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:154
#define vpTRACE
Definition: vpDebug.h:416