-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforwardInterpolated.py
147 lines (120 loc) · 3.6 KB
/
forwardInterpolated.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
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
from Point3D import *
import cv2
import numpy as np
from scipy import interpolate
R = 150 # radius of the dome
r = 33 # radius of the spherical mirror in cm
dx, dy, dz = 0,R,0 # dome coord
ang = radians(70) - atan(13.8/8.3) # mirror coord
mx, my, mz = 0, -18*sin( 70*pi/180 - atan(8.3/13.8) ), -18*cos( 70*pi/180 - atan(8.3/13.8) )
px, py, pz = 0, 75, -20 # projector coordinate
cx,cy,cz = 0,0,0 # a coordinate that the projector light ray shoots on
projectorThrow = 2.2
aspect = 4.0/3.0
img = cv2.imread("/home/stuadmin/Desktop/BSRI_Henry_2019/image115.jpg", 1)
h,w,d = img.shape
mirVect = Point3D(mx,my,mz)
projCenter = Point3D(cx, cy, cz)
projector = Point3D(px, py, pz)
projectingVect = sub(projCenter, projector)
projectionDepth = w * projectorThrow
def pixelToImagePlane(i, j):
# map to Image on centered on x - axis
u = Point3D(w/2-i, -projectionDepth, h/2-j)
# tilt to projection plane into projection direction
negY = Point3D(0,-1,0)
theta = -(angleBetween(negY,projectingVect))
v = u.copy()
v.y = u.y*cos(theta) - u.z*sin(theta)
v.z = u.y*sin(theta) + u.z*cos(theta)
v.add(projector)
return v
def imagePlaneToMirror(u):
i = sub(u,projector)
a = i.magSquared()
b = 2*dot(i,projector) - 2*dot(mirVect,i)
c = projector.magSquared() + mirVect.magSquared() - 2*dot(projector,mirVect) - r**2
if b**2- 4*a*c < 0:
return False
d = (-1*b - sqrt(b**2- 4*a*c))/(2*a)
i.mult(d)
v = add(projector, i)
return v
def mirrorToDome(u):
u.sub(mirVect)
projector.sub(mirVect)
theta = angleBetween(u,projector)
n = u.copy()
n.setMag(2*projector.mag()*cos(theta))
k = sub(n,projector)
f = sub(k,u)
a = dot(f,f)
b = 2*dot(u,f) - 2*R*f.y
c = dot(u,u) - 2*R*u.y
if b**2- 4*a*c < 0:
print("unexpectect")
return False
d = (-1*b+sqrt(b**2-4*a*c))/(2*a)
f.mult(d)
v = add(u,f)
v.add(mirVect)
u.add(mirVect)
projector.add(mirVect)
return v
def domeToCircle(u):
# shift to origin
v = u.copy()
v.y = v.y - R
posZ = Point3D(0,0,1)
phi = angleBetween(v, posZ)
v.z = 0
v.setMag(2*R*phi/pi)
v.y = v.y + R
return v
def circleToPixel(u):
# shift to origin
v = u.copy()
v.y = v.y - R
# rotate about y-axis
l = v.copy()
# x y coords are now becoming pixel i, v
v.x = l.x*cos(-pi/2) - l.y*sin(-pi/2)
v.y = l.x*sin(-pi/2) + l.y*cos(-pi/2)
v.mult(max(w,h)/(2*R))
return v
def correctToDistorted(i, j):
return circleToPixel(domeToCircle(mirrorToDome(imagePlaneToMirror(pixelToImagePlane(i,j)))))
test = [(w//4,w//4), (w//4*3, w//4), (w//4,w//4*3), (w//4*3,w//4*3)]
for t in test:
v = circleToPixel(domeToCircle(mirrorToDome(imagePlaneToMirror(pixelToImagePlane(t[0],t[1])))))
show(v)
imgWarped = np.full([h,w,3],255,dtype=img.dtype)
imgCenter = correctToDistorted(w/2,h/2)
V = np.zeros((w,h))
J = np.arange(h)
U = np.zeros((w,h))
I = np.arange(w)
scale = 0.85
for j in J:
for i in I:
onMirror = imagePlaneToMirror(pixelToImagePlane(i,j))
if onMirror != False: #(onMirror.y < mirVect.y):
onDome = mirrorToDome(onMirror)
distorted = circleToPixel(domeToCircle(onDome))
distorted.sub(imgCenter)
distorted.mult(scale)
U[i][j] = int(distorted.y + w/2)
V[i][j] = int(distorted.x + h/2)
#if(disy >= 0 and disy < w and disx >= 0 and disx < h):
#imgWarped[j][i] = img[disx][disy]
Fi = interpolate.interp2d(J, I, U, kind='cubic')
Fj = interpolate.interp2d(J, I, V, kind='cubic')
for j in range(h):
for i in range(w):
v = int(V[i][j])
u = int(U[i][j])
if(u >= 0 and u < w and v >= 0 and v < h):
imgWarped[j][i] = img[v][u]
cv2.imshow('image',imgWarped)
cv2.waitKey(0)
cv2.destroyAllWindows()