用python怎么找出图片中物体轮廓的端点
时间: 2023-12-26 12:03:20 浏览: 222
您可以使用OpenCV库中的findContours函数找到物体的轮廓,并使用approxPolyDP函数来逼近这些轮廓的端点。findContours函数返回一组包含所有轮廓的点的数组,而approxPolyDP函数使用这些点来生成一组逼近的端点。下面的代码示例显示了如何使用这些函数来查找物体轮廓的端点:
```
import cv2
# Load image
img = cv2.imread('image.jpg')
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Threshold to create binary image
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Approximate contours and draw endpoints
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
for point in approx:
cv2.circle(img, (point[0][0], point[0][1]), 5, (255, 0, 0), -1)
# Display image
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文