SLProject
4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
|
Encapsulation of the OpenCV Capture Device and holder of the last frame. More...
#include <CVCapture.h>
Public Member Functions | |
CVSize2i | open (int deviceNum) |
Opens the capture device and returns the frame size. More... | |
CVSize2i | openFile () |
Opens the video file instead of a camera feed. More... | |
void | start (float viewportWdivH) |
starts the video capturing More... | |
bool | grabAndAdjustForSL (float viewportWdivH) |
void | loadIntoLastFrame (float vieportWdivH, int camWidth, int camHeight, CVPixelFormatGL srcPixelFormat, const uchar *data, bool isContinuous) |
void | adjustForSL (float viewportWdivH) |
Does all adjustments needed for the gVideoTexture. More... | |
bool | isOpened () |
void | release () |
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. More... | |
void | videoType (CVVideoType vt) |
Setter for video type also sets the active calibration. More... | |
CVVideoType | videoType () |
int | nextFrameIndex () |
Returns the next frame index number. More... | |
int | videoLength () |
AvgFloat & | captureTimesMS () |
get number of frames in video More... | |
void | loadCalibrations (const string &computerInfo, const string &configPath) |
void | setCameraSize (int sizeIndex, int sizeIndexMax, int width, int height) |
void | moveCapturePosition (int n) |
Moves the current frame position in a video file. More... | |
Static Public Member Functions | |
static CVCapture * | instance () |
Public static instance getter for singleton pattern. More... | |
Public Attributes | |
CVMat | lastFrame |
last frame grabbed in BGR More... | |
CVMat | lastFrameFull |
last frame grabbed in BGR and full resolution More... | |
CVMat | lastFrameGray |
last frame in grayscale More... | |
CVPixelFormatGL | format |
GL pixel format. More... | |
CVSize | captureSize |
size of captured frame More... | |
float | startCaptureTimeMS |
start time of capturing in ms More... | |
bool | hasSecondaryCamera |
flag if device has secondary camera More... | |
string | videoFilename |
video filename to load More... | |
bool | videoLoops |
flag if video should loop More... | |
float | fps |
int | frameCount |
CVVSize | camSizes |
All possible camera sizes. More... | |
int | activeCamSizeIndex |
Currently active camera size index. More... | |
CVCamera * | activeCamera |
Pointer to the active camera. More... | |
CVCamera | mainCam |
camera representation for main video camera More... | |
CVCamera | scndCam |
camera representation for secondary video camera More... | |
CVCamera | videoFileCam |
camera representation for simulation using a video file More... | |
Private Member Functions | |
CVCapture () | |
private onetime constructor More... | |
~CVCapture () | |
Private constructor. More... | |
Private Attributes | |
CVVideoCapture | _captureDevice |
OpenCV capture device. More... | |
CVVideoType | _videoType = VT_NONE |
Flag for using the live video image. More... | |
AvgFloat | _captureTimesMS |
Averaged time for video capturing in ms. More... | |
HighResTimer | _timer |
High resolution timer. More... | |
Static Private Attributes | |
static CVCapture * | _instance = nullptr |
global singleton object More... | |
Encapsulation of the OpenCV Capture Device and holder of the last frame.
It holds a static image for the last captured color frame and a grayscale version as well as a single static instance of the OpenCV capture device.
The live video image grabbing is not mandatory and can be replaced by the the top level application with its own video grabbing functionality. This is e.g. used in the iOS or Android examples. The CVCapture::lastFrame and CVCapture::lastFrameGray are on the other hand used in all applications as the buffer for the last captured image.
Alternatively CVCapture can open a video file by a given videoFilename. This feature can be used across all platforms. For more information on video and capture see:
https://docs.opencv.org/3.0-beta/modules/videoio/doc/reading_and_writing_video.html
Definition at line 62 of file CVCapture.h.
|
private |
private onetime constructor
Private constructor.
Definition at line 35 of file CVCapture.cpp.
|
private |
void CVCapture::adjustForSL | ( | float | viewportWdivH | ) |
Does all adjustments needed for the gVideoTexture.
CVCapture::adjustForSL processes the following adjustments for all input images no matter with what they where captured:
1) Crops the input image if it doesn't match the screens aspect ratio. The input image mostly does't fit the aspect of the output screen aspect. If the input image is too high we crop it on top and bottom, if it is too wide we crop it on the sides. If viewportWdivH is negative the viewport aspect will be adapted to the video aspect ratio. No cropping will be applied.
2) Some cameras toward a face mirror the image and some do not. If a input image should be mirrored or not is stored in CVCalibration::_isMirroredH (H for horizontal) and CVCalibration::_isMirroredV (V for vertical).
3) Many of the further processing steps are faster done on grayscale images. We therefore create a copy that is grayscale converted.
Definition at line 387 of file CVCapture.cpp.
|
inline |
void CVCapture::copyYUVPlanes | ( | float | scrWdivH, |
int | srcW, | ||
int | srcH, | ||
uchar * | y, | ||
int | yBytes, | ||
int | yColOffset, | ||
int | yRowOffset, | ||
uchar * | u, | ||
int | uBytes, | ||
int | uColOffset, | ||
int | uRowOffset, | ||
uchar * | v, | ||
int | vBytes, | ||
int | vColOffset, | ||
int | vRowOffset | ||
) |
Copies and converts the video image in YUV_420 format to RGB and Grayscale.
CVCapture::copyYUVPlanes copies and converts the video image in YUV_420 format to the RGB image in CVCapture::lastFrame and the Y channel the grayscale image in CVCapture::lastFrameGray.
In the YUV_420 format only the luminosity channel Y has the full resolution (one byte per pixel). The color channels U and V are subsampled and have only one byte per 4 pixel. See also https://en.wikipedia.org/wiki/Chroma_subsampling
In addition the routine crops and mirrors the image if needed. So the following processing steps should be done hopefully in a single loop:
1) Crops the input image if it doesn't match the screens aspect ratio. The input image mostly does't fit the aspect of the output screen aspect. If the input image is too high we crop it on top and bottom, if it is too wide we crop it on the sides.
2) Some cameras toward a face mirror the image and some do not. If a input image should be mirrored or not is stored in CVCalibration::_isMirroredH (H for horizontal) and CVCalibration::_isMirroredV (V for vertical).
3) The most expensive part of course is the color space conversion from the YUV to RGB conversion. According to Wikipedia the conversion is defined as:
scrWdivH | aspect ratio width / height |
srcW | Source image width in pixel |
srcH | Source image height in pixel |
y | Pointer to first byte of the top left pixel of the y-plane |
yBytes | Size in bytes of the y-plane (must be srcW x srcH) |
yColOffset | Offset in bytes to the next pixel in the y-plane |
yRowOffset | Offset in bytes to the next line in the y-plane |
u | Pointer to first byte of the top left pixel of the u-plane |
uBytes | Size in bytes of the u-plane |
uColOffset | Offset in bytes to the next pixel in the u-plane |
uRowOffset | Offset in bytes to the next line in the u-plane |
v | Pointer to first byte of the top left pixel of the v-plane |
vBytes | Size in bytes of the v-plane |
vColOffset | Offset in bytes to the next pixel in the v-plane |
vRowOffset | Offset in bytes to the next line in the v-plane |
Definition at line 695 of file CVCapture.cpp.
bool CVCapture::grabAndAdjustForSL | ( | float | viewportWdivH | ) |
Grabs a new frame from the OpenCV capture device or video file and calls CVCapture::adjustForSL. This function can also be called by Android or iOS app for grabbing a frame of a video file. Android and iOS use their own capture functionality. If viewportWdivH is negative the viewport aspect will be adapted to the video aspect ratio.
Definition at line 235 of file CVCapture.cpp.
|
inlinestatic |
bool CVCapture::isOpened | ( | ) |
Definition at line 206 of file CVCapture.cpp.
void CVCapture::loadCalibrations | ( | const string & | computerInfo, |
const string & | configPath | ||
) |
Definition at line 894 of file CVCapture.cpp.
void CVCapture::loadIntoLastFrame | ( | float | vieportWdivH, |
int | camWidth, | ||
int | camHeight, | ||
CVPixelFormatGL | srcPixelFormat, | ||
const uchar * | data, | ||
bool | isContinuous | ||
) |
This method is called by iOS and Android projects that capture their video cameras on their own. We only adjust the color space. See the app_demo_slproject/ios and app_demo_slproject/android projects for the usage.
Definition at line 301 of file CVCapture.cpp.
void CVCapture::moveCapturePosition | ( | int | n | ) |
Moves the current frame position in a video file.
Definition at line 984 of file CVCapture.cpp.
int CVCapture::nextFrameIndex | ( | ) |
CVSize2i CVCapture::open | ( | int | deviceNum | ) |
Opens the capture device and returns the frame size.
Definition at line 74 of file CVCapture.cpp.
CVSize2i CVCapture::openFile | ( | ) |
Opens the video file instead of a camera feed.
Definition at line 132 of file CVCapture.cpp.
void CVCapture::release | ( | ) |
Definition at line 215 of file CVCapture.cpp.
void CVCapture::setCameraSize | ( | int | sizeIndex, |
int | sizeIndexMax, | ||
int | width, | ||
int | height | ||
) |
Sets the with and height of a camera size at index sizeIndex. If sizeIndexMax changes the vector in CVCapture gets cleared and resized.
Definition at line 969 of file CVCapture.cpp.
void CVCapture::start | ( | float | viewportWdivH | ) |
starts the video capturing
Definition at line 174 of file CVCapture.cpp.
int CVCapture::videoLength | ( | ) |
Definition at line 1014 of file CVCapture.cpp.
|
inline |
Definition at line 106 of file CVCapture.h.
void CVCapture::videoType | ( | CVVideoType | vt | ) |
Setter for video type also sets the active calibration.
The CVCapture instance has up to three video camera calibrations, one for a main camera (CVCapture::mainCam), one for the selfie camera on mobile devices (CVCapture::scndCam) and one for video file simulation (CVCapture::videoFileCam). The member CVCapture::activeCamera references the active one.
Definition at line 866 of file CVCapture.cpp.
|
private |
OpenCV capture device.
Definition at line 147 of file CVCapture.h.
|
private |
Averaged time for video capturing in ms.
Definition at line 153 of file CVCapture.h.
|
staticprivate |
global singleton object
Definition at line 144 of file CVCapture.h.
|
private |
High resolution timer.
Definition at line 154 of file CVCapture.h.
|
private |
Flag for using the live video image.
Definition at line 152 of file CVCapture.h.
CVCamera* CVCapture::activeCamera |
Pointer to the active camera.
Definition at line 136 of file CVCapture.h.
int CVCapture::activeCamSizeIndex |
Currently active camera size index.
Definition at line 134 of file CVCapture.h.
CVVSize CVCapture::camSizes |
All possible camera sizes.
A requestedSizeIndex of -1 returns on Android the default size of 640x480. This is the default size index if the camera resolutions are unknown.
Definition at line 133 of file CVCapture.h.
CVSize CVCapture::captureSize |
size of captured frame
Definition at line 123 of file CVCapture.h.
CVPixelFormatGL CVCapture::format |
GL pixel format.
Definition at line 122 of file CVCapture.h.
float CVCapture::fps |
Definition at line 128 of file CVCapture.h.
int CVCapture::frameCount |
Definition at line 129 of file CVCapture.h.
bool CVCapture::hasSecondaryCamera |
flag if device has secondary camera
Definition at line 125 of file CVCapture.h.
CVMat CVCapture::lastFrame |
last frame grabbed in BGR
Definition at line 119 of file CVCapture.h.
CVMat CVCapture::lastFrameFull |
last frame grabbed in BGR and full resolution
Definition at line 120 of file CVCapture.h.
CVMat CVCapture::lastFrameGray |
last frame in grayscale
Definition at line 121 of file CVCapture.h.
CVCamera CVCapture::mainCam |
camera representation for main video camera
Definition at line 137 of file CVCapture.h.
CVCamera CVCapture::scndCam |
camera representation for secondary video camera
Definition at line 138 of file CVCapture.h.
float CVCapture::startCaptureTimeMS |
start time of capturing in ms
Definition at line 124 of file CVCapture.h.
CVCamera CVCapture::videoFileCam |
camera representation for simulation using a video file
Definition at line 139 of file CVCapture.h.
string CVCapture::videoFilename |
video filename to load
Definition at line 126 of file CVCapture.h.
bool CVCapture::videoLoops |
flag if video should loop
Definition at line 127 of file CVCapture.h.