From 9caf6b4f2c5f3e58d9389188604937b559855dd3 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 01:47:55 -0400 Subject: [PATCH 01/19] perf: faster labeling3d_naive.h --- include/labeling3D_naive.h | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/include/labeling3D_naive.h b/include/labeling3D_naive.h index 5157e6a..6b3256d 100644 --- a/include/labeling3D_naive.h +++ b/include/labeling3D_naive.h @@ -124,14 +124,10 @@ class naive_3D : public Labeling3D { // Second scan LabelsSolver::Flatten(); + const int voxels = img_labels_.size[0] * img_labels_.size[1] * img_labels_.size[2]; int * img_row = reinterpret_cast(img_labels_.data); - for (int z = 0; z < img_labels_.size[0]; z++) { - for (int y = 0; y < img_labels_.size[1]; y++) { - for (int x = 0; x < img_labels_.size[2]; x++) { - img_row[x] = LabelsSolver::GetLabel(img_row[x]); - } - img_row += img_labels_.step[1] / sizeof(int); - } + for (int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); } LabelsSolver::Dealloc(); // Memory deallocation of the labels solver @@ -282,14 +278,10 @@ class naive_3D : public Labeling3D { // Second scan LabelsSolver::Flatten(); + const int voxels = img_labels_.size[0] * img_labels_.size[1] * img_labels_.size[2]; int * img_row = reinterpret_cast(img_labels_.data); - for (int z = 0; z < img_labels_.size[0]; z++) { - for (int y = 0; y < img_labels_.size[1]; y++) { - for (int x = 0; x < img_labels_.size[2]; x++) { - img_row[x] = LabelsSolver::GetLabel(img_row[x]); - } - img_row += img_labels_.step[1] / sizeof(int); - } + for (int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); } } }; From 807aa408c17c2c27fef1d4c95c7b57b2a08bd2be Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 01:49:51 -0400 Subject: [PATCH 02/19] fix: qualify move with std::move --- src/labeling_algorithms.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/labeling_algorithms.cc b/src/labeling_algorithms.cc index 2b66025..ae23ea0 100644 --- a/src/labeling_algorithms.cc +++ b/src/labeling_algorithms.cc @@ -135,7 +135,7 @@ void KernelMapSingleton::InitializeAlgorithm(const std::string& algorithm, const })); v.push_back(s); } - KernelMapSingleton::GetInstance().data_[algorithm] = move(v); + KernelMapSingleton::GetInstance().data_[algorithm] = std::move(v); } From d15a07886aefe69208edbea746b819cb7e5dac5e Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 01:50:38 -0400 Subject: [PATCH 03/19] fix: qualify CheckAlg with override --- include/labeling_algorithms.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/labeling_algorithms.h b/include/labeling_algorithms.h index e9383ca..bebfa84 100644 --- a/include/labeling_algorithms.h +++ b/include/labeling_algorithms.h @@ -85,7 +85,7 @@ class Labeling2D : public Labeling { virtual ~Labeling2D() = default; - virtual std::string CheckAlg() const { return LabelingCheckSingleton2D::GetCheckAlg(Conn, LabelBackground); } + virtual std::string CheckAlg() const override { return LabelingCheckSingleton2D::GetCheckAlg(Conn, LabelBackground); } virtual bool IsLabelBackground() const override { return LabelBackground; } @@ -106,7 +106,7 @@ class Labeling3D : public Labeling { virtual ~Labeling3D() = default; - virtual std::string CheckAlg() const { return LabelingCheckSingleton3D::GetCheckAlg(Conn, LabelBackground); } + virtual std::string CheckAlg() const override { return LabelingCheckSingleton3D::GetCheckAlg(Conn, LabelBackground); } virtual bool IsLabelBackground() const override { return LabelBackground; } From 97b6ff4a8caeb95241b68b6534298276a74a3f30 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 01:52:45 -0400 Subject: [PATCH 04/19] perf: faster relabel for labeling3D_PRED_2021.h --- include/labeling3D_PRED_2021.h | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/include/labeling3D_PRED_2021.h b/include/labeling3D_PRED_2021.h index 2254d74..eed2b36 100644 --- a/include/labeling3D_PRED_2021.h +++ b/include/labeling3D_PRED_2021.h @@ -390,16 +390,12 @@ class PRED_3D : public Labeling3D { // Second scan LabelsSolver::Flatten(); + const unsigned int voxels = h * w * d; int * img_row = reinterpret_cast(img_labels_.data); - for (int s = 0; s < d; s++) { - for (int r = 0; r < h; r++) { - for (int c = 0; c < w; c++) { - img_row[c] = LabelsSolver::GetLabel(img_row[c]); - } - img_row += img_labels_.step[1] / sizeof(int); - } + for (unsigned int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); } - + LabelsSolver::Dealloc(); // Memory deallocation of the labels solver } @@ -553,14 +549,10 @@ class PRED_3D : public Labeling3D { unsigned int h = img_.size.p[1]; unsigned int w = img_.size.p[2]; + const unsigned int voxels = h * w * d; int * img_row = reinterpret_cast(img_labels_.data); - for (unsigned int s = 0; s < d; s++) { - for (unsigned int r = 0; r < h; r++) { - for (unsigned int c = 0; c < w; c++) { - img_row[c] = LabelsSolver::GetLabel(img_row[c]); - } - img_row += img_labels_.step.p[1] / sizeof(int); - } + for (unsigned int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); } } }; From 28e84833c5286d29d5a5480a279ad3803a4303af Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 01:54:34 -0400 Subject: [PATCH 05/19] perf: faster relabel for labeling3D_PREDpp_2021 --- include/labeling3D_PREDpp_2021.h | 38 +++++++++++++------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/include/labeling3D_PREDpp_2021.h b/include/labeling3D_PREDpp_2021.h index 54c068e..ab9e441 100644 --- a/include/labeling3D_PREDpp_2021.h +++ b/include/labeling3D_PREDpp_2021.h @@ -391,15 +391,11 @@ class PREDpp_3D : public Labeling3D { // Second scan LabelsSolver::Flatten(); - int * img_row = reinterpret_cast(img_labels_.data); - for (int s = 0; s < d; s++) { - for (int r = 0; r < h; r++) { - for (int c = 0; c < w; c++) { - img_row[c] = LabelsSolver::GetLabel(img_row[c]); - } - img_row += img_labels_.step[1] / sizeof(int); - } - } + const unsigned int voxels = h * w * d; + int * img_row = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); + } LabelsSolver::Dealloc(); // Memory deallocation of the labels solver @@ -566,21 +562,17 @@ class PREDpp_3D : public Labeling3D { void SecondScan() { - LabelsSolver::Flatten(); + LabelsSolver::Flatten(); + + unsigned int d = img_.size.p[0]; + unsigned int h = img_.size.p[1]; + unsigned int w = img_.size.p[2]; - unsigned int d = img_.size.p[0]; - unsigned int h = img_.size.p[1]; - unsigned int w = img_.size.p[2]; - - int * img_row = reinterpret_cast(img_labels_.data); - for (unsigned int s = 0; s < d; s++) { - for (unsigned int r = 0; r < h; r++) { - for (unsigned int c = 0; c < w; c++) { - img_row[c] = LabelsSolver::GetLabel(img_row[c]); - } - img_row += img_labels_.step.p[1] / sizeof(int); - } - } + const unsigned int voxels = h * w * d; + int * img_row = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); + } } }; From 2ce6db9a8dcf5392789e0e5a3e3b75f499439f21 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 01:55:30 -0400 Subject: [PATCH 06/19] perf: faster labeling3D_SAUF_2021 --- include/labeling3D_SAUF_2021.h | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/include/labeling3D_SAUF_2021.h b/include/labeling3D_SAUF_2021.h index cc129a5..780db45 100644 --- a/include/labeling3D_SAUF_2021.h +++ b/include/labeling3D_SAUF_2021.h @@ -328,14 +328,10 @@ class SAUF_3D : public Labeling3D { // Second scan LabelsSolver::Flatten(); + const unsigned int voxels = h * w * d; int * img_row = reinterpret_cast(img_labels_.data); - for (unsigned int s = 0; s < d; s++) { - for (unsigned int r = 0; r < h; r++) { - for (unsigned int c = 0; c < w; c++) { - img_row[c] = LabelsSolver::GetLabel(img_row[c]); - } - img_row += img_labels_.step[1] / sizeof(int); - } + for (unsigned int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); } LabelsSolver::Dealloc(); // Memory deallocation of the labels solver @@ -446,14 +442,10 @@ class SAUF_3D : public Labeling3D { unsigned int h = img_.size.p[1]; unsigned int w = img_.size.p[2]; + const unsigned int voxels = h * w * d; int * img_row = reinterpret_cast(img_labels_.data); - for (unsigned int s = 0; s < d; s++) { - for (unsigned int r = 0; r < h; r++) { - for (unsigned int c = 0; c < w; c++) { - img_row[c] = LabelsSolver::GetLabel(img_row[c]); - } - img_row += img_labels_.step.p[1] / sizeof(int); - } + for (unsigned int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); } } }; From f40887461faa3555f0bc6aed2a5145e6f9ef4c2e Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 01:56:23 -0400 Subject: [PATCH 07/19] perf: faster labeling3D_SAUFpp_2021 relabeling --- include/labeling3D_SAUFpp_2021.h | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/include/labeling3D_SAUFpp_2021.h b/include/labeling3D_SAUFpp_2021.h index adeee81..02c2608 100644 --- a/include/labeling3D_SAUFpp_2021.h +++ b/include/labeling3D_SAUFpp_2021.h @@ -328,14 +328,10 @@ class SAUFpp_3D : public Labeling3D { // Second scan LabelsSolver::Flatten(); + const unsigned int voxels = h * w * d; int * img_row = reinterpret_cast(img_labels_.data); - for (unsigned int s = 0; s < d; s++) { - for (unsigned int r = 0; r < h; r++) { - for (unsigned int c = 0; c < w; c++) { - img_row[c] = LabelsSolver::GetLabel(img_row[c]); - } - img_row += img_labels_.step[1] / sizeof(int); - } + for (unsigned int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); } LabelsSolver::Dealloc(); // Memory deallocation of the labels solver @@ -461,14 +457,10 @@ class SAUFpp_3D : public Labeling3D { unsigned int h = img_.size.p[1]; unsigned int w = img_.size.p[2]; + const unsigned int voxels = h * w * d; int * img_row = reinterpret_cast(img_labels_.data); - for (unsigned int s = 0; s < d; s++) { - for (unsigned int r = 0; r < h; r++) { - for (unsigned int c = 0; c < w; c++) { - img_row[c] = LabelsSolver::GetLabel(img_row[c]); - } - img_row += img_labels_.step.p[1] / sizeof(int); - } + for (unsigned int i = 0; i < voxels; i++) { + img_row[i] = LabelsSolver::GetLabel(img_row[i]); } } }; From 52b01538991d63658f7287f1beeaf94ac65fbc10 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:01:35 -0400 Subject: [PATCH 08/19] perf: faster relabeling for labeling_grana_2016 --- include/labeling_grana_2016.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/include/labeling_grana_2016.h b/include/labeling_grana_2016.h index ba1846b..4c4d53a 100644 --- a/include/labeling_grana_2016.h +++ b/include/labeling_grana_2016.h @@ -91,12 +91,10 @@ class PRED : public Labeling2D { // Second scan n_labels_ = LabelsSolver::Flatten(); - for (int r_i = 0; r_i < img_labels_.rows; ++r_i) { - unsigned int *b = img_labels_.ptr(r_i); - unsigned int *e = b + img_labels_.cols; - for (; b != e; ++b) { - *b = LabelsSolver::GetLabel(*b); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } LabelsSolver::Dealloc(); @@ -287,12 +285,13 @@ class PRED : public Labeling2D { // Second scan n_labels_ = LabelsSolver::Flatten(); - for (int r_i = 0; r_i < img_labels_.rows; ++r_i) { - unsigned int *b = img_labels_.ptr(r_i); - unsigned int *e = b + img_labels_.cols; - for (; b != e; ++b) { - *b = LabelsSolver::GetLabel(*b); - } + const unsigned int w(img_.cols); + const unsigned int h(img_.rows); + + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } } }; From 001d479c7d1023e504f5a09bcc222745557d1f9d Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:03:17 -0400 Subject: [PATCH 09/19] perf: labeling_he_2008 faster relabel --- include/labeling_he_2008.h | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/include/labeling_he_2008.h b/include/labeling_he_2008.h index e6cbc4e..6044c24 100644 --- a/include/labeling_he_2008.h +++ b/include/labeling_he_2008.h @@ -249,13 +249,10 @@ class RBTS : public Labeling2D n_labels_ = LabelsSolver::Flatten(); // Second scan - for (int r_i = 0; r_i < img_labels_.rows; ++r_i) { - unsigned int* img_labels_row_start = img_labels_.ptr(r_i); - unsigned int* img_labels_row_end = img_labels_row_start + img_labels_.cols; - unsigned int* img_labels_row = img_labels_row_start; - for (int c_i = 0; img_labels_row != img_labels_row_end; ++img_labels_row, ++c_i) { - *img_labels_row = LabelsSolver::GetLabel(*img_labels_row); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } LabelsSolver::Dealloc(); @@ -465,14 +462,13 @@ class RBTS : public Labeling2D { n_labels_ = LabelsSolver::Flatten(); - // Second scan - for (int r_i = 0; r_i < img_labels_.rows; ++r_i) { - unsigned int* img_labels_row_start = img_labels_.ptr(r_i); - unsigned int* img_labels_row_end = img_labels_row_start + img_labels_.cols; - unsigned int* img_labels_row = img_labels_row_start; - for (int c_i = 0; img_labels_row != img_labels_row_end; ++img_labels_row, ++c_i) { - *img_labels_row = LabelsSolver::GetLabel(*img_labels_row); - } + const unsigned int w(img_.cols); + const unsigned int h(img_.rows); + + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } } From b2d79687ccab414fb3543224ada5e3063a26fd89 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:05:30 -0400 Subject: [PATCH 10/19] perf: faster labeling_he_2014 relabel --- include/labeling_he_2014.h | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/include/labeling_he_2014.h b/include/labeling_he_2014.h index 3fd46ec..51e12d8 100644 --- a/include/labeling_he_2014.h +++ b/include/labeling_he_2014.h @@ -236,11 +236,10 @@ class CTB : public Labeling2D { n_labels_ = LabelsSolver::MemFlatten(); - // Second scan - for (int r_i = 0; r_i < img_labels.rows; ++r_i) { - for (int c_i = 0; c_i < img_labels.cols; ++c_i) { - img_labels(r_i,c_i) = LabelsSolver::MemGetLabel(img_labels(r_i, c_i)); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); } // Store total accesses in the output vector 'accesses' @@ -363,18 +362,17 @@ class CTB : public Labeling2D { #undef CONDITION_N4 #undef CONDITION_N5 } - void SecondScan() + void SecondScan() { n_labels_ = LabelsSolver::Flatten(); - // Second scan - for (int r_i = 0; r_i < img_labels_.rows; ++r_i) { - unsigned int *img_labels_row_start = img_labels_.ptr(r_i); - unsigned int *img_labels_row_end = img_labels_row_start + img_labels_.cols; - unsigned int *img_labels_row = img_labels_row_start; - for (int c_i = 0; img_labels_row != img_labels_row_end; ++img_labels_row, ++c_i) { - *img_labels_row = LabelsSolver::GetLabel(*img_labels_row); - } + const unsigned int w(img_.cols); + const unsigned int h(img_.rows); + + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } } }; From 883ddabb575343d0aa7d7ea4444a9436b91998df Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:07:39 -0400 Subject: [PATCH 11/19] perf: faster relabel for labeling_PREDpp_2021 --- include/labeling_PREDpp_2021.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/include/labeling_PREDpp_2021.h b/include/labeling_PREDpp_2021.h index e4613df..78358c2 100644 --- a/include/labeling_PREDpp_2021.h +++ b/include/labeling_PREDpp_2021.h @@ -110,12 +110,10 @@ class PREDpp : public Labeling2D { // Second scan n_labels_ = LabelsSolver::Flatten(); - for (int r_i = 0; r_i < img_labels_.rows; ++r_i) { - unsigned int *b = img_labels_.ptr(r_i); - unsigned int *e = b + img_labels_.cols; - for (; b != e; ++b) { - *b = LabelsSolver::GetLabel(*b); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } LabelsSolver::Dealloc(); @@ -248,12 +246,13 @@ class PREDpp : public Labeling2D { // Second scan n_labels_ = LabelsSolver::Flatten(); - for (int r_i = 0; r_i < img_labels_.rows; ++r_i) { - unsigned int *b = img_labels_.ptr(r_i); - unsigned int *e = b + img_labels_.cols; - for (; b != e; ++b) { - *b = LabelsSolver::GetLabel(*b); - } + const unsigned int w(img_.cols); + const unsigned int h(img_.rows); + + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } } }; From c6ee0fb94ea0ba1398cf2a0d15920be2d437a90b Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:09:22 -0400 Subject: [PATCH 12/19] perf: faster labeling_sauf_4c relabeling --- include/labeling_sauf_4c.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/include/labeling_sauf_4c.h b/include/labeling_sauf_4c.h index 5db18bf..69634a9 100644 --- a/include/labeling_sauf_4c.h +++ b/include/labeling_sauf_4c.h @@ -150,10 +150,10 @@ class SAUF4C : public Labeling2D { // Second scan n_labels_ = LabelsSolver::MemFlatten(); - for (int r = 0; r < h; ++r) { - for (int c = 0; c < w; ++c) { - img_labels(r, c) = LabelsSolver::MemGetLabel(img_labels(r, c)); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); } // Store total accesses in the output vector 'accesses' @@ -263,12 +263,13 @@ class SAUF4C : public Labeling2D { { n_labels_ = LabelsSolver::Flatten(); - for (int r = 0; r < img_labels_.rows; ++r) { - unsigned * img_row_start = img_labels_.ptr(r); - unsigned * const img_row_end = img_row_start + img_labels_.cols; - for (; img_row_start != img_row_end; ++img_row_start) { - *img_row_start = LabelsSolver::GetLabel(*img_row_start); - } + const unsigned int w(img_.cols); + const unsigned int h(img_.rows); + + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } } }; From 220246ec93fcfda7941892e7beeb16c54e8d4077 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:10:29 -0400 Subject: [PATCH 13/19] perf: faster labeling_sauf_background relabel --- include/labeling_sauf_background.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/include/labeling_sauf_background.h b/include/labeling_sauf_background.h index 6ac5ea9..e2c9278 100644 --- a/include/labeling_sauf_background.h +++ b/include/labeling_sauf_background.h @@ -168,10 +168,10 @@ class SAUF_BG : public Labeling2D { // Second scan n_labels_ = LabelsSolver::MemFlatten(); - for (int r = 0; r < h; ++r) { - for (int c = 0; c < w; ++c) { - img_labels(r, c) = LabelsSolver::MemGetLabel(img_labels(r, c)); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); } // Store total accesses in the output vector 'accesses' @@ -296,12 +296,13 @@ class SAUF_BG : public Labeling2D { { n_labels_ = LabelsSolver::Flatten(); - for (int r = 0; r < img_labels_.rows; ++r) { - unsigned * img_row_start = img_labels_.ptr(r); - unsigned * const img_row_end = img_row_start + img_labels_.cols; - for (; img_row_start != img_row_end; ++img_row_start) { - *img_row_start = LabelsSolver::GetLabel(*img_row_start); - } + const unsigned int w(img_.cols); + const unsigned int h(img_.rows); + + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } } }; From db302d8f235f24b04df09fa18df80eec18dc0160 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:11:50 -0400 Subject: [PATCH 14/19] perf: faster relabel for labeling_wu_2009 --- include/labeling_wu_2009.h | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/include/labeling_wu_2009.h b/include/labeling_wu_2009.h index 766dcce..fbca338 100644 --- a/include/labeling_wu_2009.h +++ b/include/labeling_wu_2009.h @@ -158,12 +158,12 @@ class SAUF : public Labeling2D { // Second scan n_labels_ = LabelsSolver::MemFlatten(); - for (int r = 0; r < h; ++r) { - for (int c = 0; c < w; ++c) { - img_labels(r, c) = LabelsSolver::MemGetLabel(img_labels(r, c)); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); } - + // Store total accesses in the output vector 'accesses' accesses = std::vector((int)MD_SIZE, 0); @@ -276,12 +276,13 @@ class SAUF : public Labeling2D { { n_labels_ = LabelsSolver::Flatten(); - for (int r = 0; r < img_labels_.rows; ++r) { - unsigned * img_row_start = img_labels_.ptr(r); - unsigned * const img_row_end = img_row_start + img_labels_.cols; - for (; img_row_start != img_row_end; ++img_row_start) { - *img_row_start = LabelsSolver::GetLabel(*img_row_start); - } + const unsigned int w(img_.cols); + const unsigned int h(img_.rows); + + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); } } }; From 904e3820e87dd4478c74ca4b686dc953de71f27d Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:31:12 -0400 Subject: [PATCH 15/19] fix: don't convert MemGetLabel for labeling_wu_2009 --- include/labeling_wu_2009.h | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/include/labeling_wu_2009.h b/include/labeling_wu_2009.h index fbca338..f4e7de9 100644 --- a/include/labeling_wu_2009.h +++ b/include/labeling_wu_2009.h @@ -66,12 +66,10 @@ class SAUF : public Labeling2D { // Second scan n_labels_ = LabelsSolver::Flatten(); - for (int r = 0; r < img_labels_.rows; ++r) { - unsigned * img_row_start = img_labels_.ptr(r); - unsigned * const img_row_end = img_row_start + img_labels_.cols; - for (; img_row_start != img_row_end; ++img_row_start) { - *img_row_start = LabelsSolver::GetLabel(*img_row_start); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } LabelsSolver::Dealloc(); // Memory deallocation of the labels solver @@ -158,12 +156,12 @@ class SAUF : public Labeling2D { // Second scan n_labels_ = LabelsSolver::MemFlatten(); - const unsigned int pixels = h * w; - unsigned int * img_data = reinterpret_cast(img_labels_.data); - for (unsigned int i = 0; i < pixels; i++) { - img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); + for (int r = 0; r < h; ++r) { + for (int c = 0; c < w; ++c) { + img_labels(r, c) = LabelsSolver::MemGetLabel(img_labels(r, c)); + } } - + // Store total accesses in the output vector 'accesses' accesses = std::vector((int)MD_SIZE, 0); @@ -276,13 +274,13 @@ class SAUF : public Labeling2D { { n_labels_ = LabelsSolver::Flatten(); - const unsigned int w(img_.cols); - const unsigned int h(img_.rows); + const unsigned int h(img_labels_.cols); + const unsigned int w(img_labels_.rows); const unsigned int pixels = h * w; unsigned int * img_data = reinterpret_cast(img_labels_.data); for (unsigned int i = 0; i < pixels; i++) { - img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } } }; From 6505aad65392650a278e8200845a08ff24907df3 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:37:35 -0400 Subject: [PATCH 16/19] fix: use original logic for MemGetLabel for labeling_he_2014 --- include/labeling_he_2014.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/include/labeling_he_2014.h b/include/labeling_he_2014.h index 51e12d8..952a23e 100644 --- a/include/labeling_he_2014.h +++ b/include/labeling_he_2014.h @@ -122,13 +122,10 @@ class CTB : public Labeling2D { n_labels_ = LabelsSolver::Flatten(); // Second scan - for (int r_i = 0; r_i < img_labels_.rows; ++r_i) { - unsigned int *img_labels_row_start = img_labels_.ptr(r_i); - unsigned int *img_labels_row_end = img_labels_row_start + img_labels_.cols; - unsigned int *img_labels_row = img_labels_row_start; - for (int c_i = 0; img_labels_row != img_labels_row_end; ++img_labels_row, ++c_i) { - *img_labels_row = LabelsSolver::GetLabel(*img_labels_row); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } LabelsSolver::Dealloc(); @@ -236,12 +233,13 @@ class CTB : public Labeling2D { n_labels_ = LabelsSolver::MemFlatten(); - const unsigned int pixels = h * w; - unsigned int * img_data = reinterpret_cast(img_labels_.data); - for (unsigned int i = 0; i < pixels; i++) { - img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); + // Second scan + for (int r_i = 0; r_i < img_labels.rows; ++r_i) { + for (int c_i = 0; c_i < img_labels.cols; ++c_i) { + img_labels(r_i,c_i) = LabelsSolver::MemGetLabel(img_labels(r_i, c_i)); + } } - + // Store total accesses in the output vector 'accesses' accesses = std::vector((int)MD_SIZE, 0); From 23e9fd9ac5f43dc33c7d9d62708903d79fa66982 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 20 Jun 2026 02:39:00 -0400 Subject: [PATCH 17/19] fix: use original MemGetLabel logic for labeling_sauf_background --- include/labeling_sauf_background.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/include/labeling_sauf_background.h b/include/labeling_sauf_background.h index e2c9278..2d67852 100644 --- a/include/labeling_sauf_background.h +++ b/include/labeling_sauf_background.h @@ -69,12 +69,10 @@ class SAUF_BG : public Labeling2D { // Second scan n_labels_ = LabelsSolver::Flatten(); - for (int r = 0; r < img_labels_.rows; ++r) { - unsigned* img_row_start = img_labels_.ptr(r); - unsigned* const img_row_end = img_row_start + img_labels_.cols; - for (; img_row_start != img_row_end; ++img_row_start) { - *img_row_start = LabelsSolver::GetLabel(*img_row_start); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } LabelsSolver::Dealloc(); // Memory deallocation of the labels solver @@ -168,10 +166,10 @@ class SAUF_BG : public Labeling2D { // Second scan n_labels_ = LabelsSolver::MemFlatten(); - const unsigned int pixels = h * w; - unsigned int * img_data = reinterpret_cast(img_labels_.data); - for (unsigned int i = 0; i < pixels; i++) { - img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); + for (int r_i = 0; r_i < img_labels.rows; ++r_i) { + for (int c_i = 0; c_i < img_labels.cols; ++c_i) { + img_labels(r_i,c_i) = LabelsSolver::MemGetLabel(img_labels(r_i, c_i)); + } } // Store total accesses in the output vector 'accesses' From 0862d7bb94459bf3f9dfea127c22df84d89c415d Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Mon, 22 Jun 2026 11:48:26 -0400 Subject: [PATCH 18/19] perf: faster relabeling for sauf4c --- include/labeling_sauf_4c.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/include/labeling_sauf_4c.h b/include/labeling_sauf_4c.h index 69634a9..0070ffa 100644 --- a/include/labeling_sauf_4c.h +++ b/include/labeling_sauf_4c.h @@ -61,12 +61,10 @@ class SAUF4C : public Labeling2D { // Second scan n_labels_ = LabelsSolver::Flatten(); - for (int r = 0; r < img_labels_.rows; ++r) { - unsigned * img_row_start = img_labels_.ptr(r); - unsigned * const img_row_end = img_row_start + img_labels_.cols; - for (; img_row_start != img_row_end; ++img_row_start) { - *img_row_start = LabelsSolver::GetLabel(*img_row_start); - } + const unsigned int pixels = h * w; + unsigned int * img_data = reinterpret_cast(img_labels_.data); + for (unsigned int i = 0; i < pixels; i++) { + img_data[i] = LabelsSolver::GetLabel(img_data[i]); } LabelsSolver::Dealloc(); // Memory deallocation of the labels solver From e9c1829bf661c00931b17e1866d61c924be3e623 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Mon, 22 Jun 2026 11:48:45 -0400 Subject: [PATCH 19/19] revert: refactoring MemGetLabel in this way causes errors --- include/labeling_sauf_4c.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/labeling_sauf_4c.h b/include/labeling_sauf_4c.h index 0070ffa..88f6820 100644 --- a/include/labeling_sauf_4c.h +++ b/include/labeling_sauf_4c.h @@ -148,10 +148,10 @@ class SAUF4C : public Labeling2D { // Second scan n_labels_ = LabelsSolver::MemFlatten(); - const unsigned int pixels = h * w; - unsigned int * img_data = reinterpret_cast(img_labels_.data); - for (unsigned int i = 0; i < pixels; i++) { - img_data[i] = LabelsSolver::MemGetLabel(img_data[i]); + for (int r = 0; r < h; ++r) { + for (int c = 0; c < w; ++c) { + img_labels(r, c) = LabelsSolver::MemGetLabel(img_labels(r, c)); + } } // Store total accesses in the output vector 'accesses'