-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtagUtils.py
104 lines (91 loc) · 2.85 KB
/
tagUtils.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
import numpy as np
import cv2
import math
from scipy.optimize import fsolve
ss = 0.5
src = np.array([[-ss, -ss, 0],
[ss, -ss, 0],
[ss, ss, 0],
[-ss, ss, 0]])
#500 is scale
Kmat = np.array([[700, 0, 0],
[0, 700, 0],
[0, 0, 1]]) * 1.0
disCoeffs= np.zeros([4, 1]) * 1.0
edges = np.array([[0, 1],
[1, 2],
[2, 3],
[3, 0]])
def project(H,point):
x = point[0]
y = point[1]
z = H[2,0]*x +H[2,1]*y+H[2,2]
point[0] = (H[0,0]*x+H[0,1]*y+H[0,2])/z*1.0
point[1] = (H[1, 0]*x+H[1, 1] *y + H[1, 2]) / z*1.0
return point
def project_array(H):
ipoints = np.array([[-1,-1],
[1,-1],
[1,1],
[-1,1]])
for point in ipoints:
point = project(H,point)
return ipoints
def sovle_coord(R1,R2,R3,edge = 1060):
x = -(R2*R2 - R1*R1 - edge**2) / (2.0*edge)
y = -(R3*R3 - R1*R1 - edge**2) / (2.0*edge)
z = (np.sqrt(R1*R1 - x * x - y * y))-edge
return x,y,z
def verify_z(x,y,R4,edge = 1060):
x = edge - x
y = edge - y
rand2 = x**2+y**2
h = np.sqrt(R4**2 - rand2)
return edge - h
def get_Kmat(H):
campoint = project_array(H)*1.0
opoints = np.array([[-1.0, -1.0, 0.0],
[1.0, -1.0, 0.0],
[1.0, 1.0, 0.0],
[-1.0, 1.0, 0.0]])
opoints = opoints*0.5
rate, rvec, tvec = cv2.solvePnP(opoints, campoint, Kmat, disCoeffs)
return rvec,tvec
def get_pose_point(H):
"""
将空间坐标转换成相机坐标
Trans the point to camera point
:param H: homography
:return:point
"""
rvec, tvec = get_Kmat(H)
point, jac = cv2.projectPoints(src, rvec, tvec, Kmat, disCoeffs)
return np.int32(np.reshape(point,[4,2]))
def get_pose_point_noroate(H):
"""
将空间坐标转换成相机坐标但是不旋转
Trans the point to camera point but no rotating
:param H: homography
:return:point
"""
rvec, tvec = get_Kmat(H)
point, jac = cv2.projectPoints(src, np.zeros(rvec.shape), tvec, Kmat, disCoeffs)
return np.int32(np.reshape(point,[4,2]))
def average_dis(point,k):
return np.abs( k/np.linalg.norm(point[0] - point[1]))
def average_pixel(point):
return np.abs( np.linalg.norm(point[0] - point[1]))
def get_distance(H,t):
points = get_pose_point_noroate(H)
return average_dis(points,t)
def get_min_distance(array_detections,t):
min = 65535;
for detection in array_detections:
#print(detection.id)
dis = get_distance(detection.homography,t)
if dis < min:
min = dis
return min;
def get_pixel(H):
points = get_pose_point_noroate(H)
return average_pixel(points)