193bool label(
const mask &input,
bool identify_isolated_patched,
int min_label,
194 bool assign_final_labels, array &output) {
197 const int col_min = 0;
198 const int row_min = 0;
199 auto shape = input.shape();
200 const int nrows = shape[0];
201 const int ncols = shape[1];
203 const int background = 0;
204 const int attached_label = 1;
205 const int min_provisional_label = 2;
212 std::vector<int> labels = {background, attached_label};
213 int provisional_label = min_provisional_label;
215 std::vector<Run> runs;
218 std::vector<int> row_above(ncols, background);
220 for (
int r = row_min; r < nrows; ++r) {
224 if (input.is_foreground(r, c)) {
227 int L = provisional_label;
230 runs.push_back({ r , c_start , 0 ,
L });
231 Run ¤t_run = runs.back();
234 while (c < ncols and input.is_foreground(r, c)) {
236 if (identify_isolated_patched and input.is_attached(r, c)) {
238 if (
L != provisional_label) {
239 labels[
L] = attached_label;
244 int T = row_above[c];
247 if (T != background) {
250 if (
L != provisional_label) {
260 if (
L == provisional_label) {
265 provisional_label += 1;
269 current_run.
length = c - c_start;
273 for (
int n = 0;
n < current_run.
length; ++
n) {
274 row_above[c_start +
n] =
L;
278 row_above[c] = background;
284 auto N_labels =
static_cast<int>(labels.size());
288 for (
int k = 0;
k < N_labels; ++
k) {
295 if (assign_final_labels) {
297 if (identify_isolated_patched) {
299 for (
int k = 0;
k < N_labels; ++
k) {
300 labels[
k] = (labels[
k] == attached_label) ? 0 : 1;
305 for (
int k = min_provisional_label;
k < N_labels; ++
k) {
306 if (labels[
k] ==
k) {
310 labels[
k] = labels[labels[
k]];
320 const int attached = min_label + (1 - (min_label % 2));
323 int L = attached + 1;
324 for (
int k = min_provisional_label;
k < N_labels; ++
k) {
325 if (labels[
k] == attached_label) {
326 labels[
k] = attached;
327 }
else if (labels[
k] ==
k) {
331 labels[
k] = labels[labels[
k]];
338 for (
int k = 0;
k < (
int)runs.size(); ++
k) {
340 int L = labels[r.label];
341 for (
int n = 0;
n < r.length; ++
n) {
342 output(r.row, r.col +
n) =
L;
346 return (not runs.empty());