edgeelement.cpp
15.3 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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/* Webcamoid, webcam capture application.
* Copyright (C) 2016 Gonzalo Exequiel Pedone
*
* Webcamoid 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.
*
* Webcamoid 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 Webcamoid. If not, see <http://www.gnu.org/licenses/>.
*
* Web-Site: http://webcamoid.github.io/
*/
#include <QQmlContext>
#include <QVector>
#include <QtMath>
#include <akfrac.h>
#include <akpacket.h>
#include <akvideocaps.h>
#include <akvideoconverter.h>
#include <akvideopacket.h>
#include "edgeelement.h"
class EdgeElementPrivate
{
public:
int m_thLow {510};
int m_thHi {1020};
bool m_canny {false};
bool m_equalize {false};
bool m_invert {false};
AkVideoConverter m_videoConverter {{AkVideoCaps::Format_ya88pack, 0, 0, {}}};
AkVideoPacket equalize(const AkVideoPacket &src);
void sobel(const AkVideoPacket &gray,
AkVideoPacket &gradient,
AkVideoPacket &direction) const;
AkVideoPacket thinning(const AkVideoPacket &gradient,
const AkVideoPacket &direction) const;
AkVideoPacket threshold(const AkVideoPacket &thinned,
const QVector<int> &thresholds,
const QVector<int> &map) const;
void trace(AkVideoPacket &canny,
int x,
int y) const;
AkVideoPacket hysteresisThresholding(const AkVideoPacket &thresholded) const;
};
EdgeElement::EdgeElement(): AkElement()
{
this->d = new EdgeElementPrivate;
}
EdgeElement::~EdgeElement()
{
delete this->d;
}
bool EdgeElement::canny() const
{
return this->d->m_canny;
}
int EdgeElement::thLow() const
{
return this->d->m_thLow;
}
int EdgeElement::thHi() const
{
return this->d->m_thHi;
}
bool EdgeElement::equalize() const
{
return this->d->m_equalize;
}
bool EdgeElement::invert() const
{
return this->d->m_invert;
}
QString EdgeElement::controlInterfaceProvide(const QString &controlId) const
{
Q_UNUSED(controlId)
return QString("qrc:/Edge/share/qml/main.qml");
}
void EdgeElement::controlInterfaceConfigure(QQmlContext *context,
const QString &controlId) const
{
Q_UNUSED(controlId)
context->setContextProperty("Edge", const_cast<QObject *>(qobject_cast<const QObject *>(this)));
context->setContextProperty("controlId", this->objectName());
}
AkPacket EdgeElement::iVideoStream(const AkVideoPacket &packet)
{
this->d->m_videoConverter.begin();
auto src = this->d->m_videoConverter.convert(packet);
this->d->m_videoConverter.end();
if (!src)
return {};
AkVideoPacket dst(src.caps());
dst.copyMetadata(src);
AkVideoPacket src_;
if (this->d->m_equalize)
src_ = this->d->equalize(src);
else
src_ = src;
AkVideoPacket gradient;
AkVideoPacket direction;
this->d->sobel(src_, gradient, direction);
auto invert = this->d->m_invert;
if (this->d->m_canny) {
auto thinned = this->d->thinning(gradient, direction);
QVector<int> thresholds {this->d->m_thLow, this->d->m_thHi};
QVector<int> colors {0, 127, 255};
auto thresholded = this->d->threshold(thinned, thresholds, colors);
auto canny = this->d->hysteresisThresholding(thresholded);
for (int y = 0; y < src.caps().height(); y++) {
auto cannyLine = canny.constLine(0, y);
auto srcLine = reinterpret_cast<const quint16 *>(src_.constLine(0, y));
auto dstLine = reinterpret_cast<quint16 *>(dst.line(0, y));
for (int x = 0; x < src.caps().width(); x++) {
auto &pixel = cannyLine[x];
quint16 y = invert? 255 - pixel: pixel;
quint16 a = srcLine[x] & 0xff;
dstLine[x] = y << 8 | a;
}
}
} else {
for (int y = 0; y < src.caps().height(); y++) {
auto gradientLine = reinterpret_cast<const quint16 *>(gradient.constLine(0, y));
auto srcLine = reinterpret_cast<const quint16 *>(src_.constLine(0, y));
auto dstLine = reinterpret_cast<quint16 *>(dst.line(0, y));
for (int x = 0; x < src.caps().width(); x++) {
auto pixel = quint8(qBound<int>(0, gradientLine[x], 255));
quint16 y = invert? 255 - pixel: pixel;
quint16 a = srcLine[x] & 0xff;
dstLine[x] = y << 8 | a;
}
}
}
if (dst)
emit this->oStream(dst);
return dst;
}
void EdgeElement::setCanny(bool canny)
{
if (this->d->m_canny == canny)
return;
this->d->m_canny = canny;
emit this->cannyChanged(canny);
}
void EdgeElement::setThLow(int thLow)
{
if (this->d->m_thLow == thLow)
return;
this->d->m_thLow = thLow;
emit this->thLowChanged(thLow);
}
void EdgeElement::setThHi(int thHi)
{
if (this->d->m_thHi == thHi)
return;
this->d->m_thHi = thHi;
emit this->thHiChanged(thHi);
}
void EdgeElement::setEqualize(bool equalize)
{
if (this->d->m_equalize == equalize)
return;
this->d->m_equalize = equalize;
emit this->equalizeChanged(equalize);
}
void EdgeElement::setInvert(bool invert)
{
if (this->d->m_invert == invert)
return;
this->d->m_invert = invert;
emit this->invertChanged(invert);
}
void EdgeElement::resetCanny()
{
this->setCanny(false);
}
void EdgeElement::resetThLow()
{
this->setThLow(510);
}
void EdgeElement::resetThHi()
{
this->setThHi(1020);
}
void EdgeElement::resetEqualize()
{
this->setEqualize(false);
}
void EdgeElement::resetInvert()
{
this->setInvert(false);
}
AkVideoPacket EdgeElementPrivate::equalize(const AkVideoPacket &src)
{
AkVideoPacket dst(src.caps());
dst.copyMetadata(src);
int minGray = 255;
int maxGray = 0;
for (int y = 0; y < src.caps().height(); y++) {
auto line = reinterpret_cast<const quint16 *>(src.constLine(0, y));
for (int x = 0; x < src.caps().width(); x++) {
auto gray = line[x] >> 8;
if (gray < minGray)
minGray = gray;
if (gray > maxGray)
maxGray = gray;
}
}
if (maxGray == minGray) {
for (int y = 0; y < src.caps().height(); y++) {
auto srcLine = reinterpret_cast<const quint16 *>(src.constLine(0, y));
auto dstLine = reinterpret_cast<quint16 *>(dst.line(0, y));
for (int x = 0; x < src.caps().width(); x++)
dstLine[x] = minGray << 8 | srcLine[x] & 0xff;
}
} else {
int diffGray = maxGray - minGray;
quint8 colorTable[256];
for (int i = 0; i < 256; i++)
colorTable[i] = quint8(255 * (i - minGray) / diffGray);
for (int y = 0; y < src.caps().height(); y++) {
auto srcLine = reinterpret_cast<const quint16 *>(src.constLine(0, y));
auto dstLine = reinterpret_cast<quint16 *>(dst.line(0, y));
for (int x = 0; x < src.caps().width(); x++) {
auto &pixel = srcLine[x];
auto y = pixel >> 8;
auto a = pixel & 0xff;
dstLine[x] = colorTable[y] << 8 | a;
}
}
}
return dst;
}
void EdgeElementPrivate::sobel(const AkVideoPacket &gray,
AkVideoPacket &gradient,
AkVideoPacket &direction) const
{
auto caps = gray.caps();
caps.setFormat(AkVideoCaps::Format_y16);
gradient = {caps};
gradient.copyMetadata(gray);
caps.setFormat(AkVideoCaps::Format_y8);
direction = {caps};
direction.copyMetadata(gray);
auto width_1 = gray.caps().width() - 1;
auto height_1 = gray.caps().height() - 1;
for (int y = 0; y < gray.caps().height(); y++) {
auto grayLine = reinterpret_cast<const quint16 *>(gray.constLine(0, y));
auto grayLine_m1 = reinterpret_cast<const quint16 *>(gray.constLine(0, qMax(y - 1, 0)));
auto grayLine_p1 = reinterpret_cast<const quint16 *>(gray.constLine(0, qMin(y + 1, height_1)));
auto gradientLine = reinterpret_cast<quint16 *>(gradient.line(0, y));
auto directionLine = direction.line(0, y);
for (int x = 0; x < gray.caps().width(); x++) {
int x_m1 = qMax(x - 1, 0);
int x_p1 = qMin(x + 1, width_1);
int pixel_m1_p1 = grayLine_m1[x_p1] >> 8;
int pixel_p1_p1 = grayLine_p1[x_p1] >> 8;
int pixel_m1_m1 = grayLine_m1[x_m1] >> 8;
int pixel_p1_m1 = grayLine_p1[x_m1] >> 8;
int gradX = pixel_m1_p1
+ 2 * int(grayLine[x_p1] >> 8)
+ pixel_p1_p1
- pixel_m1_m1
- 2 * int(grayLine[x_m1] >> 8)
- pixel_p1_m1;
int gradY = pixel_m1_m1
+ 2 * int(grayLine_m1[x] >> 8)
+ pixel_m1_p1
- pixel_p1_m1
- 2 * int(grayLine_p1[x] >> 8)
- pixel_p1_p1;
gradientLine[x] = quint16(qAbs(gradX) + qAbs(gradY));
/* Gradient directions are classified in 4 possible cases
*
* dir 0
*
* x x x
* - - -
* x x x
*
* dir 1
*
* x x /
* x / x
* / x x
*
* dir 2
*
* \ x x
* x \ x
* x x \
*
* dir 3
*
* x | x
* x | x
* x | x
*/
if (gradX == 0 && gradY == 0)
directionLine[x] = 0;
else if (gradX == 0)
directionLine[x] = 3;
else {
qreal a = 180. * atan(qreal(gradY) / gradX) / M_PI;
if (a >= -22.5 && a < 22.5)
directionLine[x] = 0;
else if (a >= 22.5 && a < 67.5)
directionLine[x] = 1;
else if (a >= -67.5 && a < -22.5)
directionLine[x] = 2;
else
directionLine[x] = 3;
}
}
}
}
AkVideoPacket EdgeElementPrivate::thinning(const AkVideoPacket &gradient,
const AkVideoPacket &direction) const
{
AkVideoPacket thinned(gradient.caps(), true);
thinned.copyMetadata(gradient);
auto width_1 = gradient.caps().width() - 1;
auto height_1 = gradient.caps().height() - 1;
for (int y = 0; y < gradient.caps().height(); y++) {
auto edgesLine = reinterpret_cast<const quint16 *>(gradient.constLine(0, y));
auto edgesLine_m1 = reinterpret_cast<const quint16 *>(gradient.constLine(0, qMax(y - 1, 0)));
auto edgesLine_p1 = reinterpret_cast<const quint16 *>(gradient.constLine(0, qMin(y + 1, height_1)));
auto edgesAngleLine = direction.constLine(0, y);
auto thinnedLine = reinterpret_cast<quint16 *>(thinned.line(0, y));
for (int x = 0; x < gradient.caps().width(); x++) {
int x_m1 = qMax(x - 1, 0);
int x_p1 = qMin(x + 1, width_1);
auto &direction = edgesAngleLine[x];
if (direction == 0) {
/* x x x
* - - -
* x x x
*/
if (edgesLine[x] >= edgesLine[x_m1]
&& edgesLine[x] >= edgesLine[x_p1])
thinnedLine[x] = edgesLine[x];
} else if (direction == 1) {
/* x x /
* x / x
* / x x
*/
if (edgesLine[x] >= edgesLine_m1[x_p1]
&& edgesLine[x] >= edgesLine_p1[x_m1])
thinnedLine[x] = edgesLine[x];
} else if (direction == 2) {
/* \ x x
* x \ x
* x x \
*/
if (edgesLine[x] >= edgesLine_m1[x_m1]
&& edgesLine[x] >= edgesLine_p1[x_p1])
thinnedLine[x] = edgesLine[x];
} else {
/* x | x
* x | x
* x | x
*/
if (edgesLine[x] >= edgesLine_m1[x]
&& edgesLine[x] >= edgesLine_p1[x])
thinnedLine[x] = edgesLine[x];
}
}
}
return thinned;
}
AkVideoPacket EdgeElementPrivate::threshold(const AkVideoPacket &thinned,
const QVector<int> &thresholds,
const QVector<int> &map) const
{
auto caps = thinned.caps();
caps.setFormat(AkVideoCaps::Format_y8);
AkVideoPacket out(caps);
out.copyMetadata(thinned);
for (int y = 0; y < thinned.caps().height(); y++) {
auto srcLine = reinterpret_cast<const quint16 *>(thinned.constLine(0, y));
auto dstLine = out.line(0, y);
for (int x = 0; x < thinned.caps().width(); x++) {
auto &pixel = srcLine[x];
int value = -1;
for (int j = 0; j < thresholds.size(); j++)
if (pixel <= thresholds[j]) {
value = map[j];
break;
}
dstLine[x] = quint8(value < 0? map[thresholds.size()]: value);
}
}
return out;
}
AkVideoPacket EdgeElementPrivate::hysteresisThresholding(const AkVideoPacket &thresholded) const
{
auto canny = thresholded;
for (int y = 0; y < canny.caps().height(); y++)
for (int x = 0; x < canny.caps().width(); x++)
this->trace(canny, x, y);
for (int y = 0; y < canny.caps().height(); y++) {
auto line = canny.line(0, y);
for (int x = 0; x < canny.caps().width(); x++) {
auto &pixel = line[x];
if (pixel == 127)
pixel = 0;
}
}
return canny;
}
void EdgeElementPrivate::trace(AkVideoPacket &canny, int x, int y) const
{
auto cannyLine = canny.line(0, y);
if (cannyLine[x] != 255)
return;
auto lineSize = canny.lineSize(0);
bool isPoint = true;
for (int j = -1; j < 2; j++) {
int nextY = y + j;
if (nextY < 0 || nextY >= canny.caps().height())
continue;
auto cannyLineNext = cannyLine + j * lineSize;
for (int i = -1; i < 2; i++) {
int nextX = x + i;
if (i == 0 && j == 0)
continue;
if (nextX < 0 || nextX >= canny.caps().width())
continue;
auto &pixel = cannyLineNext[nextX];
if (pixel == 127) {
pixel = 255;
this->trace(canny, nextX, nextY);
}
if (pixel > 0)
isPoint = false;
}
}
if (isPoint)
cannyLine[x] = 0;
}
#include "moc_edgeelement.cpp"