def skeletonize_image(image, method=None, dilation=None, binarization=None,
invert=False):
"""Extract a 1 pixel wide representation of image by morphologically
thinning the white contiguous blobs (connected components).
If image is not black and white, a binarization process is applied
according to binarization, which can be 'sauvola', 'isodata', 'otsu',
'li' (default, ref: binarize()).
A process of dilation can also be applied by specifying the number
of pixels in dilate prior to extracting the skeleton.
The method for skeletonization can be 'medial', '3d', 'regular', or a
'combined' version of the three. Defaults to 'regular'.
A 'thin' operator is also available. For reference,
see http://scikit-image.org/docs/dev/auto_examples/edges/plot_skeleton.html
"""
# if image is all one single color, return it
if len(np.unique(image)) == 1:
return image
# scikit-image needs only 0's and 1's
mono_image = binarize_image(image, method=binarization) / 255
if invert:
mono_image = invert_image(mono_image)
if dilation:
dilation = (2 * dilation) + 1
dilation_kernel = np.ones((dilation, dilation), np.uint8)
mono_image = cv2.morphologyEx(mono_image, cv2.MORPH_DILATE,
dilation_kernel)
with warnings.catch_warnings(record=True):
warnings.filterwarnings('ignore', category=UserWarning)
if method == '3d':
skeleton = morphology.skeletonize_3d(mono_image)
elif method == 'medial':
skeleton = morphology.medial_axis(mono_image,
return_distance=False)
elif method == 'thin':
skeleton = morphology.thin(mono_image)
elif method == 'combined':
skeleton = (morphology.skeletonize_3d(mono_image)
| morphology.medial_axis(mono_image,
return_distance=False)
| morphology.skeletonize(mono_image))
else:
skeleton = morphology.skeletonize(mono_image)
return convert(skeleton)
评论列表
文章目录