-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessingThread.cpp
More file actions
272 lines (248 loc) · 10.4 KB
/
Copy pathProcessingThread.cpp
File metadata and controls
272 lines (248 loc) · 10.4 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/************************************************************************/
/* qt-opencv-multithreaded: */
/* A multithreaded OpenCV application using the Qt framework. */
/* */
/* ProcessingThread.cpp */
/* */
/* Nick D'Ademo <nickdademo@gmail.com> */
/* */
/* Copyright (c) 2012-2013 Nick D'Ademo */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#include "ProcessingThread.h"
#include <QDebug>
#include <QDir>
#include <QProcess>
ProcessingThread::ProcessingThread(SharedImageBuffer *sharedImageBuffer, int deviceNumber) : QThread(), sharedImageBuffer(sharedImageBuffer)
{
// Save Device Number
this->deviceNumber=deviceNumber;
// Initialize members
doStop=false;
sampleNumber=0;
fpsSum=0;
fps.clear();
statsData.averageFPS=0;
statsData.nFramesProcessed=0;
enableCapture = false;
receipt = "";
}
void ProcessingThread::run()
{
while(1)
{
////////////////////////////////
// Stop thread if doStop=TRUE //
////////////////////////////////
doStopMutex.lock();
if(doStop)
{
doStop=false;
doStopMutex.unlock();
break;
}
doStopMutex.unlock();
/////////////////////////////////
/////////////////////////////////
// Save processing time
processingTime=t.elapsed();
// Start timer (used to calculate processing rate)
t.start();
processingMutex.lock();
// Get frame from queue, store in currentFrame, set ROI
currentFrame=Mat(sharedImageBuffer->getByDeviceNumber(deviceNumber)->get().clone(), currentROI);
// Example of how to grab a frame from another stream (where Device Number=1)
// Note: This requires stream synchronization to be ENABLED (in the Options menu of MainWindow) and frame processing for the stream you are grabbing FROM to be DISABLED.
/*
if(sharedImageBuffer->containsImageBufferForDeviceNumber(1))
{
// Grab frame from another stream (connected to camera with Device Number=1)
Mat frameFromAnotherStream = Mat(sharedImageBuffer->getByDeviceNumber(1)->getFrame(), currentROI);
// Linear blend images together using OpenCV and save the result to currentFrame. Note: beta=1-alpha
addWeighted(frameFromAnotherStream, 0.5, currentFrame, 0.5, 0.0, currentFrame);
}
*/
////////////////////////////////////
// PERFORM IMAGE PROCESSING BELOW //
////////////////////////////////////
// Grayscale conversion (in-place operation)
if(imgProcFlags.grayscaleOn && (currentFrame.channels() == 3 || currentFrame.channels() == 4))
cvtColor(currentFrame, currentFrame, CV_BGR2GRAY);
// Smooth (in-place operations)
if(imgProcFlags.smoothOn)
{
switch(imgProcSettings.smoothType)
{
// BLUR
case 0:
blur(currentFrame, currentFrame,
Size(imgProcSettings.smoothParam1, imgProcSettings.smoothParam2));
break;
// GAUSSIAN
case 1:
GaussianBlur(currentFrame, currentFrame,
Size(imgProcSettings.smoothParam1, imgProcSettings.smoothParam2),
imgProcSettings.smoothParam3, imgProcSettings.smoothParam4);
break;
// MEDIAN
case 2:
medianBlur(currentFrame, currentFrame,
imgProcSettings.smoothParam1);
break;
}
}
// Dilate
if(imgProcFlags.dilateOn)
{
dilate(currentFrame, currentFrame,
Mat(), Point(-1, -1), imgProcSettings.dilateNumberOfIterations);
}
// Erode
if(imgProcFlags.erodeOn)
{
erode(currentFrame, currentFrame,
Mat(), Point(-1, -1), imgProcSettings.erodeNumberOfIterations);
}
// Flip
if(imgProcFlags.flipOn)
{
flip(currentFrame, currentFrame,
imgProcSettings.flipCode);
}
// Canny edge detection
if(imgProcFlags.cannyOn)
{
Canny(currentFrame, currentFrame,
imgProcSettings.cannyThreshold1, imgProcSettings.cannyThreshold2,
imgProcSettings.cannyApertureSize, imgProcSettings.cannyL2gradient);
}
////////////////////////////////////
// PERFORM IMAGE PROCESSING ABOVE //
////////////////////////////////////
// Convert Mat to QImage
frame=MatToQImage(currentFrame);
// quqinglei add here
if (enableCapture) {
QString fileName = QString("%1/eye_images/%2.png").arg(captureLocalSavePath).arg(receipt);
frame.save(fileName);
enableCapture = false;
receipt = "";
}
processingMutex.unlock();
// Inform GUI thread of new frame (QImage)
emit newFrame(frame);
// Update statistics
updateFPS(processingTime);
statsData.nFramesProcessed++;
// Inform GUI of updated statistics
emit updateStatisticsInGUI(statsData);
}
qDebug() << "Stopping processing thread...";
}
void ProcessingThread::updateFPS(int timeElapsed)
{
// Add instantaneous FPS value to queue
if(timeElapsed>0)
{
fps.enqueue((int)1000/timeElapsed);
// Increment sample number
sampleNumber++;
}
// Maximum size of queue is DEFAULT_PROCESSING_FPS_STAT_QUEUE_LENGTH
if(fps.size()>PROCESSING_FPS_STAT_QUEUE_LENGTH)
fps.dequeue();
// Update FPS value every DEFAULT_PROCESSING_FPS_STAT_QUEUE_LENGTH samples
if((fps.size()==PROCESSING_FPS_STAT_QUEUE_LENGTH)&&(sampleNumber==PROCESSING_FPS_STAT_QUEUE_LENGTH))
{
// Empty queue and store sum
while(!fps.empty())
fpsSum+=fps.dequeue();
// Calculate average FPS
statsData.averageFPS=fpsSum/PROCESSING_FPS_STAT_QUEUE_LENGTH;
// Reset sum
fpsSum=0;
// Reset sample number
sampleNumber=0;
}
}
void ProcessingThread::stop()
{
QMutexLocker locker(&doStopMutex);
doStop=true;
}
void ProcessingThread::updateImageProcessingFlags(struct ImageProcessingFlags imgProcFlags)
{
QMutexLocker locker(&processingMutex);
this->imgProcFlags.grayscaleOn=imgProcFlags.grayscaleOn;
this->imgProcFlags.smoothOn=imgProcFlags.smoothOn;
this->imgProcFlags.dilateOn=imgProcFlags.dilateOn;
this->imgProcFlags.erodeOn=imgProcFlags.erodeOn;
this->imgProcFlags.flipOn=imgProcFlags.flipOn;
this->imgProcFlags.cannyOn=imgProcFlags.cannyOn;
}
void ProcessingThread::updateImageProcessingSettings(struct ImageProcessingSettings imgProcSettings)
{
QMutexLocker locker(&processingMutex);
this->imgProcSettings.smoothType=imgProcSettings.smoothType;
this->imgProcSettings.smoothParam1=imgProcSettings.smoothParam1;
this->imgProcSettings.smoothParam2=imgProcSettings.smoothParam2;
this->imgProcSettings.smoothParam3=imgProcSettings.smoothParam3;
this->imgProcSettings.smoothParam4=imgProcSettings.smoothParam4;
this->imgProcSettings.dilateNumberOfIterations=imgProcSettings.dilateNumberOfIterations;
this->imgProcSettings.erodeNumberOfIterations=imgProcSettings.erodeNumberOfIterations;
this->imgProcSettings.flipCode=imgProcSettings.flipCode;
this->imgProcSettings.cannyThreshold1=imgProcSettings.cannyThreshold1;
this->imgProcSettings.cannyThreshold2=imgProcSettings.cannyThreshold2;
this->imgProcSettings.cannyApertureSize=imgProcSettings.cannyApertureSize;
this->imgProcSettings.cannyL2gradient=imgProcSettings.cannyL2gradient;
}
void ProcessingThread::setROI(QRect roi)
{
QMutexLocker locker(&processingMutex);
/*
currentROI.x = roi.x();
currentROI.y = roi.y();
currentROI.width = roi.width();
currentROI.height = roi.height();
*/
currentROI.x = 669;
currentROI.y = 180,
currentROI.width = 643;
currentROI.height = 900;
}
QRect ProcessingThread::getCurrentROI()
{
return QRect(currentROI.x, currentROI.y, currentROI.width, currentROI.height);
}
void ProcessingThread::captureScreen(QString receipt)
{
this->receipt = receipt;
this->enableCapture = true;
}
void ProcessingThread::setLocalSavePath(QString localSavePath)
{
captureLocalSavePath = localSavePath;
QDir dir(captureLocalSavePath);
dir.mkdir("eye_images");
qDebug() << "processingThread" << captureLocalSavePath;
}