简体   繁体   English

从numpy数组制作顺序行(cv2)

[英]Making sequential line(cv2) from numpy array

I have a numpy array that consist n (x and y) coordinate (i declared this nparray as 'line') from an image(newimg).我有一个 numpy 数组,它包含来自图像(newimg)的 n(x 和 y)坐标(我将此 nparray 声明为'line')。 lets take 5 coordinates as the example让我们以 5 个坐标为例

[[ 33 101]
 [170  95]
 [151 190]
 [125 223]
 [115 207]]

an then i want to make a sequential line between that coordinates (point 1 to point 2, point 2 to point 3, ...., point n-1 to point n and the point n to point 1)然后我想在该坐标(点 1 到点 2,点 2 到点 3,....,点 n-1 到点 n 和点 n 到点 1)之间画一条连续线

I trying to make the algorithm with cv2.line like this我试图用 cv2.line 像这样制作算法

for a in range(5) :
   cv2.line(newimg, line[a:], line[a+1:], (255,255,255), 1)
cv2.line(newimg, line, line[1], (255,255,255), 1)

and it produce SystemError: new style getargs format but argument is not a tuple它产生 SystemError: new style getargs format but argument is not a tuple

any idea?任何想法?

Solve, I edited my code to解决,我将代码编辑为

for a in range(4) :
    cv2.line(newimg, tuple(line[a]), tuple(line[a+1]), (255,255,255), 1)
cv2.line(newimg, tuple(line[4], tuple(line[0]), (255,255,255), 1)

and image:和图像:

在此处输入图像描述

Problems:问题:

There are some changes that need to be addressed:有一些变化需要解决:

  1. To access a single value in an array use line[i] .要访问数组中的单个值,请使用line[i] line[i:] returns an array of points staring from index i till the end. line[i:]返回从索引i到末尾的点数组。
  2. cv2.line() accepts tuples as start and end points. cv2.line()接受元组作为起点和终点。

Code:代码:

# proposed line as an array
line = np.array([[ 33, 101], [170,  95], [151, 190], [125, 223], [115, 207]])

# blank image
newimg = np.zeros((300, 300, 3), np.uint8)

# iterate every point and connect a line between it and the next point
for a in range(len(line) - 1):
    newimg = cv2.line(newimg, tuple(line[a]), tuple(line[a+1]), (255,255,255), 2)
newimg = cv2.line(newimg, tuple(line[0]), tuple(line[-1]), (255,255,255), 2)

Output:输出:

在此处输入图像描述

EDIT编辑

Updated the code to connect a line between the first and last points in line更新了代码以在行的第一个点和最后一个点之间连接一条line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM