Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
remove marker form thinningIteration and remove "if" from forEach
  • Loading branch information
ansleliu authored May 31, 2024
commit e881f951ef39ada490a181988c8463b0a03b1a25
81 changes: 25 additions & 56 deletions modules/ximgproc/src/thinning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,69 +92,35 @@ static uint8_t lut_guo_iter1[] = {
1, 1, 1, 1};

// Applies a thinning iteration to a binary image
static void thinningIteration(Mat img, int iter, int thinningType){
Mat marker = Mat::zeros(img.size(), CV_8UC1);
static void thinningIteration(Mat &img, Mat &marker, const uint8_t* const lut) {
int rows = img.rows;
int cols = img.cols;
marker.col(0).setTo(1);
marker.col(cols - 1).setTo(1);
marker.row(0).setTo(1);
marker.row(rows - 1).setTo(1);

if(thinningType == THINNING_ZHANGSUEN){
marker.forEach<uchar>([=](uchar& value, const int postion[]) {
int i = postion[0];
int j = postion[1];
if (i == 0 || j == 0 || i == rows - 1 || j == cols - 1)
return;

auto ptr = img.ptr(i, j); // p1

uchar p2 = ptr[-cols];
uchar p3 = ptr[-cols + 1];
uchar p4 = ptr[1];
uchar p5 = ptr[cols + 1];
uchar p6 = ptr[cols];
uchar p7 = ptr[cols - 1];
uchar p8 = ptr[-1];
uchar p9 = ptr[-cols - 1];

int neighbors = p9 | (p2 << 1) | (p3 << 2) | (p4 << 3) | (p5 << 4) | (p6 << 5) | (p7 << 6) | (p8 << 7);

if (iter == 0)
value = lut_zhang_iter0[neighbors];
else
value = lut_zhang_iter1[neighbors];
});
}
if(thinningType == THINNING_GUOHALL){
marker.forEach<uchar>([=](uchar& value, const int postion[]) {
int i = postion[0];
int j = postion[1];
if (i == 0 || j == 0 || i == rows - 1 || j == cols - 1)
return;

auto ptr = img.ptr(i, j); // p1

uchar p2 = ptr[-cols];
uchar p3 = ptr[-cols + 1];
uchar p4 = ptr[1];
uchar p5 = ptr[cols + 1];
uchar p6 = ptr[cols];
uchar p7 = ptr[cols - 1];
uchar p8 = ptr[-1];
uchar p9 = ptr[-cols - 1];

int neighbors = p9 | (p2 << 1) | (p3 << 2) | (p4 << 3) | (p5 << 4) | (p6 << 5) | (p7 << 6) | (p8 << 7);

if (iter == 0)
value = lut_guo_iter0[neighbors];
else
value = lut_guo_iter1[neighbors];
});
}
marker.forEach<uchar>([=](uchar& value, const int postion[]) {
int i = postion[0];
int j = postion[1];
if (i == 0 || j == 0 || i == rows - 1 || j == cols - 1) { return; }

auto ptr = img.ptr(i, j); // p1
uchar p2 = ptr[-cols];
uchar p3 = ptr[-cols + 1];
uchar p4 = ptr[1];
uchar p5 = ptr[cols + 1];
uchar p6 = ptr[cols];
uchar p7 = ptr[cols - 1];
uchar p8 = ptr[-1];
uchar p9 = ptr[-cols - 1];

int neighbors = p9 | (p2 << 1) | (p3 << 2) | (p4 << 3) | (p5 << 4) | (p6 << 5) | (p7 << 6) | (p8 << 7);
value = lut[neighbors];
});

img &= marker;
marker.setTo(0);
}

// Apply the thinning procedure to a given image
Expand All @@ -164,9 +130,12 @@ void thinning(InputArray input, OutputArray output, int thinningType){
// Enforce the range of the input image to be in between 0 - 255
processed /= 255;
Mat prev = processed.clone();
Mat marker = Mat::zeros(processed.size(), CV_8UC1);
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, 0, thinningType);
thinningIteration(processed, 1, thinningType);
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);
Expand Down