-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVideoCapture.cpp
109 lines (107 loc) · 2.65 KB
/
VideoCapture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#if !defined(_MSC_VER)
#include <unistd.h>
#define _sleep(ms) usleep((ms) * 1000)
#endif
#include "VideoCapture.h"
using namespace std;
namespace cvImagePipeline {
namespace Filter {
///////////////////////////////////////////////////////////////////////////
// class VideoCapture
IMPLEMENT_CVFILTER(VideoCapture);
VideoCapture::VideoCapture()
:
deviceIndex("deviceIndex", -1), filename("filename", ""),
startFrame("startFrame", 0), stopFrame("stopFrame", -1),
width("width", 0.0),
height("height", 0.0),
captureStart("captureStart", false),
captureEmpty("captureEmpty", false),
frameNumber(0)
{
defParam(deviceIndex);
defParam(filename);
defParam(startFrame);
defParam(stopFrame);
defParam(width);
defParam(height);
defParam(captureStart);
defParam(captureEmpty);
setInputMatDesc("", "");
}
VideoCapture::~VideoCapture() { }
bool VideoCapture::open(int deviceIndex)
{
bool open_state = videoCapture.open(deviceIndex);
if(width != 0.0) {
videoCapture.set(CV_CAP_PROP_FRAME_WIDTH, width);
}
if(height != 0.0) {
videoCapture.set(CV_CAP_PROP_FRAME_HEIGHT, height);
}
captureStart = false;
frameNumber = 0;
return open_state;
}
bool VideoCapture::open(const std::string& filename)
{
bool open_state = videoCapture.open(filename);
if(width != 0.0) {
videoCapture.set(CV_CAP_PROP_FRAME_WIDTH, width);
}
if(height != 0.0) {
videoCapture.set(CV_CAP_PROP_FRAME_HEIGHT, height);
}
captureStart = false;
frameNumber = 0;
return open_state;
}
bool VideoCapture::capture() {
if(!videoCapture.isOpened()) {
if(deviceIndex >= 0) {
if(!open(deviceIndex)) {
return false;
}
} else if((string)filename != "") {
if(!open((string)filename)) {
return false;
}
} else {
return false;
}
}
cv::Mat& mat = refOutputMat();
if (captureStart == false) {
do {
videoCapture >> mat;
frameNumber++;
_sleep(10);
} while (mat.empty());
captureStart = true;
captureEmpty = false;
}
else if (!captureEmpty) {
while (frameNumber < startFrame) {
videoCapture >> mat;
frameNumber++;
if (mat.empty() || (stopFrame >= 0 && frameNumber >= stopFrame)) {
captureEmpty = true;
return true;
}
}
if (stopFrame < 0 || frameNumber < stopFrame) {
videoCapture >> mat;
frameNumber++;
if (mat.empty() || (stopFrame >= 0 && frameNumber >= stopFrame)) {
captureEmpty = true;
return true;
}
}
}
return true;
}
void VideoCapture::execute() {
capture();
}
}
}