OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 14 Mar 2019 04:58:11 -0500Calculating a trajectory using a homography projectionhttp://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/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 = H.dot(p)
sum = np.sum(temp_p, 1)
px = int(round(sum[0] / sum[2]))
py = int(round(sum[1] / sum[2]))
cv2.circle(img, 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))Tue, 12 Mar 2019 13:31:48 -0500http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/Comment by Der Luftmensch for <p>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.</p>
<p>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</p>
<pre><code> 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 = H.dot(p)
sum = np.sum(temp_p, 1)
px = int(round(sum[0] / sum[2]))
py = int(round(sum[1] / sum[2]))
cv2.circle(img, 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))
</code></pre>
http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210189#post-id-210189http://www.cs.toronto.edu/~fidler/slides/2017/CSC420/lecture9.pdfTue, 12 Mar 2019 14:12:33 -0500http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210189#post-id-210189Answer by emlot77 for <p>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.</p>
<p>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</p>
<pre><code> 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 = H.dot(p)
sum = np.sum(temp_p, 1)
px = int(round(sum[0] / sum[2]))
py = int(round(sum[1] / sum[2]))
cv2.circle(img, 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))
</code></pre>
http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?answer=210231#post-id-210231I 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 imageWed, 13 Mar 2019 10:13:09 -0500http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?answer=210231#post-id-210231Comment by Ahmed for <p>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 :</p>
<p>1 - Generate a black image and add the bounding box using the function <code>rectangle</code> with white pixels for example</p>
<p>2 - Compute the transformed image using the homography matrix</p>
<p>3 - Get the position of the white pixels in the converted homography coordinates image</p>
http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210256#post-id-210256I 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 itThu, 14 Mar 2019 04:58:11 -0500http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210256#post-id-210256Comment by Der Luftmensch for <p>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 :</p>
<p>1 - Generate a black image and add the bounding box using the function <code>rectangle</code> with white pixels for example</p>
<p>2 - Compute the transformed image using the homography matrix</p>
<p>3 - Get the position of the white pixels in the converted homography coordinates image</p>
http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210248#post-id-210248There 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.Wed, 13 Mar 2019 19:55:37 -0500http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210248#post-id-210248Comment by Ahmed for <p>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 :</p>
<p>1 - Generate a black image and add the bounding box using the function <code>rectangle</code> with white pixels for example</p>
<p>2 - Compute the transformed image using the homography matrix</p>
<p>3 - Get the position of the white pixels in the converted homography coordinates image</p>
http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210238#post-id-210238Can you post an answer ? with code please ? I don't get youWed, 13 Mar 2019 11:44:29 -0500http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210238#post-id-210238Comment by Der Luftmensch for <p>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 :</p>
<p>1 - Generate a black image and add the bounding box using the function <code>rectangle</code> with white pixels for example</p>
<p>2 - Compute the transformed image using the homography matrix</p>
<p>3 - Get the position of the white pixels in the converted homography coordinates image</p>
http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210237#post-id-210237Why not simply transform the four corners (or any set of points) using the homography? See the link provided as a comment to the question.Wed, 13 Mar 2019 11:24:11 -0500http://answers.opencv.org/question/210188/calculating-a-trajectory-using-a-homography-projection/?comment=210237#post-id-210237