SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSCvCamera.h
Go to the documentation of this file.
1 #ifndef SENS_CV_CAMERA_H
2 #define SENS_CV_CAMERA_H
3 
4 #include <opencv2/core.hpp>
5 #include <SENSFrame.h>
6 #include <SENSException.h>
7 #include <SENSCalibration.h>
8 #include <atomic>
9 #include <map>
10 #include <thread>
11 #include <algorithm>
12 #include <SENSUtils.h>
13 #include <HighResTimer.h>
14 #include "SENSCamera.h"
15 
16 //---------------------------------------------------------------------------
17 //SENSCameraConfig
18 //define a config to start a capture session on a camera device
20 {
21  //this constructor forces the user to always define a complete parameter set. In this way no parameter is forgotten..
24  int targetWidth,
25  int targetHeight,
26  int manipWidth,
27  int manipHeight,
28  bool mirrorH,
29  bool mirrorV,
30  bool convertManipToGray)
40  {
41  }
42 
45  //! largest target image width (only BGR)
46  const int targetWidth;
47  //! largest target image width (only BGR)
48  const int targetHeight;
49  //! width of smaller image version (e.g. for tracking)
50  const int manipWidth;
51  //! height of smaller image version (e.g. for tracking)
52  const int manipHeight;
53  //! mirror image horizontally after capturing
54  const bool mirrorH;
55  //! mirror image vertically after capturing
56  const bool mirrorV;
57  //! provide gray version of small image
58  const bool convertManipToGray;
59 };
60 
61 /*!
62  Camera wrapper for computer vision applications that adds convenience functions and that makes post processing steps.
63  */
65 {
66 public:
67  //!error code definition, will be returned by configure in case of failure. See method advice() for explanation.
69  {
70  SUCCESS = 0,
76  };
77 
78  //! returns advice to an error code
79  static std::string advice(const ConfigReturnCode returnCode)
80  {
81  switch (returnCode)
82  {
83  case SUCCESS: return "Camera was configured successfully";
84  case WARN_TARGET_SIZE_NOT_FOUND: return "Only a smaller resolution was found (you can still start the camera)";
85  case ERROR_CAMERA_INVALID: return "Camera is null";
86  case ERROR_CAMERA_IS_RUNNING: return "Camera is running, you have to stop it first";
87  case ERROR_FACING_NOT_AVAILABLE: return "Camera facing is not available as configured";
88  case ERROR_UNKNOWN: return "Unknown error";
89  default: return "Unknown return code";
90  }
91  }
92 
93  SENSCvCamera(SENSCamera* camera);
94 
95  /*!
96  configure camera: the function checks with the wrapped camera if it could successfully configure the camera.
97  */
99  int targetWidth,
100  int targetHeight,
101  int manipWidth,
102  int manipHeight,
103  bool mirrorH,
104  bool mirrorV,
105  bool convertManipToGray);
106 
107  bool started();
108  bool start();
109  void stop();
110 
112 
113  bool supportsFacing(SENSCameraFacing facing);
114  //get camera pointer reference
115  SENSCamera*& cameraRef() { return _camera; }
116  bool isConfigured() { return _config != nullptr; }
117  const SENSCvCameraConfig* config() { return _config.get(); }
118 
119  //! returns SENSCalibration if it was started (maybe a guessed one from a fovV guess). Else returns nullptr. The calibration is used for computer vision applications. So, if a manipulated image is requested (see imgManipSize in SENSCamera::start(...), SENSFrame::imgManip and SENSCameraConfig) this calibration is adjusted to fit to this image, else to the original sized image (see SENSFrame::imgBGR)
120  const SENSCalibration* const calibration() const { return _calibration.get(); }
121  const SENSCalibration* const calibrationManip() const { return _calibrationManip.get(); }
122 
123  //guess a calibration from what we know and update all derived calibration
124  //(fallback is used if camera api defines no fovV value)
125  void guessAndSetCalibration(float fovDegFallbackGuess);
126 
127  cv::Mat scaledCameraMat();
128 
129  //! Set calibration and adapt it to current image size. Camera has to be configured, before this function is called.
130  void setCalibration(const SENSCalibration& calibration, bool buildUndistortionMaps);
131  //! clear calibration set from outsilde with setCalibration. Only then an automatic guess will be possible.
133 
134 private:
135  SENSFramePtr processNewFrame(const SENSTimePt& timePt, cv::Mat bgrImg, cv::Mat intrinsics, bool intrinsicsChanged);
136 
138  std::unique_ptr<SENSCvCameraConfig> _config;
139 
140  //! The calibration is used for computer vision applications. This calibration is adjusted to fit to the original sized image (see SENSFrame::imgBGR and SENSCameraConfig::targetWidth, targetHeight)
141  std::unique_ptr<SENSCalibration> _calibration;
142  //calibration that fits to (targetWidth,targetHeight)
143  std::unique_ptr<SENSCalibration> _calibrationManip;
144 
145  //this is a calibration that is set from outside. if it is valid, no guesses will be made
146  std::unique_ptr<SENSCalibration> _calibrationOverwrite;
147 };
148 
149 #endif //SENS_CV_CAMERA_H
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
SENSCameraFacing
Definition of camera facing.
Definition: SENSCamera.h:28
std::shared_ptr< SENSFrame > SENSFramePtr
Definition: SENSFrame.h:73
Pure abstract camera class.
Definition: SENSCamera.h:160
cv::Mat scaledCameraMat()
void clearCalibration()
clear calibration set from outsilde with setCalibration. Only then an automatic guess will be possibl...
Definition: SENSCvCamera.h:132
bool supportsFacing(SENSCameraFacing facing)
const SENSCvCameraConfig * config()
Definition: SENSCvCamera.h:117
ConfigReturnCode
error code definition, will be returned by configure in case of failure. See method advice() for expl...
Definition: SENSCvCamera.h:69
@ ERROR_FACING_NOT_AVAILABLE
Definition: SENSCvCamera.h:74
@ ERROR_CAMERA_IS_RUNNING
Definition: SENSCvCamera.h:73
@ ERROR_CAMERA_INVALID
Definition: SENSCvCamera.h:72
@ WARN_TARGET_SIZE_NOT_FOUND
Definition: SENSCvCamera.h:71
bool isConfigured()
Definition: SENSCvCamera.h:116
void guessAndSetCalibration(float fovDegFallbackGuess)
std::unique_ptr< SENSCalibration > _calibrationManip
Definition: SENSCvCamera.h:143
std::unique_ptr< SENSCvCameraConfig > _config
Definition: SENSCvCamera.h:138
void setCalibration(const SENSCalibration &calibration, bool buildUndistortionMaps)
Set calibration and adapt it to current image size. Camera has to be configured, before this function...
SENSCvCamera(SENSCamera *camera)
ConfigReturnCode configure(SENSCameraFacing facing, int targetWidth, int targetHeight, int manipWidth, int manipHeight, bool mirrorH, bool mirrorV, bool convertManipToGray)
SENSCamera * _camera
Definition: SENSCvCamera.h:137
const SENSCalibration *const calibration() const
returns SENSCalibration if it was started (maybe a guessed one from a fovV guess)....
Definition: SENSCvCamera.h:120
SENSFramePtr latestFrame()
SENSCamera *& cameraRef()
Definition: SENSCvCamera.h:115
static std::string advice(const ConfigReturnCode returnCode)
returns advice to an error code
Definition: SENSCvCamera.h:79
std::unique_ptr< SENSCalibration > _calibration
The calibration is used for computer vision applications. This calibration is adjusted to fit to the ...
Definition: SENSCvCamera.h:141
const SENSCalibration *const calibrationManip() const
Definition: SENSCvCamera.h:121
SENSFramePtr processNewFrame(const SENSTimePt &timePt, cv::Mat bgrImg, cv::Mat intrinsics, bool intrinsicsChanged)
Definition: SENSCvCamera.cpp:7
std::unique_ptr< SENSCalibration > _calibrationOverwrite
Definition: SENSCvCamera.h:146
const SENSCameraStreamConfig * streamConfig
Definition: SENSCvCamera.h:44
const int manipWidth
width of smaller image version (e.g. for tracking)
Definition: SENSCvCamera.h:50
const int manipHeight
height of smaller image version (e.g. for tracking)
Definition: SENSCvCamera.h:52
const int targetWidth
largest target image width (only BGR)
Definition: SENSCvCamera.h:46
const int targetHeight
largest target image width (only BGR)
Definition: SENSCvCamera.h:48
const bool convertManipToGray
provide gray version of small image
Definition: SENSCvCamera.h:58
SENSCvCameraConfig(const SENSCameraDeviceProps *deviceProps, const SENSCameraStreamConfig *streamConfig, int targetWidth, int targetHeight, int manipWidth, int manipHeight, bool mirrorH, bool mirrorV, bool convertManipToGray)
Definition: SENSCvCamera.h:22
const bool mirrorV
mirror image vertically after capturing
Definition: SENSCvCamera.h:56
const SENSCameraDeviceProps * deviceProps
Definition: SENSCvCamera.h:43
const bool mirrorH
mirror image horizontally after capturing
Definition: SENSCvCamera.h:54