用python写opencv骨架提取并且打印出骨架上所在的坐标点
时间: 2024-04-30 08:22:34 浏览: 186
以下是一个用Python和OpenCV实现骨架提取并打印坐标点的示例代码:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('skeleton.png', 0)
# 二值化图像
ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
# 提取骨架
size = np.size(img)
skel = np.zeros(img.shape, np.uint8)
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
# 打印骨架上的坐标点
rows, cols = np.where(skel == 255)
for i in range(len(rows)):
print('({},{})'.format(rows[i], cols[i]))
# 显示结果
cv2.imshow('Skeleton', skel)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个例子中,我们首先读取了一幅灰度图像,然后使用阈值将其二值化,接着使用OpenCV的findContours函数查找图像中的轮廓,并绘制出来。然后,我们使用一个循环来提取骨架,直到没有像素可以被腐蚀为止。最后,我们使用np.where函数找到骨架上的坐标点,并打印出来。最终,我们使用imshow函数显示提取出来的骨架。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)