I am working on the centerline extraction, I have tried skeletonize, RivGraph and several other methods but could not get a good centerline. First the line on the ends are not following the center and it is also shorter than the mask itself.

Here is the code that I have tried:

# Thresholding the mask values to binary
mask[mask < 200] = 0
mask[mask > 200] = 255

# Convert the grayscale image to binary image
img = img_as_ubyte(mask)

# Perform thinning
thinned = skeletonize(img, method= 'lee')

# Get the x, y coordinates of non-zero (white) pixels
y, x = np.nonzero(thinned)

I = imageio.v2.imread("mask.jpg")

Imask = np.zeros((I.shape[0], I.shape[1]), dtype=bool)

Imask[I[:,:] != 255] = True   

coords, widths = ru.mask_to_centerline(Imask, 'ew')

plt.plot(coords[:,0], coords[:,1])

Could anybody give me some hint?

mask.jpg result.jpg

1 Answer 1


Since your first tag is grass-gis, I suggest you try the GRASS module v.centerline. You'll have to perform the following steps:

  1. convert the raster to a polygon vector (r.to.vect)
  2. extract the boundary of the polygon "mask" as a line (v.to.lines)
  3. Then try v.centerline on that line feature

If you encounter any problems with the above, post again with your GRASS specific code, and we'll try to help.

  • Hi Micha, very much appreciated it. I will try the way you described. Right now I am trying all the methods possible in python to work this out.
    – H Tai
    Aug 30 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.