5 image.resize(rows*cols);
6 image.assign(begin(m), end(m));
16 for (
int i=0; i<
image.size(); i++) {
25 for (
int i=1; i<stat.size(); i++) {
32 for (
int i=0; i<label.size(); ++i) {
33 label[i] = stat[label[i]];
36 copy(label.begin(), label.end(), begin(
label2d));
49 vector<int> parent(
cols*rows);
50 vector<int> labels(
cols*rows);
55 for (
int y = 0; y <
rows; ++y ) {
56 for (
int x = 0; x <
cols; ++x ) {
61 bool connected =
false;
64 if (x > 0 &&
image[y*cols+x-1] ==
image[y*cols+x]) {
70 if (y > 0 &&
image[(y-1)*cols+x] ==
image[y*cols+x] && (!connected ||
image[(y-1)*cols+x] < k)) {
71 k = rst[(y-1)*cols+x];
85 if (x > 0 &&
image[y*cols+x-1] ==
image[y*cols+x] && rst[y*cols+x-1] != k)
86 uf_union(k, rst[y*cols+x-1], parent);
87 if (y > 0 &&
image[(y-1)*cols+x] ==
image[y*cols+x] && rst[(y-1)*cols+x] != k)
88 uf_union(k, rst[(y-1)*cols+x], parent);
97 rst[i] =
uf_find(rst[i], parent, labels);
115 while ( parent[x]>0 )
117 while ( parent[y]>0 )
128 while (parent[x] > 0) {
143 for (
int i=0; i<
label2d.size(); ++i) {
144 if (
label2d[i] == robotsLabel) {
159 vector<float>
f(max(
rows, cols));
160 vector<float>
d(f.size());
161 vector<int> v(f.size());
162 vector<float> z(f.size() + 1);
164 for (
int x=0; x<
cols; x++) {
165 for (
int y=0; y<
rows; y++) {
167 f[y] =
BinaryRobot[y*cols+x] == 0 ? numeric_limits<float>::max() : 0;
169 f[y] =
BinaryNonRobot[y*cols+x] == 0 ? numeric_limits<float>::max() : 0;
173 for (
int y = 0; y <
rows; y++) {
174 Region[y*cols+x] =
d[y];
178 float maxV=0, minV=numeric_limits<float>::max();
179 for (
int y = 0; y <
rows; y++) {
182 for (
int x = 0; x <
cols; x++) {
183 Region[y*cols+x] = (float) sqrt(
d[x]);
184 if (maxV < Region[y*cols+x]) {maxV = Region[y*cols+x];}
185 if (minV > Region[y*cols+x]) {minV = Region[y*cols+x];}
191 for (
int y = 0; y <
rows; y++) {
192 for (
int x = 0; x <
cols; x++) {
194 Region[y*cols+x] = (Region[y*cols+x] - minV) * (1/(maxV-minV)) +1;
196 Region[y*cols+x] = (Region[y*cols+x] - minV) * (1/(maxV-minV));
207 z[0] = -numeric_limits<float>::max();
208 z[1] = numeric_limits<float>::max();
210 for (
int q = 1; q < f.size(); q++) {
211 float s = ((f[q] + q * q) - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);
215 s = ((f[q] + q * q) - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);
220 z[k + 1] = numeric_limits<float>::max();
224 for (
int q = 0; q < f.size(); q++) {
225 while (z[k + 1] < q) {k++;}
227 d[q] = (q - v[k]) * (q - v[k]) + f[v[k]];
233 valarray<float> col = A[slice(row*
cols, cols, 1)];
234 return vector<float>(begin(col), end(col));
valarray< int > BinaryRobot
Binary array of the label assigned to a robot.
int next_label
The current label ID.
vector< int > image
The image to perform the labeling on.
void uf_union(int x, int y, vector< int > &parent)
Combine two labels.
connected_components(valarray< int > m, int rows, int cols, bool zeroAsBg)
Constructor that initializes the private member variables.
int getMaxLabel()
Get the max label of the labeling process which is in the range [0,max_label].
void constructBinaryImages(int robotsLabel)
Create binary arrays of connected labels.
vector< float > getVector(valarray< float > A, int row)
Get a slice of an array.
vector< int > labeling()
Label the connected components.
int cols
Number of columns of the image.
valarray< float > NormalizedEuclideanDistanceBinary(bool RobotR)
Calculate the normalized euclidean distance transform of a binary image with foreground pixels set to...
valarray< int > compactLabeling()
Rearrange the labels to make the label numbers consecutive.
int rows
Number of rows of the image.
valarray< int > label2d
Array of labels.
bool zeroAsBg
Whether 0 should be considered or not.
valarray< int > BinaryNonRobot
Binary array of the labels not assigned to a robot.
int uf_find(int x, vector< int > parent, vector< int > &label)
Return the root label (starts from 1 because label array is initialized to 0 at first). The label array records the new label for every root.
void DT1D(vector< float > f, vector< float > &d, vector< int > &v, vector< float > &z)
DT1D.