Skip to content

Commit d131e7a

Browse files
authored
Merge pull request #3743 from vrabaud:thinning
Misc thinning fixes. #3743 - edges could be modified - 2 sets of iterations were always done even if the first set did not return any change - countNonZero is slow compared to hasNonZero ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name.
1 parent baaeb68 commit d131e7a

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

modules/ximgproc/src/thinning.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ static void thinningIteration(Mat img, int iter, int thinningType){
9696
Mat marker = Mat::zeros(img.size(), CV_8UC1);
9797
int rows = img.rows;
9898
int cols = img.cols;
99+
marker.col(0).setTo(1);
100+
marker.col(cols - 1).setTo(1);
101+
marker.row(0).setTo(1);
102+
marker.row(rows - 1).setTo(1);
99103

100104
if(thinningType == THINNING_ZHANGSUEN){
101105
marker.forEach<uchar>([=](uchar& value, const int postion[]) {
@@ -133,6 +137,7 @@ static void thinningIteration(Mat img, int iter, int thinningType){
133137
//int m1 = iter == 0 ? (p2 * p4 * p6) : (p2 * p4 * p8);
134138
//int m2 = iter == 0 ? (p4 * p6 * p8) : (p2 * p6 * p8);
135139
//if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0) value = 0;
140+
// else value = 1;
136141
});
137142
}
138143
if(thinningType == THINNING_GUOHALL){
@@ -170,6 +175,7 @@ static void thinningIteration(Mat img, int iter, int thinningType){
170175
//int N = N1 < N2 ? N1 : N2;
171176
//int m = iter == 0 ? ((p6 | p7 | (!p9)) & p8) : ((p2 | p3 | (!p5)) & p4);
172177
//if ((C == 1) && ((N >= 2) && ((N <= 3)) & (m == 0))) value = 0;
178+
// else value = 1;
173179
});
174180
}
175181

@@ -183,16 +189,17 @@ void thinning(InputArray input, OutputArray output, int thinningType){
183189
// Enforce the range of the input image to be in between 0 - 255
184190
processed /= 255;
185191

186-
Mat prev = Mat::zeros(processed.size(), CV_8UC1);
192+
Mat prev = processed.clone();
187193
Mat diff;
188194

189195
do {
190196
thinningIteration(processed, 0, thinningType);
191197
thinningIteration(processed, 1, thinningType);
192198
absdiff(processed, prev, diff);
199+
if (!hasNonZero(diff)) break;
193200
processed.copyTo(prev);
194201
}
195-
while (countNonZero(diff) > 0);
202+
while (true);
196203

197204
processed *= 255;
198205

modules/ximgproc/test/test_thinning.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
namespace opencv_test { namespace {
88

9-
static int createTestImage(Mat& src)
9+
static int createTestImage(Mat1b& src)
1010
{
11-
src = Mat::zeros(Size(256, 256), CV_8UC1);
11+
src = Mat1b::zeros(Size(256, 256));
12+
// Create a corner point that should not be affected.
13+
src(0, 0) = 255;
14+
1215
for (int x = 50; x < src.cols - 50; x += 50)
1316
{
1417
cv::circle(src, Point(x, x/2), 30 + x/2, Scalar(255), 5);
@@ -20,13 +23,14 @@ static int createTestImage(Mat& src)
2023

2124
TEST(ximgproc_Thinning, simple_ZHANGSUEN)
2225
{
23-
Mat src;
26+
Mat1b src;
2427
int src_pixels = createTestImage(src);
2528

26-
Mat dst;
29+
Mat1b dst;
2730
thinning(src, dst, THINNING_ZHANGSUEN);
2831
int dst_pixels = countNonZero(dst);
2932
EXPECT_LE(dst_pixels, src_pixels);
33+
EXPECT_EQ(dst(0, 0), 255);
3034

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

3640
TEST(ximgproc_Thinning, simple_GUOHALL)
3741
{
38-
Mat src;
42+
Mat1b src;
3943
int src_pixels = createTestImage(src);
4044

41-
Mat dst;
45+
Mat1b dst;
4246
thinning(src, dst, THINNING_GUOHALL);
4347
int dst_pixels = countNonZero(dst);
4448
EXPECT_LE(dst_pixels, src_pixels);
49+
EXPECT_EQ(dst(0, 0), 255);
4550

4651
#if 0
4752
imshow("src", src); imshow("dst", dst); waitKey();

0 commit comments

Comments
 (0)