-
Notifications
You must be signed in to change notification settings - Fork 3
/
ccl_le.cpp
191 lines (159 loc) · 4.33 KB
/
ccl_le.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
// Marathon Match - CCL - Label Equivalence
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <list>
#include <algorithm>
#include <utility>
#include <cmath>
#include <functional>
#include <cstring>
#include <cmath>
#include <limits>
#define NOMINMAX
#ifdef _MSC_VER
#include <ctime>
inline double get_time()
{
return static_cast<double>(std::clock()) / CLOCKS_PER_SEC;
}
#else
#include <sys/time.h>
inline double get_time()
{
timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + 1e-6 * tv.tv_usec;
}
#endif
using namespace std;
void init_CCL(int L[], int R[], int N)
{
for (int id = 0; id < N; id++) L[id] = R[id] = id;
}
inline int diff(int d1, int d2)
{
return abs(((d1>>16) & 0xff) - ((d2>>16) & 0xff)) + abs(((d1>>8) & 0xff) - ((d2>>8) & 0xff)) + abs((d1 & 0xff) - (d2 & 0xff));
}
bool scanning(int D[], int L[], int R[], int N, int W, int th)
{
int m = false;
for (int id = 0; id < N; id++) {
int Did = D[id];
int label = N;
if (id - W >= 0 && diff(Did, D[id-W]) <= th) label = min(label, L[id-W]);
if (id + W < N && diff(Did, D[id+W]) <= th) label = min(label, L[id+W]);
int r = id % W;
if (r && diff(Did, D[id-1]) <= th) label = min(label, L[id-1]);
if (r + 1 != W && diff(Did, D[id+1]) <= th) label = min(label, L[id+1]);
if (label < L[id]) {
R[L[id]] = label;
m = true;
}
}
return m;
}
bool scanning8(int D[], int L[], int R[], int N, int W, int th)
{
bool m = false;
for (int id = 0; id < N; id++) {
int Did = D[id];
int label = N;
if (id - W >= 0 && diff(Did, D[id-W]) <= th) label = min(label, L[id-W]);
if (id + W < N && diff(Did, D[id+W]) <= th) label = min(label, L[id+W]);
int r = id % W;
if (r) {
if (diff(Did, D[id-1]) <= th) label = min(label, L[id-1]);
if (id - W - 1 >= 0 && diff(Did, D[id-W-1]) <= th) label = min(label, L[id-W-1]);
if (id + W - 1 < N && diff(Did, D[id+W-1]) <= th) label = min(label, L[id+W-1]);
}
if (r + 1 != W) {
if (diff(Did, D[id+1]) <= th) label = min(label, L[id+1]);
if (id - W + 1 >= 0 && diff(Did, D[id-W+1]) <= th) label = min(label, L[id-W+1]);
if (id + W + 1 < N && diff(Did, D[id+W+1]) <= th) label = min(label, L[id+W+1]);
}
if (label < L[id]) {
R[L[id]] = label;
m = true;
}
}
return m;
}
void analysis(int D[], int L[], int R[], int N)
{
for (int id = 0; id < N; id++) {
int label = L[id];
int ref;
if (label == id) {
do { label = R[ref = label]; } while (ref ^ label);
R[id] = label;
}
}
}
void labeling(int D[], int L[], int R[], int N)
{
for (int id = 0; id < N; id++) L[id] = R[R[L[id]]];
}
class CCL {
public:
vector<int> ccl(vector<int>& image, int W, int degree_of_connectivity, int threshold);
};
vector<int> CCL::ccl(vector<int>& image, int W, int degree_of_connectivity, int threshold)
{
int* D = static_cast<int*>(&image[0]);
int N = image.size();
int* L = new int[N];
int* R = new int[N];
init_CCL(L, R, N);
for (;;) {
if (degree_of_connectivity == 4 ? scanning(D, L, R, N, W, threshold) : scanning8(D, L, R, N, W, threshold)) {
analysis(D, L, R, N);
labeling(D, L, R, N);
} else break;
}
vector<int> result(L, L + N);
delete [] L;
delete [] R;
return result;
}
void read_data(const string filename, vector<int>& image, int& W, int& degree_of_connectivity, int& threshold)
{
fstream fs(filename.c_str(), ios_base::in);
string line;
stringstream ss;
int data;
getline(fs, line);
ss.str(line);
ss >> W >> degree_of_connectivity >> threshold;
getline(fs, line);
ss.str(""); ss.clear();
for (ss.str(line); ss >> data; image.push_back(data));
}
int main(int argc, char* argv[])
{
ios_base::sync_with_stdio(false);
if (argc < 2) {
cerr << "Usage: " << argv[0] << " input_file" << endl;
exit(1);
}
vector<int> image;
int W, degree_of_connectivity, threshold;
read_data(argv[1], image, W, degree_of_connectivity, threshold);
CCL ccl;
double start = get_time();
vector<int> result(ccl.ccl(image, W, degree_of_connectivity, threshold));
double end = get_time();
cerr << "Time: " << end - start << endl;
cout << result.size() << endl; /// number of pixels
cout << W << endl; /// width
for (int i = 0; i < static_cast<int>(result.size()) / W; i++) {
for (int j = 0; j < W; j++) cout << result[i*W+j] << " ";
cout << endl;
}
return 0;
}