-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrwcralgorithm.cpp
More file actions
114 lines (95 loc) · 2.41 KB
/
Copy pathcrwcralgorithm.cpp
File metadata and controls
114 lines (95 loc) · 2.41 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
#include "crwcralgorithm.h"
#include <QImage>
#include <array>
/**
* \brief Simple 2D median filter with 3X3 window
* \tparam T
* \param img
* \param w
* \param h
*/
template <typename T>
void medianFilter(T* img, int w, int h)
{
size_t numPixels = w * h;
int kernelSize = 3;
T* imgOut = new T[numPixels];
memcpy(imgOut, img, numPixels * sizeof(T));
int halfSize = kernelSize / 2;
#pragma omp parallel for
for (int x = halfSize; x < w - halfSize; x++)
{
for (int y = halfSize; y < h - halfSize; y++)
{
std::array<T, 9> elements;
int subIdx = 0;
for (int i = -halfSize; i <= halfSize; i++)
{
for (int j = -halfSize; j <= halfSize; j++)
{
size_t idx = x + i + (y + j) * w;
elements[subIdx++] = img[idx];
}
}
std::sort(elements.begin(), elements.end());
imgOut[x + y * w] = elements[kernelSize * kernelSize / 2 + 1];
}
}
memcpy(img, imgOut, numPixels * sizeof(T));
delete[] imgOut;
imgOut = nullptr;
}
CRWCRAlgorithm::CRWCRAlgorithm(QObject* parent)
: QObject(parent), solver_(nullptr), isPreProcess_(false), image_(nullptr)
{
twoLabelSeed_ = new TwoLabelSeed();
}
void CRWCRAlgorithm::process()
{
// initialize seed buffer
twoLabelSeed_->initialize(dim_);
solver_->setSeed(twoLabelSeed_);
solver_->solve();
emit segmentationTime(solver_->getUseTime());
emit segmentationDone(solver_->generateProbabilityImage());
}
CRWCRAlgorithm::~CRWCRAlgorithm()
{
delete[] image_;
image_ = nullptr;
delete twoLabelSeed_;
twoLabelSeed_ = nullptr;
delete solver_;
solver_ = nullptr;
}
/**
* \brief Set image data
* \param data
*/
void CRWCRAlgorithm::setImage(const QImage& data)
{
dim_ = QSize(data.width(), data.height());
delete[] image_;
image_ = new float[dim_.width() * dim_.height()];
for (int x = 0; x < dim_.width(); x++)
{
for (int y = 0; y < dim_.height(); y++)
{
QColor col = data.pixelColor(x, y);
//rgb2gray: 0.2989 * R + 0.5870 * G + 0.1140 * B
// normalize to [0,1]
image_[x + dim_.width() * y] = (0.2989 * col.red() + 0.5870 * col.green() + 0.1140 * col.blue()) / 255;
}
}
if (isPreProcess_)
{
medianFilter(image_, dim_.width(), dim_.height());
}
delete solver_;
solver_ = nullptr;
solver_ = new CRWCRSolver(image_, dim_.width(), dim_.height());
}
void CRWCRAlgorithm::setSeeds(const PointListGeometry& foregroundseed, const PointListGeometry& backgroundseed)
{
twoLabelSeed_->setSeeds(foregroundseed, backgroundseed);
}