@@ -92,7 +92,7 @@ static uint8_t lut_guo_iter1[] = {
92
92
1 , 1 , 1 , 1 };
93
93
94
94
// Applies a thinning iteration to a binary image
95
- static void thinningIteration (Mat &img, Mat &marker, const uint8_t * const lut) {
95
+ static void thinningIteration (Mat &img, Mat &marker, const uint8_t * const lut, bool &changed ) {
96
96
int rows = img.rows ;
97
97
int cols = img.cols ;
98
98
@@ -113,12 +113,20 @@ static void thinningIteration(Mat &img, Mat &marker, const uint8_t* const lut) {
113
113
uchar p9 = imgRow[j - cols - 1 ] != 0 ;
114
114
115
115
int neighbors = p9 | (p2 << 1 ) | (p3 << 2 ) | (p4 << 3 ) | (p5 << 4 ) | (p6 << 5 ) | (p7 << 6 ) | (p8 << 7 );
116
- markerRow[j] = lut[neighbors];
116
+ uchar lut_value = lut[neighbors];
117
+
118
+ if (lut_value == 0 )
119
+ {
120
+ markerRow[j] = lut_value;
121
+ changed = true ;
122
+ }
123
+
117
124
}
118
125
}
119
126
}
120
127
});
121
128
129
+ // Bitwise AND and reset marker for the next iteration
122
130
img &= marker;
123
131
}
124
132
@@ -138,15 +146,18 @@ void thinning(InputArray input, OutputArray output, int thinningType){
138
146
const auto lutIter0 = (thinningType == THINNING_GUOHALL) ? lut_guo_iter0 : lut_zhang_iter0;
139
147
const auto lutIter1 = (thinningType == THINNING_GUOHALL) ? lut_guo_iter1 : lut_zhang_iter1;
140
148
do {
141
- thinningIteration (processed, marker, lutIter0);
142
- thinningIteration (processed, marker, lutIter1);
143
- const auto res = cv::norm (processed, prev, cv::NORM_L1);
144
- if (res <= 0 ) { break ; }
145
- processed.copyTo (prev);
149
+ bool changed0 = false ;
150
+ bool changed1 = false ;
151
+ thinningIteration (processed, marker, lutIter0, changed0);
152
+ thinningIteration (processed, marker, lutIter1, changed1);
153
+
154
+ if (changed0 | changed1)
155
+ processed.copyTo (prev);
156
+ else
157
+ break ;
146
158
} while (true );
147
159
148
- processed *= 255 ;
149
- output.assign (processed);
160
+ output.assign (processed * 255 );
150
161
}
151
162
152
163
} // namespace ximgproc
0 commit comments