@@ -92,7 +92,7 @@ static uint8_t lut_guo_iter1[] = {
9292 1 , 1 , 1 , 1 };
9393
9494// 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 ) {
9696 int rows = img.rows ;
9797 int cols = img.cols ;
9898
@@ -113,12 +113,20 @@ static void thinningIteration(Mat &img, Mat &marker, const uint8_t* const lut) {
113113 uchar p9 = imgRow[j - cols - 1 ] != 0 ;
114114
115115 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+
117124 }
118125 }
119126 }
120127 });
121128
129+ // Bitwise AND and reset marker for the next iteration
122130 img &= marker;
123131}
124132
@@ -138,15 +146,18 @@ void thinning(InputArray input, OutputArray output, int thinningType){
138146 const auto lutIter0 = (thinningType == THINNING_GUOHALL) ? lut_guo_iter0 : lut_zhang_iter0;
139147 const auto lutIter1 = (thinningType == THINNING_GUOHALL) ? lut_guo_iter1 : lut_zhang_iter1;
140148 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 ;
146158 } while (true );
147159
148- processed *= 255 ;
149- output.assign (processed);
160+ output.assign (processed * 255 );
150161}
151162
152163} // namespace ximgproc
0 commit comments