Thinning contour lines in a binary image

I have a binary image with contour lines and I need to clear every contour line of all unnecessary pixels, leaving a minimally connected line.

Can someone give me a source, sample code or additional information on this issue and where to look for help?

+1
source share
4 answers

If you are looking for python implementations take a look at scikit-image .

One of their examples is, in fact, your precedent.

Alternatively, if you want to stick with the โ€œstraightโ€ scipy, you can do this using the following erosion and extensions using scipy.ndimage . (As @AxezDNyde mentions.)

Edit: Fixed links.

+2
source

There is actually an algorithm for the thinning algorithm Zhang-Suen. You can find its code here: http://rosettacode.org/wiki/Zhang-Suen_thinning_algorithm

I also wrote a vectorized version of this in Python, which is about 10 times faster than this code. Here is the code:

 def neighbours_vec(image): return image[2:,1:-1], image[2:,2:], image[1:-1,2:], image[:-2,2:], image[:-2,1:-1], image[:-2,:-2], image[1:-1,:-2], image[2:,:-2] def transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9): return ((P3-P2) > 0).astype(int) + ((P4-P3) > 0).astype(int) + \ ((P5-P4) > 0).astype(int) + ((P6-P5) > 0).astype(int) + \ ((P7-P6) > 0).astype(int) + ((P8-P7) > 0).astype(int) + \ ((P9-P8) > 0).astype(int) + ((P2-P9) > 0).astype(int) def zhangSuen_vec(image, iterations): for iter in range (1, iterations): print iter # step 1 P2,P3,P4,P5,P6,P7,P8,P9 = neighbours_vec(image) condition0 = image[1:-1,1:-1] condition4 = P4*P6*P8 condition3 = P2*P4*P6 condition2 = transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9) == 1 condition1 = (2 <= P2+P3+P4+P5+P6+P7+P8+P9) * (P2+P3+P4+P5+P6+P7+P8+P9 <= 6) cond = (condition0 == 1) * (condition4 == 0) * (condition3 == 0) * (condition2 == 1) * (condition1 == 1) changing1 = numpy.where(cond == 1) image[changing1[0]+1,changing1[1]+1] = 0 # step 2 P2,P3,P4,P5,P6,P7,P8,P9 = neighbours_vec(image) condition0 = image[1:-1,1:-1] condition4 = P2*P6*P8 condition3 = P2*P4*P8 condition2 = transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9) == 1 condition1 = (2 <= P2+P3+P4+P5+P6+P7+P8+P9) * (P2+P3+P4+P5+P6+P7+P8+P9 <= 6) cond = (condition0 == 1) * (condition4 == 0) * (condition3 == 0) * (condition2 == 1) * (condition1 == 1) changing2 = numpy.where(cond == 1) image[changing2[0]+1,changing2[1]+1] = 0 return image 
+2
source

The combination of erosion and dilatation (and vice versa) in a binary image can help get rid of salt pepper like noise, leaving small lines intact. Key words: "ranking filters" and "morphological filters".

+1
source

PyPi has a package called thinning that you can simply install with pip. It implements Guo and Hall thinning algorithm for bumpy grayscale opencv arrays / images.

0
source

All Articles