-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvp_video_cap.hpp
171 lines (143 loc) · 3.32 KB
/
nvp_video_cap.hpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef __NVP_VIDEO_CAP_H__
#define __NVP_VIDEO_CAP_H__
#define USE_STD // Use std lib rather than thread lib
#include <opencv2/opencv.hpp>
#ifdef USE_STD
#include <thread>
#include <mutex>
#else
#include <boost/thread.hpp>
#endif
class NvpVideoCap{
private:
int mId;
cv::VideoCapture *mCap;
bool mOpened;
cv::Mat mFrames[2];
int mCurFrame;
#ifdef USE_STD
std::mutex mtx;
std::thread *mThread;
#else
boost::mutex mtx;
boost::thread *mThread;
#endif
int mWidth;
int mHeight;
void VideoCapThread()
{
bool ret;
cv::Mat tmp;
//fprintf (stderr, "VideoCapThread(): in...\n");
while (mOpened) {
if (mCap) {
ret = mCap->read(tmp);
if (!ret) {
fprintf (stderr, "Read frame error!\n");
Close();
break;
}
if (mtx.try_lock()) {
mCurFrame ^= 1;
tmp.copyTo(mFrames[mCurFrame]);
mtx.unlock();
}
}
}
}
// Disable copy constructor
NvpVideoCap(NvpVideoCap &cap);
// Disable assignment
NvpVideoCap& operator=(const NvpVideoCap &cap);
public:
NvpVideoCap() : mCap(NULL), mOpened(false)
{
mId = 0;
mCurFrame = 0;
mWidth = 0;
mHeight = 0;
}
~NvpVideoCap()
{
Close();
}
bool isOpened()
{
return mOpened;
}
int GetOneFrame(cv::Mat& img)
{
if (!isOpened()) {
return -1;
}
mtx.lock();
mFrames[mCurFrame].copyTo(img);
mtx.unlock();
return 0;
}
int Width()
{
return mWidth;
}
int Height()
{
return mHeight;
}
void SetFrameWidth(int width)
{
mCap->set(CV_CAP_PROP_FRAME_WIDTH, width);
mWidth = width;
}
void SetFrameHeight(int height)
{
mCap->set(CV_CAP_PROP_FRAME_HEIGHT, height);
mHeight = height;
}
void SetFrameRate(int rate)
{
mCap->set(CV_CAP_PROP_FPS, rate);
}
void Close()
{
mOpened = false;
if (mThread) {
mThread->join();
}
mThread = NULL;
if (mCap) {
mCap->release();
}
mCap = NULL;
}
int Open(int id = 0, int width = 0, int height = 0)
{
mId = id;
fprintf (stderr, "mId is %d, width = %d, height = %d.\n",
mId, width, height
);
mCap = new cv::VideoCapture(mId);
if (!mCap->isOpened()) {
Close();
return -1;
}
if (width != 0 && height != 0) {
SetFrameWidth(width);
SetFrameHeight(height);
}
mCap->read(mFrames[0]);
mCap->read(mFrames[1]);
mOpened = true;
#ifdef USE_STD
mThread = new std::thread(&NvpVideoCap::VideoCapThread, this);
#else
mThread = new boost::thread(&NvpVideoCap::VideoCapThread, this);
#endif
if (mThread == NULL) {
mOpened = false;
Close();
return -1;
}
return 0;
}
};
#endif // #ifndef __NVP_VIDEO_CAP_H__