openCV中是否有内置函数可以进行骨骼化?

2024-05-19 02:50:25 发布

您现在位置:Python中文网/ 问答频道 /正文

<>我在C/C++中发现了一些实现,如voronoi skeleton。通常这些代码需要密集的循环,这在python中是不好的。python中是否可以调用任何内置骨架函数?


Tags: 函数代码内置骨架skeletonvoronoi密集
1条回答
网友
1楼 · 发布于 2024-05-19 02:50:25

OpenCV没有一个骨架函数,但是您可以创建自己的函数。来自here

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.

Here您可以找到一个使用形态操作的示例:

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

相关问题 更多 >

    热门问题