-
Notifications
You must be signed in to change notification settings - Fork 7
/
ProcessingThread.cpp
310 lines (290 loc) · 11.5 KB
/
ProcessingThread.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/************************************************************************/
/* qt-opencv-multithreaded: */
/* A multithreaded OpenCV application using the Qt framework. */
/* */
/* ProcessingThread.cpp */
/* */
/* Nick D'Ademo <nickdademo@gmail.com> */
/* */
/* Copyright (c) 2012 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 "ImageBuffer.h"
#include "ProcessingThread.h"
#include "MatToQImage.h"
// Qt header files
#include <QDebug>
// OpenCV header files
#include <opencv/cv.h>
#include <opencv/highgui.h>
// Configuration header file
#include "Config.h"
ProcessingThread::ProcessingThread(ImageBuffer *imageBuffer, int inputSourceWidth, int inputSourceHeight) : QThread(),
imageBuffer(imageBuffer),
inputSourceWidth(inputSourceWidth),
inputSourceHeight(inputSourceHeight)
{
// Initialize variables
stopped=false;
sampleNo=0;
fpsSum=0;
avgFPS=0;
fps.clear();
currentROI=Rect(0,0,inputSourceWidth,inputSourceHeight);
// Store original ROI
originalROI=currentROI;
} // ProcessingThread constructor
ProcessingThread::~ProcessingThread()
{
} // ProcessingThread destructor
void ProcessingThread::run()
{
while(1)
{
/////////////////////////////////
// Stop thread if stopped=TRUE //
/////////////////////////////////
stoppedMutex.lock();
if (stopped)
{
stopped=false;
stoppedMutex.unlock();
break;
}
stoppedMutex.unlock();
/////////////////////////////////
/////////////////////////////////
// Save processing time
processingTime=t.elapsed();
// Start timer (used to calculate processing rate)
t.start();
// Get frame from queue, store in currentFrame, set ROI
currentFrame=Mat(imageBuffer->getFrame(),currentROI);
updateMembersMutex.lock();
///////////////////
// PERFORM TASKS //
///////////////////
// Note: ROI changes will take effect on next frame
if(resetROIFlag)
resetROI();
else if(setROIFlag)
setROI();
////////////////////////////////////
// PERFORM IMAGE PROCESSING BELOW //
////////////////////////////////////
// Grayscale conversion
if(grayscaleOn)
cvtColor(currentFrame,currentFrameGrayscale,CV_BGR2GRAY);
// Smooth (in-place operations)
if(smoothOn)
{
if(grayscaleOn)
{
switch(smoothType)
{
// BLUR
case 0:
blur(currentFrameGrayscale,currentFrameGrayscale,Size(smoothParam1,smoothParam2));
break;
// GAUSSIAN
case 1:
GaussianBlur(currentFrameGrayscale,currentFrameGrayscale,Size(smoothParam1,smoothParam2),smoothParam3,smoothParam4);
break;
// MEDIAN
case 2:
medianBlur(currentFrameGrayscale,currentFrameGrayscale,smoothParam1);
break;
}
}
else
{
switch(smoothType)
{
// BLUR
case 0:
blur(currentFrame,currentFrame,Size(smoothParam1,smoothParam2));
break;
// GAUSSIAN
case 1:
GaussianBlur(currentFrame,currentFrame,Size(smoothParam1,smoothParam2),smoothParam3,smoothParam4);
break;
// MEDIAN
case 2:
medianBlur(currentFrame,currentFrame,smoothParam1);
break;
}
}
}
// Dilate
if(dilateOn)
{
if(grayscaleOn)
dilate(currentFrameGrayscale,currentFrameGrayscale,Mat(),Point(-1,-1),dilateNumberOfIterations);
else
dilate(currentFrame,currentFrame,Mat(),Point(-1,-1),dilateNumberOfIterations);
}
// Erode
if(erodeOn)
{
if(grayscaleOn)
erode(currentFrameGrayscale,currentFrameGrayscale,Mat(),Point(-1,-1),erodeNumberOfIterations);
else
erode(currentFrame,currentFrame,Mat(),Point(-1,-1),erodeNumberOfIterations);
}
// Flip
if(flipOn)
{
if(grayscaleOn)
flip(currentFrameGrayscale,currentFrameGrayscale,flipCode);
else
flip(currentFrame,currentFrame,flipCode);
}
// Canny edge detection
if(cannyOn)
{
if(!grayscaleOn)
Canny(currentFrame,currentFrame,
cannyThreshold1,cannyThreshold2,
cannyApertureSize,cannyL2gradient);
else
Canny(currentFrameGrayscale,currentFrameGrayscale,
cannyThreshold1,cannyThreshold2,
cannyApertureSize,cannyL2gradient);
}
////////////////////////////////////
// PERFORM IMAGE PROCESSING ABOVE //
////////////////////////////////////
// Convert Mat to QImage: Show grayscale frame [if Grayscale mode is ON]
if(grayscaleOn)
frame=MatToQImage(currentFrameGrayscale);
// Convert Mat to QImage: Show BGR frame
else
frame=MatToQImage(currentFrame);
updateMembersMutex.unlock();
// Update statistics
updateFPS(processingTime);
currentSizeOfBuffer=imageBuffer->getSizeOfImageBuffer();
// Inform GUI thread of new frame (QImage)
emit newFrame(frame);
}
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
sampleNo++;
}
// 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)&&(sampleNo==PROCESSING_FPS_STAT_QUEUE_LENGTH))
{
// Empty queue and store sum
while(!fps.empty())
fpsSum+=fps.dequeue();
// Calculate average FPS
avgFPS=fpsSum/PROCESSING_FPS_STAT_QUEUE_LENGTH;
// Reset sum
fpsSum=0;
// Reset sample number
sampleNo=0;
}
} // updateFPS()
void ProcessingThread::stopProcessingThread()
{
stoppedMutex.lock();
stopped=true;
stoppedMutex.unlock();
} // stopProcessingThread()
void ProcessingThread::setROI()
{
// Save selection as new (current) ROI
currentROI=selectionBox;
qDebug() << "ROI successfully SET.";
// Reset flag to FALSE
setROIFlag=false;
} // setROI()
void ProcessingThread::resetROI()
{
// Reset ROI to original if not already
if((currentROI.x!=originalROI.x)&&(currentROI.y!=originalROI.y)&&(currentROI.width!=originalROI.width)&&(currentROI.height!=originalROI.height))
{
currentROI=originalROI;
qDebug() << "ROI successfully RESET.";
}
// Reset flag to FALSE
resetROIFlag=false;
} // resetROI()
void ProcessingThread::updateImageProcessingFlags(struct ImageProcessingFlags imageProcessingFlags)
{
QMutexLocker locker(&updateMembersMutex);
this->grayscaleOn=imageProcessingFlags.grayscaleOn;
this->smoothOn=imageProcessingFlags.smoothOn;
this->dilateOn=imageProcessingFlags.dilateOn;
this->erodeOn=imageProcessingFlags.erodeOn;
this->flipOn=imageProcessingFlags.flipOn;
this->cannyOn=imageProcessingFlags.cannyOn;
} // updateImageProcessingFlags()
void ProcessingThread::updateImageProcessingSettings(struct ImageProcessingSettings imageProcessingSettings)
{
QMutexLocker locker(&updateMembersMutex);
this->smoothType=imageProcessingSettings.smoothType;
this->smoothParam1=imageProcessingSettings.smoothParam1;
this->smoothParam2=imageProcessingSettings.smoothParam2;
this->smoothParam3=imageProcessingSettings.smoothParam3;
this->smoothParam4=imageProcessingSettings.smoothParam4;
this->dilateNumberOfIterations=imageProcessingSettings.dilateNumberOfIterations;
this->erodeNumberOfIterations=imageProcessingSettings.erodeNumberOfIterations;
this->flipCode=imageProcessingSettings.flipCode;
this->cannyThreshold1=imageProcessingSettings.cannyThreshold1;
this->cannyThreshold2=imageProcessingSettings.cannyThreshold2;
this->cannyApertureSize=imageProcessingSettings.cannyApertureSize;
this->cannyL2gradient=imageProcessingSettings.cannyL2gradient;
} // updateImageProcessingSettings()
void ProcessingThread::updateTaskData(struct TaskData taskData)
{
QMutexLocker locker(&updateMembersMutex);
this->setROIFlag=taskData.setROIFlag;
this->resetROIFlag=taskData.resetROIFlag;
this->selectionBox.x=taskData.selectionBox.left();
this->selectionBox.y=taskData.selectionBox.top();
this->selectionBox.width=taskData.selectionBox.width();
this->selectionBox.height=taskData.selectionBox.height();
} // updateTaskData()
int ProcessingThread::getAvgFPS()
{
return avgFPS;
} // getAvgFPS()
int ProcessingThread::getCurrentSizeOfBuffer()
{
return currentSizeOfBuffer;
} // getCurrentSizeOfBuffer()
Rect ProcessingThread::getCurrentROI()
{
return currentROI;
} // getCurrentROI();