-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
117 lines (99 loc) · 3.39 KB
/
utility.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import matplotlib.pyplot as plt
import numpy as np
import numpy.matlib as mth
#-------------------------------------------------------------------
# Plot
#-------------------------------------------------------------------
# plot chart
def plotchart(chart):
"Plot chart color"
chart = np.array(chart);
n = chart.shape[0];
indx = np.array([0, 2, 4, 6, 0]);
for i in range(n):
c = chart[i,:];
plt.plot(c[indx],c[indx+1]);
def plotbbox(bbox, color=[1,0,0]):
"Plot bounding box"
x = bbox[:,0]; y = bbox[:,1];
bbox = np.array([[x[0],y[0]],[x[1],y[0]],[x[1],y[1]],[x[0],y[1]],[x[0],y[0]]]);
plt.plot(bbox[:,0],bbox[:,1],'o-', color=color);
def imageshow(im):
"Show color image"
plt.imshow(im, interpolation = 'bicubic');
plt.xticks([]), plt.yticks([]); # to hide tick values on X and Y axis
#-------------------------------------------------------------------
# Math
#-------------------------------------------------------------------
# R_t = angle2mat( \fi_t^i )
# Exemplo:
# R = angle2mat([0, 0, 0])
# print(np.sum(R==np.eye(3)) == 9)
#
def angle2mat( fi ):
"To convert angle to rotation matrix"
rx = fi[0]; ry = fi[1]; rz = fi[2];
sx = np.sin(rx); cx = np.cos(rx);
sy = np.sin(ry); cy = np.cos(ry);
sz = np.sin(rz); cz = np.cos(rz);
Rx = np.array([[ 1, 0, 0], [ 0, cx, -sx], [ 0, sx, cx]]);
Ry = np.array([[cy, 0, sy], [ 0, 1, 0], [-sy, 0, cy]]);
Rz = np.array([[cz, -sz, 0], [sz, cz, 0], [ 0, 0, 1]]);
return np.dot(Rz,np.dot(Ry,Rx));
# function pp = tocenter(p)
def center(p):
"Center point"
c = np.mean(p,axis=0);
return p-mth.repmat(c,p.shape[0],1);
# function p=projection(P,K,R,T)
# Exemplo:
# P = np.array([[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]]);
# K = np.array([[10, 0, 10],[0, 10, 10],[0, 0, 1]]);
# R = angle2mat([0,0,np.pi/4]);
# t = np.array([[0,0,-10]]);
# p = projection(P,K,R,t);
# plt.figure(1)
# plt.plot(p[:,0],p[:,1],'o');
# plt.show();
#
def projection(P,K,R,t):
"Projection in 2d plane"
P = center(P);
H = np.dot(K,np.concatenate((R,t.T),axis=1));
P = np.concatenate((P,np.ones([P.shape[0],2])),axis=1);
p = np.dot(H,P.T);
p = p[0:2]/p[2,:];
return p.T;
# function bbox = boundingbox(box)
# minx = min(box(:,1)); miny = min(box(:,2));
# maxx = max(box(:,1)); maxy = max(box(:,2));
# bbox = [minx miny; maxx maxy];
# end
def boundingbox(box):
"Estimate boundingbox"
xmin = np.min(box[:,0]); ymin = np.min(box[:,1]);
xmax = np.max(box[:,0]); ymax = np.max(box[:,1]);
bbox = np.array([[xmin, ymin],[xmax, ymax]]);
return bbox;
def validimagebox(box, imshape):
'''get valid box in the image'''
xmin = np.max((box[0,0],0)); ymin = np.max((box[0,1],0));
xmax = np.min((box[1,0],imshape[1])); ymax = np.min((box[1,1],imshape[0]));
box = np.array([[xmin, ymin],[xmax, ymax]]);
return box;
def boxdimension(bbox):
h = abs(bbox[1,1] - bbox[0,1]);
w = abs(bbox[1,0] - bbox[0,0]);
return w, h
def isboxtruncate(box, imsize):
"Is box truncate for the image"
return (np.any(box<0) or np.any(box[:,0]>=imsize[1]) or np.any(box[:,1]>=imsize[0]));
def isboxocclude(bbox1, bbox2):
"Is occlude box"
s1 = bbox1[1,:] - bbox1[0,:];
s2 = bbox2[1,:] - bbox2[0,:];
c1 = np.mean(bbox1,axis=0);
c2 = np.mean(bbox2,axis=0);
d = np.abs(c1-c2);
s = (s1+s2)/2;
return np.all((d-s) < 0);