简体   繁体   English

是否有内置 function 进行骨架化?

[英]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.

相关问题 带枚举内置函数的嵌套列表理解 - Nested list comprehension with enumerate build-in function 如何查找/检测Python AST中是否使用了内置函数? - How to find/detect if a build-in function is used in Python AST? 如何在不使用 sorted 内置 function 的情况下对事物进行排序? - How to sort things, without using the sorted build-in function? 自定义 RMSE 损失 function 和内置 RMSE 的 Lightgbm 分数不同 - Lightgbm scores for custom RMSE loss function and build-in RMSE are different 功能类似于内置地图,但支持numpy广播 - A function similar to build-in map but support numpy broadcast python3中的下一个内置函数是什么意思? - What does next build-in function mean in python3? python将内置属性函数分配给列表元素 - python assign build-in property function to list element 使用内置函数雅可比计算 PyTorch 中的雅可比时得到全零答案 - Get all zero answer while calculating jacobian in PyTorch using build-in function-jacobian 如何像内置类一样在 __init__ state 之前禁用 class 的 settattr? - How to disable settattr for your class in pre __init__ state just like build-in classes do? 如何使用第3方安装的模块替换Python Build-in方法? - How do I replace the Python Build-in method, with a 3rd party installed module instead?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM