SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVCapture.h
Go to the documentation of this file.
1 /**
2  * \file CVCapture
3  * \brief OpenCV Capture Device
4  * \date Winter 2016
5  * \remarks Please use clangformat to format the code. See more code style on
6  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
7  * \authors Michael Goettlicher, Marcus Hudritsch
8  * \copyright http://opensource.org/licenses/GPL-3.0
9 */
10 
11 #ifndef CVCapture_H
12 #define CVCapture_H
13 
14 /*
15 The OpenCV library version 3.4 or above with extra module must be present.
16 If the application captures the live video stream with OpenCV you have
17 to define in addition the constant APP_USES_CVCAPTURE.
18 All classes that use OpenCV begin with CV.
19 See also the class docs for CVCapture, CVCalibration and CVTracked
20 for a good top down information.
21 */
22 
23 #include <SL.h>
24 #include <CVTypedefs.h>
25 #include <CVImage.h>
26 #include <Averaged.h>
27 #include <opencv2/opencv.hpp>
28 #include <CVCamera.h>
29 #include <HighResTimer.h>
30 
31 #ifdef SL_EMSCRIPTEN
32 # include <WebCamera.h>
33 #endif
34 
35 using Utils::AvgFloat;
36 
37 //-----------------------------------------------------------------------------
38 //! Video type if multiple exist on mobile devices
40 {
41  VT_NONE = 0, //!< No camera needed
42  VT_MAIN = 1, //!< Main camera on all on all all devices
43  VT_SCND = 2, //!< Selfie camera on mobile devices
44  VT_FILE = 3, //!< Loads a video from file with OpenCV
45 };
46 
47 //-----------------------------------------------------------------------------
48 //! Encapsulation of the OpenCV Capture Device and holder of the last frame.
49 /*! It holds a static image for the last captured color frame and a grayscale
50 version as well as a single static instance of the OpenCV capture device.
51 \n
52 The live video image grabbing is not mandatory and can be replaced by the the
53 top level application with its own video grabbing functionality. This is e.g.
54 used in the iOS or Android examples.
55 The CVCapture::lastFrame and CVCapture::lastFrameGray are on the other
56 hand used in all applications as the buffer for the last captured image.\n
57 Alternatively CVCapture can open a video file by a given videoFilename.
58 This feature can be used across all platforms.
59 For more information on video and capture see:\n
60 https://docs.opencv.org/3.0-beta/modules/videoio/doc/reading_and_writing_video.html
61 */
62 class CVCapture
63 {
64 public: //! Public static instance getter for singleton pattern
65  static CVCapture* instance()
66  {
67  if (!_instance)
68  {
69  _instance = new CVCapture();
70  return _instance;
71  }
72  else
73  return _instance;
74  }
75 
76  CVSize2i open(int deviceNum);
78  void start(float viewportWdivH);
79  bool grabAndAdjustForSL(float viewportWdivH);
80  void loadIntoLastFrame(float vieportWdivH,
81  int camWidth,
82  int camHeight,
83  CVPixelFormatGL srcPixelFormat,
84  const uchar* data,
85  bool isContinuous);
86  void adjustForSL(float viewportWdivH);
87  bool isOpened();
88  void release();
89  void copyYUVPlanes(float scrWdivH,
90  int srcW,
91  int srcH,
92  uchar* y,
93  int ySize,
94  int yPixStride,
95  int yLineStride,
96  uchar* u,
97  int uSize,
98  int uPixStride,
99  int uLineStride,
100  uchar* v,
101  int vSize,
102  int vPixStride,
103  int vLineStride);
104 
105  void videoType(CVVideoType vt);
107  int nextFrameIndex();
108  int videoLength(); //! get number of frames in video
110  void loadCalibrations(const string& computerInfo,
111  const string& configPath);
112  void setCameraSize(int sizeIndex,
113  int sizeIndexMax,
114  int width,
115  int height);
116 
117  void moveCapturePosition(int n);
118 
119  CVMat lastFrame; //!< last frame grabbed in BGR
120  CVMat lastFrameFull; //!< last frame grabbed in BGR and full resolution
121  CVMat lastFrameGray; //!< last frame in grayscale
122  CVPixelFormatGL format; //!< GL pixel format
123  CVSize captureSize; //!< size of captured frame
124  float startCaptureTimeMS; //!< start time of capturing in ms
125  bool hasSecondaryCamera; //!< flag if device has secondary camera
126  string videoFilename; //!< video filename to load
127  bool videoLoops; //!< flag if video should loop
128  float fps;
130 
131  /*! A requestedSizeIndex of -1 returns on Android the default size of 640x480.
132  This is the default size index if the camera resolutions are unknown.*/
133  CVVSize camSizes; //!< All possible camera sizes
134  int activeCamSizeIndex; //!< Currently active camera size index
135 
136  CVCamera* activeCamera; //!< Pointer to the active camera
137  CVCamera mainCam; //!< camera representation for main video camera
138  CVCamera scndCam; //!< camera representation for secondary video camera
139  CVCamera videoFileCam; //!< camera representation for simulation using a video file
140 
141 private:
142  CVCapture(); //!< private onetime constructor
143  ~CVCapture();
144  static CVCapture* _instance; //!< global singleton object
145 
146 #ifndef SL_EMSCRIPTEN
147  CVVideoCapture _captureDevice; //!< OpenCV capture device
148 #else
149  WebCamera _webCamera; //!< Browser capture stream
150 #endif
151 
152  CVVideoType _videoType = VT_NONE; //!< Flag for using the live video image
153  AvgFloat _captureTimesMS; //!< Averaged time for video capturing in ms
154  HighResTimer _timer; //!< High resolution timer
155 };
156 //-----------------------------------------------------------------------------
157 #endif // CVCapture_H
CVVideoType
Video type if multiple exist on mobile devices.
Definition: CVCapture.h:40
@ VT_SCND
Selfie camera on mobile devices.
Definition: CVCapture.h:43
@ VT_FILE
Loads a video from file with OpenCV.
Definition: CVCapture.h:44
@ VT_NONE
No camera needed.
Definition: CVCapture.h:41
@ VT_MAIN
Main camera on all on all all devices.
Definition: CVCapture.h:42
CVPixelFormatGL
Pixel format according to OpenGL pixel format defines.
Definition: CVImage.h:24
cv::VideoCapture CVVideoCapture
Definition: CVTypedefs.h:68
cv::Size2i CVSize2i
Definition: CVTypedefs.h:56
vector< cv::Size > CVVSize
Definition: CVTypedefs.h:87
cv::Size CVSize
Definition: CVTypedefs.h:55
cv::Mat CVMat
Definition: CVTypedefs.h:38
Interface to access the camera through the browser.
Encapsulation of the OpenCV Capture Device and holder of the last frame.
Definition: CVCapture.h:63
CVCamera scndCam
camera representation for secondary video camera
Definition: CVCapture.h:138
int frameCount
Definition: CVCapture.h:129
CVVideoCapture _captureDevice
OpenCV capture device.
Definition: CVCapture.h:147
void moveCapturePosition(int n)
Moves the current frame position in a video file.
Definition: CVCapture.cpp:984
bool hasSecondaryCamera
flag if device has secondary camera
Definition: CVCapture.h:125
CVVideoType videoType()
Definition: CVCapture.h:106
~CVCapture()
Private constructor.
Definition: CVCapture.cpp:59
void loadCalibrations(const string &computerInfo, const string &configPath)
Definition: CVCapture.cpp:894
float fps
Definition: CVCapture.h:128
CVVSize camSizes
All possible camera sizes.
Definition: CVCapture.h:133
void release()
Definition: CVCapture.cpp:215
CVCamera * activeCamera
Pointer to the active camera.
Definition: CVCapture.h:136
void copyYUVPlanes(float scrWdivH, int srcW, int srcH, uchar *y, int ySize, int yPixStride, int yLineStride, uchar *u, int uSize, int uPixStride, int uLineStride, uchar *v, int vSize, int vPixStride, int vLineStride)
Copies and converts the video image in YUV_420 format to RGB and Grayscale.
Definition: CVCapture.cpp:695
CVPixelFormatGL format
GL pixel format.
Definition: CVCapture.h:122
CVSize captureSize
size of captured frame
Definition: CVCapture.h:123
void setCameraSize(int sizeIndex, int sizeIndexMax, int width, int height)
Definition: CVCapture.cpp:969
void adjustForSL(float viewportWdivH)
Does all adjustments needed for the gVideoTexture.
Definition: CVCapture.cpp:387
int videoLength()
Definition: CVCapture.cpp:1014
CVSize2i open(int deviceNum)
Opens the capture device and returns the frame size.
Definition: CVCapture.cpp:74
void start(float viewportWdivH)
starts the video capturing
Definition: CVCapture.cpp:174
CVMat lastFrameFull
last frame grabbed in BGR and full resolution
Definition: CVCapture.h:120
CVCapture()
private onetime constructor
Definition: CVCapture.cpp:35
CVMat lastFrame
last frame grabbed in BGR
Definition: CVCapture.h:119
string videoFilename
video filename to load
Definition: CVCapture.h:126
CVVideoType _videoType
Flag for using the live video image.
Definition: CVCapture.h:152
CVMat lastFrameGray
last frame in grayscale
Definition: CVCapture.h:121
CVSize2i openFile()
Opens the video file instead of a camera feed.
Definition: CVCapture.cpp:132
int activeCamSizeIndex
Currently active camera size index.
Definition: CVCapture.h:134
CVCamera videoFileCam
camera representation for simulation using a video file
Definition: CVCapture.h:139
CVCamera mainCam
camera representation for main video camera
Definition: CVCapture.h:137
static CVCapture * instance()
Public static instance getter for singleton pattern.
Definition: CVCapture.h:65
int nextFrameIndex()
Returns the next frame index number.
Definition: CVCapture.cpp:1000
AvgFloat & captureTimesMS()
get number of frames in video
Definition: CVCapture.h:109
bool isOpened()
Definition: CVCapture.cpp:206
void loadIntoLastFrame(float vieportWdivH, int camWidth, int camHeight, CVPixelFormatGL srcPixelFormat, const uchar *data, bool isContinuous)
Definition: CVCapture.cpp:301
AvgFloat _captureTimesMS
Averaged time for video capturing in ms.
Definition: CVCapture.h:153
HighResTimer _timer
High resolution timer.
Definition: CVCapture.h:154
bool videoLoops
flag if video should loop
Definition: CVCapture.h:127
float startCaptureTimeMS
start time of capturing in ms
Definition: CVCapture.h:124
static CVCapture * _instance
global singleton object
Definition: CVCapture.h:144
bool grabAndAdjustForSL(float viewportWdivH)
Definition: CVCapture.cpp:235
High Resolution Timer class using C++11.
Definition: HighResTimer.h:31
Interface to access the camera in the browser.
Definition: WebCamera.h:26
Utils::Averaged< float > AvgFloat
Definition: Averaged.h:85