请写出python opencv 为二值化图片轮廓点每一百个取一个样点,并获取样点坐标的具体代码
时间: 2023-03-08 14:16:41 浏览: 127
import cv2
# Read the image
img = cv2.imread("image.jpg")
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the contours
contours, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Iterate through each contour
for cnt in contours:
# Calculate approximated contour
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
# Get coordinates of each point in the approximated contour
points = approx.reshape(len(approx), -1)
x = points[:, 0]
y = points[:, 1]
# Take every 100th point
step = max(1, int(len(x)/100))
x = x[::step]
y = y[::step]
# Get corresponding coordinates of each point
coordinates = list(zip(x, y))
# Print points
print(coordinates)
阅读全文