python提取图像轮廓坐标
时间: 2023-07-18 14:43:27 浏览: 69
要提取图像轮廓坐标,可以使用OpenCV库中的findContours函数。示例代码如下:
```python
import cv2
# 读入图像
img = cv2.imread('image.jpg')
# 将图像转为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 提取轮廓
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 遍历轮廓并绘制
for contour in contours:
cv2.drawContours(img, [contour], -1, (0, 0, 255), 2)
# 显示图像
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 获取第一个轮廓的坐标
contour = contours[0]
print(contour)
```
其中,findContours函数的参数解释如下:
- binary:二值化后的图像
- cv2.RETR_EXTERNAL:只检测外轮廓
- cv2.CHAIN_APPROX_SIMPLE:压缩轮廓,只保留轮廓的端点坐标
运行代码后,会显示绘制了轮廓后的图像,并输出第一个轮廓的坐标。