0

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.

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your help. I have the four points and they should be a closed loop. If i just put the coordinates in a list without any sorting i sometimes get a loop and other times the lines are crossed. If i do it like you said i'd get that error: IndexError: index 3 is out of bounds for axis 0 with size 2
I guess the problem is the sorting of the coordinates. I need an array where in the first row is the coordinates of id0 in the second row the coordinates of id1. The crossed lines i get when the order is incorrect. Maybe i should from scratch and get a better code for the checking of the ID than four if-statements. Do you a have idea for that?
Yeah sorry buddy guess im bad in explaining. Sorting is maybe the wrong word. cv2.polylines needs the coordinates in the right order so id0 as first point, id1 as second point. And that is my problem that my code doesnt create a array where that points are in this format.
If i use the code from above i get every time one id is detected a new array with only coorinate inside not all four.
[[208. 193.] [ 0. 0.] [ 0. 0.] [ 0. 0.] [ 0. 0.]] and than: [[ 0. 0.] [567. 198.] [ 0. 0.] [ 0. 0.] [ 0. 0.]] but i need both coordinates in that array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.