Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
40
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/vpException.h>
50#include <visp3/core/vpMatrix.h>
51
52#ifdef VISP_HAVE_NLOHMANN_JSON
53#include VISP_NLOHMANN_JSON(json.hpp)
54#endif
55
309
310class VISP_EXPORT vpCameraParameters
311{
314
315public:
322
323 // generic functions
326 vpCameraParameters(double px, double py, double u0, double v0);
327 vpCameraParameters(double px, double py, double u0, double v0, double kud, double kdu);
328 vpCameraParameters(double px, double py, double u0, double v0, const std::vector<double> &distortion_coefficients);
329
330 vpCameraParameters &operator=(const vpCameraParameters &c);
331 bool operator==(const vpCameraParameters &c) const;
332 bool operator!=(const vpCameraParameters &c) const;
333 virtual ~vpCameraParameters();
334
335 void init();
336 void init(const vpCameraParameters &c);
337 void initFromCalibrationMatrix(const vpMatrix &K);
338 void initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov, const double &vfov);
339 void initPersProjWithoutDistortion(double px, double py, double u0, double v0);
340 void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu);
341 void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0,
342 const std::vector<double> &distortion_coefficients);
343
351 inline bool isFovComputed() const { return m_isFov; }
352
353 void computeFov(const unsigned int &w, const unsigned int &h);
354
362 inline double getHorizontalFovAngle() const
363 {
364 if (!m_isFov) {
365 std::cout << "Warning: The FOV is not computed, getHorizontalFovAngle() won't be significant." << std::endl;
366 }
367 return m_hFovAngle;
368 }
369
377 inline double getVerticalFovAngle() const
378 {
379 if (!m_isFov) {
380 std::cout << "Warning: The FOV is not computed, getVerticalFovAngle() won't be significant." << std::endl;
381 }
382 return m_vFovAngle;
383 }
384
397 inline std::vector<vpColVector> getFovNormals() const
398 {
399 if (!m_isFov) {
400 std::cout << "Warning: The FOV is not computed, getFovNormals() won't be significant." << std::endl;
401 }
402 return m_fovNormals;
403 }
404
405 inline double get_px() const { return m_px; }
406 inline double get_px_inverse() const { return m_inv_px; }
407 inline double get_py_inverse() const { return m_inv_py; }
408 inline double get_py() const { return m_py; }
409 inline double get_u0() const { return m_u0; }
410 inline double get_v0() const { return m_v0; }
411 inline double get_kud() const { return m_kud; }
412 inline double get_kdu() const { return m_kdu; }
413 inline std::vector<double> getKannalaBrandtDistortionCoefficients() const { return m_dist_coefs; }
414
415 inline vpCameraParametersProjType get_projModel() const { return m_projModel; }
416
417 vpMatrix get_K() const;
418 vpMatrix get_K_inverse() const;
419
420 void printParameters();
421 friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpCameraParameters &cam);
422
423private:
424 static const double DEFAULT_U0_PARAMETER;
425 static const double DEFAULT_V0_PARAMETER;
426 static const double DEFAULT_PX_PARAMETER;
427 static const double DEFAULT_PY_PARAMETER;
428 static const double DEFAULT_KUD_PARAMETER;
429 static const double DEFAULT_KDU_PARAMETER;
430 static const vpCameraParametersProjType DEFAULT_PROJ_TYPE;
431
432 double m_px, m_py;
433 double m_u0, m_v0;
434 double m_kud;
435 double m_kdu;
436 std::vector<double> m_dist_coefs;
437
438 unsigned int m_width;
439 unsigned int m_height;
440 bool m_isFov;
441 double m_hFovAngle;
442 double m_vFovAngle;
443 std::vector<vpColVector> m_fovNormals;
444
445 double m_inv_px, m_inv_py;
446
447 vpCameraParametersProjType m_projModel;
448#ifdef VISP_HAVE_NLOHMANN_JSON
449 friend void to_json(nlohmann::json &j, const vpCameraParameters &cam);
450 friend void from_json(const nlohmann::json &j, vpCameraParameters &cam);
451#endif
452};
453
454#ifdef VISP_HAVE_NLOHMANN_JSON
455
456#if defined(__clang__)
457// Mute warning : declaration requires an exit-time destructor [-Wexit-time-destructors]
458// message : expanded from macro 'NLOHMANN_JSON_SERIALIZE_ENUM'
459# pragma clang diagnostic push
460# pragma clang diagnostic ignored "-Wexit-time-destructors"
461#endif
462
463#include VISP_NLOHMANN_JSON(json.hpp)
464NLOHMANN_JSON_SERIALIZE_ENUM(vpCameraParameters::vpCameraParametersProjType, {
465 {vpCameraParameters::perspectiveProjWithoutDistortion, "perspectiveWithoutDistortion"},
466 {vpCameraParameters::perspectiveProjWithDistortion, "perspectiveWithDistortion"},
467 {vpCameraParameters::ProjWithKannalaBrandtDistortion, "kannalaBrandtDistortion"}
468 });
469
470#if defined(__clang__)
471# pragma clang diagnostic pop
472#endif
473
480inline void to_json(nlohmann::json &j, const vpCameraParameters &cam)
481{
482 j["px"] = cam.m_px;
483 j["py"] = cam.m_py;
484 j["u0"] = cam.m_u0;
485 j["v0"] = cam.m_v0;
486 j["model"] = cam.m_projModel;
487
488 switch (cam.m_projModel) {
490 {
491 j["kud"] = cam.m_kud;
492 j["kdu"] = cam.m_kdu;
493 break;
494 }
496 {
497 j["dist_coeffs"] = cam.m_dist_coefs;
498 break;
499 }
501 break;
502 default: {
503 throw(vpException(vpException::fatalError, "Case not handled in to_json(nlohmann::json &, const vpCameraParameters &)"));
504 }
505 }
506}
507
534inline void from_json(const nlohmann::json &j, vpCameraParameters &cam)
535{
536 const double px = j.at("px").get<double>();
537 const double py = j.at("py").get<double>();
538 const double u0 = j.at("u0").get<double>();
539 const double v0 = j.at("v0").get<double>();
541
542 switch (model) {
544 {
545 cam.initPersProjWithoutDistortion(px, py, u0, v0);
546 break;
547 }
549 {
550 const double kud = j.at("kud").get<double>();
551 const double kdu = j.at("kdu").get<double>();
552 cam.initPersProjWithDistortion(px, py, u0, v0, kud, kdu);
553 break;
554 }
556 {
557 const std::vector<double> coeffs = j.at("dist_coeffs").get<std::vector<double>>();
558 cam.initProjWithKannalaBrandtDistortion(px, py, u0, v0, coeffs);
559 break;
560 }
561 default: {
562 throw(vpException(vpException::fatalError, "Case not handled in from_json(nlohmann::json &, const vpCameraParameters &)"));
563 }
564 }
565}
566#endif
567END_VISP_NAMESPACE
568#endif
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ ProjWithKannalaBrandtDistortion
Projection with Kannala-Brandt distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
friend void to_json(nlohmann::json &j, const vpCameraParameters &cam)
Converts camera parameters into a JSON representation.
friend class vpMeterPixelConversion
double getHorizontalFovAngle() const
friend class vpPixelMeterConversion
std::vector< double > getKannalaBrandtDistortionCoefficients() const
std::vector< vpColVector > getFovNormals() const
double getVerticalFovAngle() const
double get_px_inverse() const
double get_py_inverse() const
vpCameraParametersProjType get_projModel() const
friend void from_json(const nlohmann::json &j, vpCameraParameters &cam)
Deserialize a JSON object into camera parameters. The minimal required properties are:
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ fatalError
Fatal error.
Definition vpException.h:72
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175