extract 2d-3d bounding box corners
Closed this issue · 3 comments
Thank you for sharing your code.
I want to draw the projected ground truth 3D bounding boxes onto the 2d image, however, I couldn't extract the corners of the 3d boxes in 2d correctly. Do you have an idea how can I do it?
Thanks!
Hi, I have a small code snippet about projecting bbox corner into 2D image. The obj_matrix can refer to the tran_matrix, in the prepare data, you can try if it is able to draw correctly.
def get_bbox_corners(bbox_size,obj_matrix,wrd2cam_matrix):
'''
get bbox in camera coordinate
'''
s_x, s_y, s_z = bbox_size[0], bbox_size[1], bbox_size[2]
verts_cannonical = [[- s_x / 2, - s_y / 2, - s_z / 2],
[- s_x / 2, - s_y / 2, s_z / 2],
[- s_x / 2, s_y / 2, - s_z / 2],
[- s_x / 2, s_y / 2, s_z / 2],
[s_x / 2, - s_y / 2, - s_z / 2],
[s_x / 2, - s_y / 2, s_z / 2],
[s_x / 2, s_y / 2, - s_z / 2],
[s_x / 2, s_y / 2, s_z / 2]]
verts_cannonical=np.array(verts_cannonical)
verts = np.dot(obj_matrix[0:3,0:3], verts_cannonical.T).T+obj_matrix[0:3,3]
verts=np.dot(wrd2cam_matrix[0:3,0:3],verts.T).T+wrd2cam_matrix[0:3,3]
verts[:,0:2]=-verts[:,0:2]
return verts
def project_points2img(points,K):
'''
:param points: n3, in camera coordinate
:param K: 33 intrinsic matrix
:return:
'''
points=np.dot(points,K[0:3,0:3].T)
x_coor=points[:,0]/points[:,2]
y_coor=points[:,1]/points[:,2]
z=points[:,2]
img_coor=np.concatenate([x_coor[:,np.newaxis],y_coor[:,np.newaxis],z[:,np.newaxis]],axis=1)
return img_coor
I think it maybe the problem that the image in the prepare data is already downsampled by 2. You can try to draw with half the values of the image coordinates.
It fixed the problem. Thank you very much! :)