forked from rudsonm/tcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Watershed.cpp
103 lines (94 loc) · 3.16 KB
/
Watershed.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
#include "Watershed.h"
std::vector<cv::Mat> Watershed::segmentate(const Instance &instance) {
printf("Segmentating\n");
std::vector<cv::Mat> result;
for (int z = 0; z < instance.DEPTH; z++) {
result.push_back(cv::Mat::zeros(cv::Size(instance.WIDTH, instance.HEIGHT), CV_16U));
}
UniquePriorityQueue<Voxel> voxels;
for (const auto& peak : instance.peaks) {
for (Voxel voxel : peak.second)
result.at(voxel.z).at<ushort>(voxel.x, voxel.y) = peak.first;
for (Voxel voxel : peak.second) {
for (int zk = -1; zk <= 1; zk++) {
for (int xk = -1; xk <= 1; xk++) {
for (int yk = -1; yk <= 1; yk++) {
if (abs(zk) + abs(xk) + abs(yk) != 1)
continue;
int z = voxel.z + zk,
x = voxel.x + xk,
y = voxel.y + yk;
if (z < 0 || z >= instance.DEPTH
|| x < 0 || x >= instance.HEIGHT
|| y < 0 || y >= instance.WIDTH)
continue;
int neighbor = instance.rock.at(z).at<uchar>(x, y);
if (neighbor > 0 && result.at(z).at<ushort>(x, y) == 0)
voxels.push(Voxel(z, x, y, neighbor));
}
}
}
}
}
while (!voxels.empty()) {
Voxel voxel = voxels.pop();
std::set<int> neighborhood;
for (int zk = -1; zk <= 1; zk++) {
for (int xk = -1; xk <= 1; xk++) {
for (int yk = -1; yk <= 1; yk++) {
if (abs(zk) + abs(xk) + abs(yk) != 1)
continue;
int z = voxel.z + zk,
x = voxel.x + xk,
y = voxel.y + yk;
if (voxel.z == z && voxel.x == x && voxel.y == y)
continue;
if (z < 0 || z >= instance.DEPTH
|| x < 0 || x >= instance.HEIGHT
|| y < 0 || y >= instance.WIDTH)
continue;
int neighbor = result.at(z).at<ushort>(x, y);
int neighborDistance = instance.rock.at(z).at<uchar>(x, y);
if (neighbor == Watershed::WATERSHED_BARRIER || neighborDistance == 0)
continue;
if (neighbor == 0)
voxels.push(Voxel(z, x, y, neighborDistance));
else
neighborhood.insert(neighbor);
}
}
}
if (neighborhood.size() == 1)
result.at(voxel.z).at<ushort>(voxel.x, voxel.y) = *neighborhood.begin();
else if (neighborhood.size() > 1)
result.at(voxel.z).at<ushort>(voxel.x, voxel.y) = Watershed::WATERSHED_BARRIER;
}
return result;
}
std::vector<cv::Mat> Watershed::paint(const Instance &instance) {
std::vector<cv::Mat> result(instance.DEPTH);
std::map<int, cv::Vec3b> colors;
for (int z = 0; z < instance.DEPTH; z++) {
cv::Mat colored = cv::Mat(instance.HEIGHT, instance.WIDTH, CV_8UC3);
for (int x = 0; x < instance.HEIGHT; x++) {
for (int y = 0; y < instance.WIDTH; y++) {
int label = instance.rock.at(z).at<ushort>(x, y);
cv::Vec3b color;
if (label == 0)
color = cv::Vec3b(0, 0, 0);
else if (colors.find(label) == colors.end()) {
int b = cv::theRNG().uniform(0, 256);
int g = cv::theRNG().uniform(0, 256);
int r = cv::theRNG().uniform(0, 256);
color = cv::Vec3b((uchar)b, (uchar)g, (uchar)r);
colors.insert(std::make_pair(label, color));
} else {
color = colors[label];
}
colored.at<cv::Vec3b>(x, y) = color;
}
}
result.at(z) = colored;
}
return result;
}