Ask Your Question

Calculating a trajectory using a homography projection

asked 2019-03-12 13:31:48 -0500

Ahmed gravatar image

updated 2019-03-14 04:57:36 -0500

I have the homography matrix and I projected a scene using that homography matrix. Now I have trajectories in 2D, I want to transform them into the projected scene.

I'm trying to do object detection in one image, and do a homography calculations of trajectories of that image, I don't know how to convert the coordinates from the detected bounding box centers of the first image to converted homography coordinates image

  for trk in car_detections:
        trk = trk.astype(np.int32)

        p = np.array(((trk[1] + trk[3]) / 2,  (trk[0] + trk[2]) / 2, 1)).reshape((3, 1))
        temp_p =
        sum = np.sum(temp_p, 1)
        px = int(round(sum[0] / sum[2]))
        py = int(round(sum[1] / sum[2])), center = (px, py), radius= 10, color=(255,0,0))

        point_lists[trk[4]].append((px, py))
        x = [i[0] for i in point_lists[trk[4]]]
        y = [i[1] for i in point_lists[trk[4]]]
        p = np.polyfit(x, y, deg=1)
        y = p[1] + p[0] * np.array(x)
        fitted = list(zip(x, y))
        cv2.polylines(img, np.int32([fitted]), False, color=(255, 0, 0))
edit retag flag offensive close merge delete


1 answer

Sort by ยป oldest newest most voted

answered 2019-03-13 10:13:09 -0500

I am not sure to understand everything, but I was thinking about something that might help. If you know the position of the bounding box in the first image, the homography matrix and you are able to transform an image to the projected scene using homography :

1 - Generate a black image and add the bounding box using the function rectangle with white pixels for example

2 - Compute the transformed image using the homography matrix

3 - Get the position of the white pixels in the converted homography coordinates image

edit flag offensive delete link more


Why not simply transform the four corners (or any set of points) using the homography? See the link provided as a comment to the question.

Der Luftmensch gravatar imageDer Luftmensch ( 2019-03-13 11:24:11 -0500 )edit

Can you post an answer ? with code please ? I don't get you

Ahmed gravatar imageAhmed ( 2019-03-13 11:44:29 -0500 )edit

There is no reason to transform an entire image when you can just transform/warp the bounding box corners. The slides linked above show exactly how this can be accomplished and are worth browsing for how clearly they explain the concept.

Der Luftmensch gravatar imageDer Luftmensch ( 2019-03-13 19:55:37 -0500 )edit

I transformed the coordinates of the bounding box, but I can't visualize it, please look at the edited post code. I did what you want. Please post an answer with the fix, It took me a lot of days to do it

Ahmed gravatar imageAhmed ( 2019-03-14 04:58:11 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-03-12 13:31:48 -0500

Seen: 34 times

Last updated: Mar 14