cv2如何得到轮廓的坐标
时间: 2023-10-27 13:45:42 浏览: 74
您可以使用OpenCV库中的`findContours()`函数来获取图像中轮廓的坐标。以下是一个示例代码:
```python
import cv2
# 读取图像
image = cv2.imread('image.jpg')
# 将图像转换为灰度
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化图像
_, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
# 显示结果
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上述代码中,首先读取图像并将其转换为灰度图像。然后,通过二值化将图像转换为黑白图像,以便更容易检测轮廓。接下来,使用`findContours()`函数查找轮廓,并将其保存在`contours`变量中。最后,使用`drawContours()`函数将轮廓绘制在原始图像上,并显示结果。
请注意,`findContours()`函数的第一个参数是二值化后的图像,第二个参数是轮廓检测模式,第三个参数是轮廓近似方法。在示例中,我们使用了`RETR_EXTERNAL`模式来检测最外层的轮廓,并使用`CHAIN_APPROX_SIMPLE`方法来压缩轮廓的点。
希望能对您有所帮助!
阅读全文
相关推荐















