From c8ca52b764381171bd5a832b7ff50243849a0668 Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Thu, 3 Oct 2024 19:59:01 +0300 Subject: [PATCH] Update thinning.cpp --- modules/ximgproc/src/thinning.cpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/modules/ximgproc/src/thinning.cpp b/modules/ximgproc/src/thinning.cpp index b3a7dc15eb..ff92f36ec4 100644 --- a/modules/ximgproc/src/thinning.cpp +++ b/modules/ximgproc/src/thinning.cpp @@ -92,7 +92,7 @@ static uint8_t lut_guo_iter1[] = { 1, 1, 1, 1}; // Applies a thinning iteration to a binary image -static void thinningIteration(Mat &img, Mat &marker, const uint8_t* const lut) { +static void thinningIteration(Mat &img, Mat &marker, const uint8_t* const lut, bool &changed) { int rows = img.rows; int cols = img.cols; @@ -113,12 +113,20 @@ static void thinningIteration(Mat &img, Mat &marker, const uint8_t* const lut) { uchar p9 = imgRow[j - cols - 1] != 0; int neighbors = p9 | (p2 << 1) | (p3 << 2) | (p4 << 3) | (p5 << 4) | (p6 << 5) | (p7 << 6) | (p8 << 7); - markerRow[j] = lut[neighbors]; + uchar lut_value = lut[neighbors]; + + if (lut_value == 0) + { + markerRow[j] = lut_value; + changed = true; + } + } } } }); + // Bitwise AND and reset marker for the next iteration img &= marker; } @@ -138,15 +146,18 @@ void thinning(InputArray input, OutputArray output, int thinningType){ const auto lutIter0 = (thinningType == THINNING_GUOHALL) ? lut_guo_iter0 : lut_zhang_iter0; const auto lutIter1 = (thinningType == THINNING_GUOHALL) ? lut_guo_iter1 : lut_zhang_iter1; do { - thinningIteration(processed, marker, lutIter0); - thinningIteration(processed, marker, lutIter1); - const auto res = cv::norm(processed, prev, cv::NORM_L1); - if (res <= 0) { break; } - processed.copyTo(prev); + bool changed0 = false; + bool changed1 = false; + thinningIteration(processed, marker, lutIter0, changed0); + thinningIteration(processed, marker, lutIter1, changed1); + + if (changed0 | changed1) + processed.copyTo(prev); + else + break; } while (true); - processed *= 255; - output.assign(processed); + output.assign(processed * 255); } } //namespace ximgproc