-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoCaptureThread.cpp
More file actions
104 lines (86 loc) · 3.12 KB
/
VideoCaptureThread.cpp
File metadata and controls
104 lines (86 loc) · 3.12 KB
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
#include "VideoCaptureThread.h"
#define PROCESS_IN_CAPTURE_THREAD
VideoCaptureThread::VideoCaptureThread(QObject *parent)
:QThread(parent)
{
this->processor = new LaneDetection();
}
VideoCaptureThread::~VideoCaptureThread()
{
}
void VideoCaptureThread::setParameters(QMutex* lock, QWaitCondition* conImageReady, VideoCaptureWrapper *camera, QVideoFrame *videoFrame, unsigned char *cvImageBuf, int width, int height)
{
this->running = true;
this->conImageReady = conImageReady;
this->lock = lock;
this->camera = camera;
this->videoFrame = videoFrame;
this->cvImageBuf = cvImageBuf;
this->width = width;
this->height = height;
}
void VideoCaptureThread::run()
{
#if defined(QT_DEBUG)
QElapsedTimer timer;
float fps = 0.0f;
int millisElapsed = 0;
int millis;
timer.start();
#endif
// run into when 'videoFrame' is provided by parent.
cv::Mat screenImage;
if(videoFrame != NULL){
//Assuming desktop, RGB camera image and RGBA QVideoFrame
// 将 videoFrame 的二进制内容映射到cv::Mat型结构中,用来做一个cvtColor格式转换
videoFrame->map(QAbstractVideoBuffer::ReadOnly);
screenImage = cv::Mat(height,width,CV_8UC4,videoFrame->bits());
}
//while(!isInterruptionRequested() && running && videoFrame != NULL && camera != NULL){
while(!isInterruptionRequested() && running && camera != NULL){
lock->lock();
if(!camera->grabFrame())
break;
unsigned char* cameraFrame = camera->retrieveFrame();
#ifdef PROCESS_IN_CAPTURE_THREAD
Mat dstImage;
cv::Mat tempMat(height,width,CV_8UC3,cameraFrame);
dstImage = processor->process(tempMat);
// run into when 'videoFrame' is provided by parent.
//Get camera image into screen frame buffer
if(videoFrame != NULL){
cv::cvtColor(dstImage,screenImage,cv::COLOR_RGB2RGBA);
// emit only when 'videoFrame' is provided by parent.
emit imageReady();
}
#else
// run into when 'videoFrame' is provided by parent.
//Get camera image into screen frame buffer
if(videoFrame != NULL){
cv::Mat tempMat(height,width,CV_8UC3,cameraFrame);
cv::cvtColor(tempMat,screenImage,cv::COLOR_RGB2RGBA);
// emit only when 'videoFrame' is provided by parent.
emit imageReady();
}
//Export camera image
if(cvImageBuf){
//Assuming desktop, RGB camera image
memcpy(cvImageBuf,cameraFrame,height*width*3);
}
#endif
//QThread::msleep(33);
// 解锁图像处理线程
conImageReady->wakeAll();
lock->unlock();
#if defined(QT_DEBUG)
millis = (int)timer.restart();
millisElapsed += millis;
fps = CAM_FPS_RATE*fps + (1.0f - CAM_FPS_RATE)*(1000.0f/millis);
if(millisElapsed >= CAM_FPS_PRINT_PERIOD){
qDebug("Camera is running at %f FPS",fps);
millisElapsed = 0;
}
#endif
}
exec();
}