要获取轮廓的骨架并提取点列表,可以使用opencv中的findContours和approxPolyDP函数。以下是一个示例代码:

import cv2 import numpy as np

读取图像并灰度化

img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

二值化

ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

查找轮廓

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

遍历轮廓

for cnt in contours: # 获取轮廓的骨架 hull = cv2.convexHull(cnt)

# 使用approxPolyDP函数对骨架进行多边形拟合
epsilon = 0.01 * cv2.arcLength(hull, True)
approx = cv2.approxPolyDP(hull, epsilon, True)

# 提取点列表
points = []
for i in range(len(approx)):
    x, y = approx[i][0]
    points.append((x,y))

# 绘制骨架和多边形拟合结果
cv2.drawContours(img, [hull], -1, (0, 0, 255), 2)
cv2.drawContours(img, [approx], -1, (0, 255, 0), 2)

cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()

注意,这里使用的是convexHull函数获取轮廓的骨架,如果你要获取非凸形状的骨架,可以使用Skeletonization算法

python opencv 获取轮廓的骨架并提取点列表

原文地址: https://www.cveoy.top/t/topic/d8af 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录