/
BackgroundFinder.cpp
59 lines (50 loc) · 1.38 KB
/
BackgroundFinder.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
#include "BackgroundFinder.h"
#include <QtDebug>
#include <QtConcurrentRun>
BackgroundFinder::BackgroundFinder(QObject *parent) :
QObject(parent)
{
}
const QImage &BackgroundFinder::background() const
{
return _bg;
}
void BackgroundFinder::reset()
{
_bg.fill(Qt::white);
_rolling.clear();
}
void BackgroundFinder::addFrame(const QImage &img)
{
if (_rolling.isEmpty())
{
for (int i = 0; i < img.width() * img.height(); i++)
{
_rolling.append(RollingMedianCalculator());
}
}
QVector<QFuture<QRgb> > futures;
futures.reserve(img.width() * img.height());
for (int y = 0; y < img.height(); y++)
{
for (int x = 0; x < img.width(); x++)
{
const int index = y * img.width() + x;
QFuture<QRgb> future = QtConcurrent::run(&_rolling[index],
&RollingMedianCalculator::next,
img.pixel(x,y));
futures.append(future);
}
}
QImage newBG(img.size(), img.format());
for (int y = 0; y < img.height(); y++)
{
for (int x = 0; x < img.width(); x++)
{
const int index = y * img.width() + x;
futures[index].waitForFinished();
newBG.setPixel(x,y, futures.at(index).result());
}
}
_bg = newBG;
}