-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkitti2Dbox.lua
97 lines (67 loc) · 2.18 KB
/
kitti2Dbox.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
----------------------------------------------------------------------
-- KITTI dataset interface
--
--
-- Author : Aysegul Dundar
-- email : [email protected]
-- Date 04/17/2013
----------------------------------------------------------------------
function wrapToPi(alpha)
local alpha = alpha%(2*math.pi)
if (alpha>math.pi) then
alpha = alpha-2*math.pi
end
return alpha;
end
function projectToImage(pts_3D, K)
local pts_2D = K*pts_3D[{{1,3},{}}] --pts_3D(1:3,:)
pts_2D[1] = pts_2D[1]:cdiv(pts_2D[3])
pts_2D[2] = pts_2D[2]:cdiv(pts_2D[3])
-- the last row is meaningless should be cropped
return(pts_2D)
end
function kitti2Dbox(tracklet, corner)
local w=corner.w
local h=corner.h
local l=corner.l
local corners={}
corners.x = torch.Tensor{l/2, l/2, -l/2, -l/2, l/2, l/2, -l/2, -l/2};
corners.y = torch.Tensor{w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2};
corners.z = torch.Tensor{0,0,0,0,h,h,h,h};
local t = {tracklet.tx; tracklet.ty; tracklet.tz};
local rz = wrapToPi(tracklet.rz)
occlusion =0;
local R = torch.Tensor{{math.cos(rz), -math.sin(rz), 0},
{math.sin(rz), math.cos(rz), 0},
{0, 0, 1}}
local a = torch.Tensor(3,8)
a[1] = corners.x
a[2] = corners.y
a[3] = corners.z
local corners_3D = R*a
corners_3D[1] = corners_3D[1]+t[1]
corners_3D[2] = corners_3D[2]+t[2]
corners_3D[3] = corners_3D[3]+t[3]
a = torch.Tensor(4,8):fill(1)
a[1]:copy(corners_3D[1])
a[2]:copy(corners_3D[2])
a[3]:copy(corners_3D[3])
local velToCam = torch.Tensor{
{0.0002, -0.9999, -0.0106, 0.0594},
{0.0104, 0.0106, -0.9999, -0.0751},
{0.9999, 0.0001, 0.0105, -0.2721},
{ 0, 0, 0, 1.0000}}
corners_3D = velToCam*a
local K = torch.Tensor{
{721.5377, 0, 609.5593},
{0, 721.5377, 172.8540},
{0, 0, 1.0000}}
local corners_2D = projectToImage(corners_3D, K);
-- compute and draw the 2D bounding box from the 3D box projection
box={}
box.x1 = torch.min(corners_2D[1]);
box.x2 = torch.max(corners_2D[1]);
box.y1 = torch.min(corners_2D[2]);
box.y2 = torch.max(corners_2D[2]);
return box
end