[英]Is there a build-in function to do skeletonization?
I found some implementation in C/C++ such as voronoi skeleton .我在 C/C++ 中找到了一些实现,例如voronoi skeleton 。 Usually those codes require intensive looping, which is bad in python.通常这些代码需要密集的循环,这在 python 中是不好的。 Is there any build-in skeleton function can be called in python?有没有可以在python中调用的内置骨架function?
OpenCV doesn't have a skeleton function, but you can make your own function. OpenCV 没有骨架功能,但您可以制作自己的功能。 From here :从这里:
The skeleton/MAT can be produced in two main ways.骨架/MAT可以通过两种主要方式生产。
The first is to use some kind of morphological thinning that successively erodes away pixels from the boundary (while preserving the end points of line segments) until no more thinning is possible, at which point what is left approximates the skeleton.第一种是使用某种形态细化,从边界连续侵蚀像素(同时保留线段的端点),直到不再细化,此时剩下的部分近似于骨架。
The alternative method is to first calculate the distance transform of the image.另一种方法是首先计算图像的距离变换。 The skeleton then lies along the singularities (ie creases or curvature discontinuities) in the distance transform.然后骨架位于距离变换中的奇异点(即折痕或曲率不连续点)。 This latter approach is more suited to calculating the MAT since the MAT is the same as the distance transform but with all points off the skeleton suppressed to zero.后一种方法更适合计算 MAT,因为 MAT 与距离变换相同,但骨架外的所有点都被抑制为零。
Here you can find an example that uses morphological operations: 在这里您可以找到一个使用形态学运算的示例:
import cv2
import numpy as np
img = cv2.imread('sofsk.png',0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
ret,img = cv2.threshold(img,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
done = False
while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
if zeros==size:
done = True
cv2.imshow("skel",skel)
cv2.waitKey(0)
cv2.destroyAllWindows()
实际上,现在 OpenCV 确实在其扩展图像处理模块中实现了张素骨架化,请参见https://docs.opencv.org/4.4.0/df/d2d/group__ximgproc.html#ga37002c6ca80c978edb6ead5d6b39740c 。
From https://docs.opencv.org/master/df/d2d/group__ximgproc.html#ga37002c6ca80c978edb6ead5d6b39740c来自https://docs.opencv.org/master/df/d2d/group__ximgproc.html#ga37002c6ca80c978edb6ead5d6b39740c
Make sure you're using opencv-contrib-python确保您使用的是 opencv-contrib-python
thinned = cv2.ximgproc.thinning(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY))
By default, thinningType=cv2.ximgproc.THINNING_ZHANGSUEN.默认情况下,thinningType=cv2.ximgproc.THINNING_ZHANGSUEN。 But you can also do cv2.ximgproc.THINNING_GUOHALL like this:但是您也可以像这样执行 cv2.ximgproc.THINNING_GUOHALL:
thinned = cv2.ximgproc.thinning(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY), thinningType=cv2.ximgproc.THINNING_GUOHALL)
The skeletonization
process by morphology using only the erosion, dilation and subtraction methods as in this link :形态学的skeletonization
化过程仅使用此链接中的腐蚀、膨胀和减法方法:
Produces the following operation and result:产生以下操作和结果:
This can be simulated online in the Opencv-Flow tool, but reproducing this example is exhausting.这可以在Opencv-Flow工具中在线模拟,但是复制这个例子很累。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.