Skip to content

Commit

Permalink
Update thinning.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
sturkmen72 authored Oct 3, 2024
1 parent 66cf9b3 commit c8ca52b
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions modules/ximgproc/src/thinning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -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
Expand Down

0 comments on commit c8ca52b

Please sign in to comment.