python - 是否有内置函数可以进行骨架化?

标签 python opencv

我在 C/C++ 中找到了一些实现,例如 voronoi skeleton .通常这些代码需要密集循环,这在 python 中是不好的。有没有可以在python中调用的内置骨架函数?

最佳答案

OpenCV 没有骨架 函数,但您可以创建自己的函数。来自 Skeletonization/Medial Axis Transform :

The skeleton/MAT can be produced in two main ways.

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 (i.e. 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.

Skeletonization using OpenCV-Python展示了一个使用形态学操作的例子:

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()

关于python - 是否有内置函数可以进行骨架化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33095476/

相关文章:

python - Django - 登录并重定向到用户个人资料页面

python - Pytorch如何将除第一维之外的可变大小的张量相乘

Python 协程在产量上给出未知的 None

python - Odoo 10 - XMLRPC - 使用外部 XML 标识符

java - 将 objective-c 代码转换为 java 会出现编译错误

python - 在 spyder 中访问 OpenCV 文档

python - 在 Lua 中什么都不做的函数

python - 如何制作语义标签图像?

python - opencv 在实时视频中计数对象 - 需要帮助对一盒面粉进行条件计数

c++ - 在 OpenCV 中什么是 VideoCapture 析构函数