在openCV中有什么内置函数可以做骨架化吗? [英] is there any build-in function can do skeletonization in openCV?

查看:61
本文介绍了在openCV中有什么内置函数可以做骨架化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I found some implementation in C/C++ such as voronoi skeleton. Usually those codes require intensive looping, which is bad in python. Is there any build-in skeleton function can be called in python?

推荐答案

OpenCV没有 skeleton 函数,但是您可以创建自己的函数.来自此处:

OpenCV doesn't have a skeleton function, but you can make your own function. From here:

骨架/MAT可以通过两种主要方式生成.

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.

另一种方法是首先计算距离变换图片.然后,骨骼沿着距离变换中的奇异点(即折痕或曲率不连续点)放置.后一种方法更适合于计算MAT,因为MAT与距离变换相同,但骨架上的所有点都被抑制为零.

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.

此处,您可以找到使用以下示例形态学操作:

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中有什么内置函数可以做骨架化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆