Home > Software engineering >  Numpy - insert into array
Numpy - insert into array

Time:05-18

i created an AR aruco marker programm. My table has four markers in every corner one. Now i want to create a rectangle which bounds the four markers. I already have the coordinates of every point i want to connect them. Problem ist when i use cv2.createpolyline the function does not connect them in the right order. I now wanted to create a numpy array which inserts the coordinates of the marker if it is detected. And when all four a detected the polyline should be drawn. This is my code so far:

if id == [0]:
    outline_poly = np.insert(outline_poly, 0, tl)
if id == [1]:
    outline_poly = np.insert(outline_poly, 1, tr)
if id == [2]:
    outline_poly = np.insert(outline_poly, 2, bl)
if id == [3]:
    outline_poly = np.insert(outline_poly, 3, br)

For example: if marker with the ID = 0 is detected the coordinates of the point top left (tl) are inserted to the array. The code above is part of an loop which only checks one ID in every loop. So it would be awesome when ID0 is detected the coordinates of that point are in the first row ID1 in the second, id2 in the third,...

Hopefully that is understandable. Thank you very much.

CodePudding user response:

When you say "connect them in the right order", do you mean that you want the resulting polyline to be a closed loop? If so, you'll need to make sure that the first and last points in your array are the same. For example:

if id == [0]:
    outline_poly = np.insert(outline_poly, 0, tl)
if id == [1]:
    outline_poly = np.insert(outline_poly, 1, tr)
if id == [2]:
    outline_poly = np.insert(outline_poly, 2, bl)
if id == [3]:
    outline_poly = np.insert(outline_poly, 3, br)

make sure the first and last points are the same

outline_poly = np.append(outline_poly, outline_poly[0])

Alternatively, if you just want the polyline to be a straight line between the four points, you can use NumPy's vstack function:

if id == [0]:
    outline_poly = np.vstack((outline_poly, tl))
if id == [1]:
    outline_poly = np.vstack((outline_poly, tr))
if id == [2]:
    outline_poly = np.vstack((outline_poly, bl))
if id == [3]:
    outline_poly = np.vstack((outline_poly, br))
  • Related