-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhaarcascade.cpp
461 lines (392 loc) · 16.5 KB
/
haarcascade.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* QtHaar, Viola-Jones implementation in Qt.
* Copyright (C) 2015 Gonzalo Exequiel Pedone
*
* QtHaar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QtHaar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QtHaar. If not, see <http://www.gnu.org/licenses/>.
*
* Email : hipersayan DOT x AT gmail DOT com
* Web-Site: http://github.com/hipersayanX/QtHaar
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
*
* By downloading, copying, installing or using the software you agree to this license.
* If you do not agree to this license, do not download, install,
* copy or use the software.
*
*
* Intel License Agreement
* For Open Source Computer Vision Library
*
* Copyright (C) 2000, Intel Corporation, all rights reserved.
* Third party copyrights are property of their respective owners.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistribution's of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistribution's in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * The name of Intel Corporation may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* This software is provided by the copyright holders and contributors "as is" and
* any express or implied warranties, including, but not limited to, the implied
* warranties of merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the Intel Corporation or contributors be liable for any direct,
* indirect, incidental, special, exemplary, or consequential damages
* (including, but not limited to, procurement of substitute goods or services;
* loss of use, data, or profits; or business interruption) however caused
* and on any theory of liability, whether in contract, strict liability,
* or tort (including negligence or otherwise) arising in any way out of
* the use of this software, even if advised of the possibility of such damage.
*/
#include <cmath>
#include <QFile>
#include <QXmlStreamReader>
#include <QStringList>
#include "haarcascade.h"
HaarCascadeHID::HaarCascadeHID(const HaarCascade &cascade,
int startX,
int endX,
int startY,
int endY,
int windowWidth,
int windowHeight,
int oWidth,
const quint32 *integral,
const quint32 *tiltedIntegral,
qreal step,
qreal invArea,
qreal scale,
bool cannyPruning,
const quint32 **p,
const quint64 **pq,
const quint32 **ip,
const quint32 **icp,
QList<QRect> *roi,
QMutex *mutex)
{
this->m_count = cascade.m_stages.size();
this->m_stages = new HaarStageHID *[this->m_count];
this->m_startX = startX;
this->m_endX = endX;
this->m_startY = startY;
this->m_endY = endY;
this->m_windowWidth = windowWidth;
this->m_windowHeight = windowHeight;
this->m_oWidth = oWidth;
this->m_step = step;
this->m_invArea = invArea;
this->m_isTree = cascade.m_isTree;
this->m_cannyPruning = cannyPruning;
this->m_roi = roi;
this->m_mutex = mutex;
for (int i = 0; i < 4; i++) {
this->m_p[i] = p[i];
this->m_pq[i] = pq[i];
this->m_ip[i] = ip[i];
this->m_icp[i] = icp[i];
}
for (int i = 0; i < this->m_count; i++)
this->m_stages[i] = new HaarStageHID(cascade.m_stages[i],
oWidth,
integral,
tiltedIntegral,
invArea,
scale);
for (int i = 0; i < this->m_count; i++) {
int parent = cascade.m_stages[i].parentStage();
this->m_stages[i]->m_parentStagePtr = parent < 0? NULL: this->m_stages[parent];
int next = cascade.m_stages[i].nextStage();
this->m_stages[i]->m_nextStagePtr = next < 0? NULL: this->m_stages[next];
int child = cascade.m_stages[i].childStage();
this->m_stages[i]->m_childStagePtr = child < 0? NULL: this->m_stages[child];
}
}
HaarCascadeHID::~HaarCascadeHID()
{
for (int i = 0; i < this->m_count; i++)
delete this->m_stages[i];
delete [] this->m_stages;
}
void HaarCascadeHID::run(HaarCascadeHID *cascade)
{
for (int j = cascade->m_startY; j < cascade->m_endY; j++) {
int y = qRound(j * cascade->m_step);
int iStep = 1;
for (int i = cascade->m_startX; i < cascade->m_endX; i += iStep) {
int x = qRound(i * cascade->m_step);
size_t offset = x + y * cascade->m_oWidth;
if (cascade->m_cannyPruning) {
qint32 sum = cascade->m_ip[0][offset]
- cascade->m_ip[1][offset]
- cascade->m_ip[2][offset]
+ cascade->m_ip[3][offset];
qint32 sumCanny = cascade->m_icp[0][offset]
- cascade->m_icp[1][offset]
- cascade->m_icp[2][offset]
+ cascade->m_icp[3][offset];
if (sum < 20 || sumCanny < 100) {
iStep = 2;
continue;
}
}
quint32 sum = cascade->m_p[0][offset]
- cascade->m_p[1][offset]
- cascade->m_p[2][offset]
+ cascade->m_p[3][offset];
quint64 sum2 = cascade->m_pq[0][offset]
- cascade->m_pq[1][offset]
- cascade->m_pq[2][offset]
+ cascade->m_pq[3][offset];
qreal mean = sum * cascade->m_invArea;
qreal varianceNormFactor = sum2 * cascade->m_invArea - mean * mean;
varianceNormFactor = (varianceNormFactor >= 0.0)? sqrt(varianceNormFactor): 1.0;
int stageResult = 1;
if (cascade->m_isTree) {
HaarStageHID *haarStage = cascade->m_stages[0];
while (haarStage) {
if (haarStage->pass(offset, varianceNormFactor))
haarStage = haarStage->m_childStagePtr;
else {
while (haarStage && haarStage->m_nextStagePtr == NULL)
haarStage = haarStage->m_parentStagePtr;
if (haarStage == NULL) {
stageResult = 0;
break;
}
haarStage = haarStage->m_nextStagePtr;
}
}
} else for (int stage = 0; stage < cascade->m_count; stage++)
if (!cascade->m_stages[stage]->pass(offset, varianceNormFactor)) {
stageResult = -stage;
break;
}
if (stageResult > 0) {
cascade->m_mutex->lock();
cascade->m_roi->append(QRect(x,
y,
cascade->m_windowWidth,
cascade->m_windowHeight));
cascade->m_mutex->unlock();
}
iStep = stageResult != 0? 1: 2;
}
}
delete cascade;
}
HaarCascade::HaarCascade(QObject *parent):
QObject(parent)
{
this->m_isTree = false;
}
HaarCascade::HaarCascade(const HaarCascade &other):
QObject(NULL)
{
this->m_name = other.m_name;
this->m_windowSize = other.m_windowSize;
this->m_stages = other.m_stages;
this->m_errorString = other.m_errorString;
this->m_isTree = other.m_isTree;
}
HaarCascade::~HaarCascade()
{
}
QString HaarCascade::name() const
{
return this->m_name;
}
QString &HaarCascade::name()
{
return this->m_name;
}
QSize HaarCascade::windowSize() const
{
return this->m_windowSize;
}
QSize &HaarCascade::windowSize()
{
return this->m_windowSize;
}
HaarStageVector HaarCascade::stages() const
{
return this->m_stages;
}
HaarStageVector &HaarCascade::stages()
{
return this->m_stages;
}
QString HaarCascade::errorString() const
{
return this->m_errorString;
}
bool HaarCascade::load(const QString &fileName)
{
this->resetName();
this->resetWindowSize();
this->resetStages();
QFile haarFile(fileName);
if (!haarFile.open(QIODevice::ReadOnly))
return false;
QXmlStreamReader haarReader(&haarFile);
QStringList pathList;
QString path;
QList<QRect> featureRectList;
QList<qreal> featureWeightList;
int stage = 0;
this->m_isTree = false;
while (!haarReader.atEnd()) {
QXmlStreamReader::TokenType token = haarReader.readNext();
if (token == QXmlStreamReader::Invalid) {
if (this->m_errorString != haarReader.errorString()) {
this->m_errorString = haarReader.errorString();
emit this->errorStringChanged(haarReader.errorString());
}
return false;
} else if (token == QXmlStreamReader::StartElement) {
pathList << haarReader.name().toString();
if (path.isEmpty()
&& haarReader.name() != "opencv_storage")
return false;
else if (path == "opencv_storage")
this->m_name = haarReader.name().toString();
else if (path == QString("opencv_storage/%1/stages").arg(this->m_name)
&& haarReader.name() == "_") {
this->m_stages << HaarStage();
this->m_stages.last().setParentStage(stage - 1);
}
else if (path == QString("opencv_storage/%1/stages/_/trees").arg(this->m_name)
&& haarReader.name() == "_") {
this->m_stages.last().trees() << HaarTree();
}
else if (path == QString("opencv_storage/%1/stages/_/trees/_").arg(this->m_name)
&& haarReader.name() == "_") {
this->m_stages.last().trees().last().features() << HaarFeature();
}
} else if (token == QXmlStreamReader::EndElement) {
if (path == QString("opencv_storage/%1/stages/_").arg(this->m_name)) {
int parent = this->m_stages.last().parentStage();
if (parent != -1
&& this->m_stages[parent].childStage() == -1)
this->m_stages[parent].setChildStage(stage);
this->m_isTree |= this->m_stages.last().nextStage() != -1;
stage++;
}
else if (path == QString("opencv_storage/%1/stages/_/trees/_/_/feature/rects").arg(this->m_name)) {
this->m_stages.last().trees().last().features().last().setRects(featureRectList.toVector());
this->m_stages.last().trees().last().features().last().setWeight(featureWeightList.toVector());
featureRectList.clear();
featureWeightList.clear();
}
pathList.removeLast();
} else if (token == QXmlStreamReader::Characters) {
if (path == QString("opencv_storage/%1/size").arg(this->m_name)) {
QStringList sizeStr = haarReader.text().toString().simplified().split(" ");
QSize size(sizeStr[0].toInt(), sizeStr[1].toInt());
if (!this->m_windowSize.isValid())
this->m_windowSize = size;
}
else if (path == QString("opencv_storage/%1/stages/_/stage_threshold").arg(this->m_name))
this->m_stages.last().threshold() = haarReader.text().toDouble();
else if (path == QString("opencv_storage/%1/stages/_/parent").arg(this->m_name))
this->m_stages.last().parentStage() = haarReader.text().toInt();
else if (path == QString("opencv_storage/%1/stages/_/next").arg(this->m_name))
this->m_stages.last().nextStage() = haarReader.text().toInt();
else if (path == QString("opencv_storage/%1/stages/_/trees/_/_/threshold").arg(this->m_name))
this->m_stages.last().trees().last().features().last().threshold() = haarReader.text().toDouble();
else if (path == QString("opencv_storage/%1/stages/_/trees/_/_/left_node").arg(this->m_name))
this->m_stages.last().trees().last().features().last().leftNode() = haarReader.text().toInt();
else if (path == QString("opencv_storage/%1/stages/_/trees/_/_/left_val").arg(this->m_name))
this->m_stages.last().trees().last().features().last().leftVal() = haarReader.text().toDouble();
else if (path == QString("opencv_storage/%1/stages/_/trees/_/_/right_node").arg(this->m_name))
this->m_stages.last().trees().last().features().last().rightNode() = haarReader.text().toInt();
else if (path == QString("opencv_storage/%1/stages/_/trees/_/_/right_val").arg(this->m_name))
this->m_stages.last().trees().last().features().last().rightVal() = haarReader.text().toDouble();
else if (path == QString("opencv_storage/%1/stages/_/trees/_/_/feature/rects/_").arg(this->m_name)) {
QStringList sizeStr = haarReader.text().toString().simplified().split(" ");
featureRectList << QRect(sizeStr[0].toInt(), sizeStr[1].toInt(),
sizeStr[2].toInt(), sizeStr[3].toInt());
featureWeightList << sizeStr[4].toDouble();
}
else if (path == QString("opencv_storage/%1/stages/_/trees/_/_/feature/tilted").arg(this->m_name))
this->m_stages.last().trees().last().features().last().tilted() = haarReader.text().toInt();
}
path = pathList.join("/");
}
return true;
}
HaarCascade &HaarCascade::operator =(const HaarCascade &other)
{
if (this != &other) {
this->m_name = other.m_name;
this->m_windowSize = other.m_windowSize;
this->m_stages = other.m_stages;
this->m_errorString = other.m_errorString;
this->m_isTree = other.m_isTree;
}
return *this;
}
bool HaarCascade::operator ==(const HaarCascade &other) const
{
if (this->m_name == other.m_name
&& this->m_windowSize == other.m_windowSize
&& this->m_stages == other.m_stages
&& this->m_isTree == other.m_isTree)
return true;
return true;
}
bool HaarCascade::operator !=(const HaarCascade &other) const
{
return !(*this == other);
}
void HaarCascade::setName(const QString &name)
{
if (this->m_name == name)
return;
this->m_name = name;
emit this->nameChanged(name);
}
void HaarCascade::setWindowSize(const QSize &windowSize)
{
if (this->m_windowSize == windowSize)
return;
this->m_windowSize = windowSize;
emit this->windowSizeChanged(windowSize);
}
void HaarCascade::setStages(const HaarStageVector &stages)
{
if (this->m_stages == stages)
return;
this->m_stages = stages;
emit this->stagesChanged(stages);
}
void HaarCascade::resetName()
{
this->setName(QString());
}
void HaarCascade::resetWindowSize()
{
this->setWindowSize(QSize());
}
void HaarCascade::resetStages()
{
this->setStages(HaarStageVector());
}