Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc thinning fixes. #3743

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 9 additions & 2 deletions modules/ximgproc/src/thinning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ static void thinningIteration(Mat img, int iter, int thinningType){
Mat marker = Mat::zeros(img.size(), CV_8UC1);
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[]) {
Expand Down Expand Up @@ -133,6 +137,7 @@ static void thinningIteration(Mat img, int iter, int thinningType){
//int m1 = iter == 0 ? (p2 * p4 * p6) : (p2 * p4 * p8);
//int m2 = iter == 0 ? (p4 * p6 * p8) : (p2 * p6 * p8);
//if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0) value = 0;
// else value = 1;
});
}
if(thinningType == THINNING_GUOHALL){
Expand Down Expand Up @@ -170,6 +175,7 @@ static void thinningIteration(Mat img, int iter, int thinningType){
//int N = N1 < N2 ? N1 : N2;
//int m = iter == 0 ? ((p6 | p7 | (!p9)) & p8) : ((p2 | p3 | (!p5)) & p4);
//if ((C == 1) && ((N >= 2) && ((N <= 3)) & (m == 0))) value = 0;
// else value = 1;
});
}

Expand All @@ -183,16 +189,17 @@ 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 = Mat::zeros(processed.size(), CV_8UC1);
Mat prev = processed.clone();
Mat diff;

do {
thinningIteration(processed, 0, thinningType);
thinningIteration(processed, 1, thinningType);
absdiff(processed, prev, diff);
if (!hasNonZero(diff)) break;
processed.copyTo(prev);
}
while (countNonZero(diff) > 0);
while (true);

processed *= 255;

Expand Down
17 changes: 11 additions & 6 deletions modules/ximgproc/test/test_thinning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

namespace opencv_test { namespace {

static int createTestImage(Mat& src)
static int createTestImage(Mat1b& src)
{
src = Mat::zeros(Size(256, 256), CV_8UC1);
src = Mat1b::zeros(Size(256, 256));
// Create a corner point that should not be affected.
src(0, 0) = 255;

for (int x = 50; x < src.cols - 50; x += 50)
{
cv::circle(src, Point(x, x/2), 30 + x/2, Scalar(255), 5);
Expand All @@ -20,13 +23,14 @@ static int createTestImage(Mat& src)

TEST(ximgproc_Thinning, simple_ZHANGSUEN)
{
Mat src;
Mat1b src;
int src_pixels = createTestImage(src);

Mat dst;
Mat1b dst;
thinning(src, dst, THINNING_ZHANGSUEN);
int dst_pixels = countNonZero(dst);
EXPECT_LE(dst_pixels, src_pixels);
EXPECT_EQ(dst(0, 0), 255);

#if 0
imshow("src", src); imshow("dst", dst); waitKey();
Expand All @@ -35,13 +39,14 @@ TEST(ximgproc_Thinning, simple_ZHANGSUEN)

TEST(ximgproc_Thinning, simple_GUOHALL)
{
Mat src;
Mat1b src;
int src_pixels = createTestImage(src);

Mat dst;
Mat1b dst;
thinning(src, dst, THINNING_GUOHALL);
int dst_pixels = countNonZero(dst);
EXPECT_LE(dst_pixels, src_pixels);
EXPECT_EQ(dst(0, 0), 255);

#if 0
imshow("src", src); imshow("dst", dst); waitKey();
Expand Down
Loading