gpt4 book ai didi

python - 如何使用OpenCV或numpy打印直线上每个点的坐标?

转载 作者:行者123 更新时间:2023-11-30 21:52:51 32 4
gpt4 key购买 nike

我使用以下代码打印线上每个点的坐标。

第一,我使用两个坐标在黑色二维平面上画一条线。

第二,我用两点的坐标来计算斜率和截距。

第三,我打印2D平面上直线上所有点的坐标。

我认为我不够聪明,无法做到这一点。虽然我可以解决这个问题,但这并不是一个简单的方法。

我的代码:

import cv2
import numpy as np

y1=-304 #point1_y
y2=477 #point2_y
x1=-957 #point1_x
x2=883 #point2_x

img=np.zeros((300,300,3),np.uint8)
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),3)
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

k=(y2-y1)/(x2-x1)
b = y1 - k*x1
for x in range(1,300):
y=k*x+b
print(x,y)

结果图像:

enter image description here

坐标信息:

1 102.62934782608696
2 103.05380434782609
3 103.47826086956522
4 103.90271739130435
5 104.32717391304348
6 104.75163043478261
7 105.17608695652174
8 105.60054347826087
9 106.025
10 106.44945652173914
……

对于opencv,有没有简单的方法来输出线上的每个点?

最佳答案

我不知道 opencv 是否可行,但 numpy 可行(因为您在代码中使用 numpy,我假设您没问题)。

import numpy as np
x = [-957, 883]
y = [-304, 477]
x_coor = np.arange(1, x2)
y_coor = np.interp(x_coor, x, y)
coordinates = np.column_stack((x_coor, y_coor))

关于python - 如何使用OpenCV或numpy打印直线上每个点的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59816588/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com