-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathequi_to_offset_cube.py
More file actions
175 lines (126 loc) · 6.1 KB
/
equi_to_offset_cube.py
File metadata and controls
175 lines (126 loc) · 6.1 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import cv2
import numpy as np
from cube import Cube
from cube import Face
def xrotation(th):
c = np.cos(th)
s = np.sin(th)
return np.array([[1, 0, 0], [0, c, s], [0, -s, c]])
def yrotation(th):
c = np.cos(th)
s = np.sin(th)
return np.array([[c, 0, s], [0, 1, 0], [-s, 0, c]])
def deg2rad(d):
return d*np.pi/180
def rotate_image(old_image):
(old_height, old_width, _) = old_image.shape
M = cv2.getRotationMatrix2D(((old_width - 1) / 2., (old_height - 1) / 2.), 270, 1)
rotated = cv2.warpAffine(old_image, M, (old_width, old_height))
return rotated
class OffsetCube(Cube):
def __init__(self, expand_coef, offcenter_z, yaw, pitch, w, h, equi_img, bmp_fn=None):
self.expand_coef = expand_coef
self.offcenter_z = offcenter_z
self.yaw = yaw # in rad
self.pitch = pitch # in rad
self.fw = w # face width
self.fh = h # face height
self.bmp_fn = bmp_fn
d= {}
d['left'] = (0, 3*np.pi/2)
d['front'] = (0, 0)
d['right'] = (0, np.pi/2)
d['back'] = (0, np.pi)
d['top'] = (np.pi/2, np.pi)
d['bottom'] = (-np.pi/2, np.pi)
self.d = d
self.init_cube(equi_img)
def init_cube(self, equi_img):
NO_ROTATE = 0
a = np.array
self.faces = [
Face('left', project_offset_face_np(self.pitch, self.yaw, self.d['left'], self.fh, self.fw, self.expand_coef, self.offcenter_z, equi_img), a([-1., 0., 0.]), a([ 0., 0., 1.]), a([0., 1., 0.]), self.expand_coef, NO_ROTATE, self.yaw, self.pitch),
Face('front', project_offset_face_np(self.pitch, self.yaw, self.d['front'], self.fh, self.fw, self.expand_coef, self.offcenter_z, equi_img), a([ 0., 0., 1.]), a([ 1., 0., 0.]), a([0., 1., 0.]), self.expand_coef, NO_ROTATE, self.yaw, self.pitch),
Face('right', project_offset_face_np(self.pitch, self.yaw, self.d['right'], self.fh, self.fw, self.expand_coef, self.offcenter_z, equi_img), a([ 1., 0., 0.]), a([ 0., 0., -1.]), a([0., 1., 0.]), self.expand_coef, NO_ROTATE, self.yaw, self.pitch),
Face('top', project_offset_face_np(self.pitch, self.yaw, self.d['top'], self.fh, self.fw, self.expand_coef, self.offcenter_z, equi_img), a([ 0., 1., 0.]), a([-1., 0., 0.]), a([0., 0., 1.]), self.expand_coef, NO_ROTATE, self.yaw, self.pitch),
Face('back', project_offset_face_np(self.pitch, self.yaw, self.d['back'], self.fh, self.fw, self.expand_coef, self.offcenter_z, equi_img), a([ 0., 0., -1.]), a([-1., 0., 0.]), a([0., 1., 0.]), self.expand_coef, NO_ROTATE, self.yaw, self.pitch),
Face('bottom', project_offset_face_np(self.pitch, self.yaw, self.d['bottom'],self.fh, self.fw, self.expand_coef, self.offcenter_z, equi_img), a([ 0., -1., 0.]), a([-1., 0., 0.]), a([0., 0.,-1.]), self.expand_coef, NO_ROTATE, self.yaw, self.pitch),
]
self.front_face = self.faces[1].pv
self.face_vecs = np.zeros((3,6))
for i, f in enumerate(self.faces):
self.face_vecs[:, i] = f.pv / f.k
def project_offset_face_np(theta0, phi0, f_info, height, width, expand_coef, offcenter_z, img):
"""
theta0 is front pitch
phi0 is front yaw
both in radiant
"""
theta1, phi1= f_info
m = np.dot( yrotation( phi0 ), xrotation( theta0 ) )
n = np.dot( yrotation( phi1 ), xrotation( theta1 ) )
mn = np.dot( m, n )
(base_height, base_width, _) = img.shape
new_img = np.zeros((height, width, 3), np.uint8)
DI = np.ones((height*width, 3), np.int)
trans = np.array([[2./float(width)*expand_coef, 0., -expand_coef],
[0.,-2./float(height)*expand_coef, expand_coef]])
xx, yy = np.meshgrid(np.arange(width), np.arange(height))
DI[:, 0] = xx.reshape(height*width)
DI[:, 1] = yy.reshape(height*width)
v = np.ones((height*width, 3), np.float)
v[:, :2] = np.dot(DI, trans.T)
v = np.dot(v, mn.T)
pv = np.dot(np.array([0, 0, 1.]), m.T)
off = offcenter_z * pv
a = v[:,0]**2 + v[:,1]**2 + v[:,2]**2
b = -2 * (v[:,0]*off[0] + v[:,1]*off[1] + v[:,2]*off[2])
c = np.sum(off*off)-1
t = (-b+np.sqrt(b*b - 4*a*c))/(2*a)
v = v*t[:,None] - off
diag = np.sqrt(v[:,2]**2 + v[:,0]**2)
theta = np.pi/2 - np.arctan2(v[:,1],diag)
phi = np.arctan2(v[:,0],v[:,2]) + np.pi
ey = np.rint(theta*base_height/np.pi ).astype(np.int)
ex = np.rint(phi*base_width/(2*np.pi) ).astype(np.int)
ex[ex >= base_width] = base_width - 1
ey[ey >= base_height] = base_height - 1
new_img[DI[:, 1], DI[:, 0]] = img[ey, ex]
return new_img
def equi_to_offset_cube(image):
cube_yaw = 0
cube_pitch = 0
face_size = 656
off_cb = OffsetCube(1.025, -0.7, deg2rad(cube_yaw), deg2rad(cube_pitch), face_size, face_size, image)
write_to_cb_img(off_cb, face_size, 'g_offsetcube.jpg')
def equi_to_cube(image):
cube_yaw = 0
cube_pitch = 0
face_size = 656
off_cb = OffsetCube(1.01, 0., deg2rad(cube_yaw), deg2rad(cube_pitch), face_size, face_size, image)
write_to_cb_img(off_cb, face_size, 'g_cube.jpg')
def write_to_cb_img(off_cb, face_size, name):
cube_img_h = face_size * 3
cube_img_w = face_size * 2
cube_img = np.zeros((cube_img_h, cube_img_w, 3), np.uint8)
for face in off_cb.faces:
if face.descr == 'top':
cube_img[:cube_img_h / 3, cube_img_w / 2:] = face.img.copy()
elif face.descr == 'front':
cube_img[cube_img_h / 3:cube_img_h * 2 / 3, :cube_img_w / 2] = rotate_image(face.img).copy()
elif face.descr == 'right':
cube_img[cube_img_h * 2 / 3:, :cube_img_w / 2] = rotate_image(face.img).copy()
elif face.descr == 'back':
cube_img[cube_img_h / 3:cube_img_h * 2 / 3, cube_img_w / 2:] = face.img.copy()
elif face.descr == 'left':
cube_img[:cube_img_h / 3, :cube_img_w / 2] = rotate_image(face.img).copy()
elif face.descr == 'bottom':
cube_img[cube_img_h * 2 / 3:, cube_img_w / 2:] = face.img.copy()
cv2.imwrite(name, cube_img)
if __name__== '__main__':
image = cv2.imread('../youtube_equi_image.jpg')
cube_yaw = 0
cube_pitch = 0
off_cb = OffsetCube(1.03125, -0.7, deg2rad(cube_yaw), deg2rad(cube_pitch), 528, 528, image)
for face in off_cb.faces:
cv2.imwrite('generated_offset_cube_%s.bmp'%face.descr, face.img)