-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectify.py
200 lines (165 loc) · 7.56 KB
/
rectify.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import cv2 as cv2
import numpy as np
from scipy.spatial import Delaunay
import matplotlib.pyplot as plt
def rectify(img_gray, corners):
desired_corners = np.array([[0,0],[128, 0], [128, 128] ,[0, 128]], dtype=np.float32)
H = find_svd(corners, desired_corners)
shape = [128, 128]
rect_img = getPerspectiveTransform(img_gray,H, shape)
return rect_img
def warp_lena(img, img_lena, corners):
desired_corners = np.array([[0,0],[img_lena.shape[0], 0], [img_lena.shape[0], img_lena.shape[0]] ,[0, img_lena.shape[0]]], dtype=np.float32)
H = find_svd(corners, desired_corners)
shape = [img_lena.shape[0],img_lena.shape[0]]
warped_img = getPerspectiveTransform_Lena(img,img_lena, H, shape)
return warped_img
def find_cube_pts(img, reqdPts, corners, shape, calib):
desired_corners = np.array([[0, 0],[0, shape[1]],[shape[0], shape[1]], [shape[0], 0]])
H = find_svd(corners, desired_corners)
H = np.linalg.inv(H)
H = H/H[2,2]
E = np.zeros([3, 4])
calib_inv = np.linalg.inv(calib)
E_ = np.matmul(calib_inv, H)
lamda = (np.linalg.norm(np.matmul(calib_inv, H[:, 0])) + np.linalg.norm(np.matmul(calib_inv, H[:, 1])))/2
B = np.linalg.det(E_)
if B < 0:
E_ = -E_
E_ = E_/lamda
E[:,0] = (E_[:,0]/lamda).T
E[:,1] = (E_[:,1]/lamda).T
E[:,2] = (np.cross(E[:,0], E[:,1])*lamda).T
E[:,3] = (E_[:,2]/lamda).T
E = E[:]/E[2,3]
imgPts = np.matmul(calib,np.matmul(E,reqdPts.T))
return imgPts
def find_svd(c1,c2):
[x1,y1],[x2,y2],[x3,y3],[x4,y4] = c2
[xp1, yp1], [xp2, yp2], [xp3, yp3], [xp4, yp4] = c1
A = np.array([[-x1,-y1,-1,0,0,0,x1*xp1,y1*xp1,xp1],[0,0,0,-x1,-y1,-1,x1*yp1,y1*yp1,yp1],[-x2,-y2,-1,0,0,0,x2*xp2,y2*xp2,xp2],\
[0,0,0,-x2,-y2,-1,x2*yp2,y2*yp2,yp2],[-x3,-y3,-1,0,0,0,x3*xp3,y3*xp3,xp3],[0,0,0,-x3,-y3,-1,x3*yp3,y3*yp3,yp3],\
[-x4,-y4,-1,0,0,0,x4*xp4,y4*xp4,xp4],[0,0,0,-x4,-y4,-1,x4*yp4,y4*yp4,yp4]], dtype=np.float32)
A_trans = A.transpose()
A_prod = np.dot(A_trans,A)
w,v = np.linalg.eig(A_prod)
H = v[:,-1]
H = np.reshape(H,(3,3))
H = H/H[2,2]
if abs(np.linalg.det(H)) < 0.0001:
return H
H = np.linalg.inv(H)
H = H/H[2,2]
return H
def getPerspectiveTransform(img, H, shape):
Hinv = np.linalg.inv(H)
Hinv = Hinv/Hinv[2,2]
rect_img = np.zeros((shape[0], shape[1], 1))
img_ = img.astype(np.float32)
counter=0
for i in range(shape[0]): # x? to change
for j in range(shape[1]): #y?
[x, y, z] = np.dot(Hinv, np.transpose([j, i, 1]))
x = x/z
y = y/z
counter+=1
if (type(x) != np.float64) or (type(y)!= np.float64):
# print(1)
continue
if (x < 1919 and y < 1079 and x >= 0 and y >= 0):
rect_img[i,j] = (img_[int(np.floor(y)),int(np.floor(x))] + img_[int(np.floor(y)),int(np.ceil(x))]
+ img_[int(np.ceil(y)), int(np.ceil(x))]+ img_[int(np.ceil(y)) , int(np.floor(x))])/4.0
return rect_img
def getPerspectiveTransform_Lena(img, img_lena, H, shape):
Hinv = np.linalg.inv(H)
Hinv = Hinv/Hinv[2,2]
img_ = np.zeros((img.shape[0], img.shape[1], 4))
img_[:,:,0:3] = img
for i in range(shape[0]): # x? to change
for j in range(shape[1]): #y?
[x, y, z] = np.dot(Hinv, np.transpose([j, i, 1]))
x = x/z
y = y/z
#print(x, y)
index_x = [int(np.floor(y)), int(np.floor(y)), int(np.ceil(y)), int(np.ceil(y))]
index_y = [int(np.floor(x)), int(np.floor(x)), int(np.ceil(x)), int(np.ceil(x))]
if(x < 1920 and y < 1080 and x>=0 and y>=0):
img_[int(np.floor(y)), int(np.floor(x)), 0:3] = (img_[int(np.floor(y)), int(np.floor(x)), 0:3]*img_[int(np.floor(y)), int(np.floor(x)), 3]
+ img_lena[i,j,0:3])/(img_[int(np.floor(y)), int(np.floor(x)), 3] + 1)
img_[int(np.floor(y)), int(np.floor(x)), 3] += 1
return img_[:,:,0:3].astype(np.uint8)
def orient_img(img):
scale = 5
scaleEnd = -3
num_rot = 0
img_ = np.asarray(img[scale:scaleEnd, scale:scaleEnd]).astype(np.int32)
inds = np.where(img_>= np.max(img_) - 15)
xmin = np.min(inds[0])+scale
xmax = np.max(inds[0])+scale
ymin = np.min(inds[1])+scale
ymax = np.max(inds[1])+scale
topLeft = [xmin, ymin]
topRight = [xmin, ymax]
bottomLeft = [xmax, ymin]
bottomRight = [xmax, ymax]
keypts = np.array([np.add(bottomRight, 0.1*np.add(topLeft, np.multiply(-1,bottomRight))), np.add(topRight,0.1*np.add(bottomLeft,np.multiply(-1,topRight))) ,
np.add(topLeft,0.1*np.add(bottomRight,np.multiply(-1,topLeft))),np.add(bottomLeft, 0.1*np.add(topRight, np.multiply(-1,bottomLeft))) ])
for i in range(len(keypts)):
if img[int(keypts[i,0])][int(keypts[i,1])] >=230 :
for k in range(i):
img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
num_rot = i
return num_rot, img
def find_id(img):
sizex = img.shape[0]
sizey = img.shape[1]
scale = 5
scaleEnd = -3
img_ = np.asarray(img[scale:scaleEnd, scale:scaleEnd]).astype(np.int32)
inds = np.where(img_>= np.max(img_) - 15)
xmin = np.min(inds[0])+scale
xmax = np.max(inds[0])+scale
ymin = np.min(inds[1])+scale
ymax = np.max(inds[1])+scale
topLeft = [xmin, ymin]
topRight = [xmin, ymax]
bottomLeft = [xmax, ymin]
bottomRight = [xmax, ymax]
keypts = np.array([np.add(bottomLeft, 0.375*np.add(topRight, np.multiply(-1,bottomLeft))),np.add(bottomRight, 0.375*np.add(topLeft, np.multiply(-1,bottomRight))),
np.add(topRight,0.375*np.add(bottomLeft,np.multiply(-1,topRight))),np.add(topLeft,0.375*np.add(bottomRight,np.multiply(-1,topLeft)))])
id = 0
cv2.rectangle(img,(ymin,xmin),(ymax,xmax),(0,255,0),thickness=1)
for i in range(len(keypts)):
if(img[int(keypts[i][0])][int(keypts[i][1])] >245):
id = (id << 1) | int('00000001', 2)
else:
id = (id << 1) | int('00000000', 2)
return id
def draw_cubes(img, corners, imgPts):
for i in range(corners.shape[0]):
cv2.line(img, tuple(corners[i%4]),tuple(corners[(i+1)%4]),(0,255,255),3)
cv2.line(img, tuple(imgPts[0:2, i%4].astype(np.int32)),tuple(imgPts[0:2, (i+1)%4].astype(np.int32)),(0,255,255),3)
cv2.line(img, tuple(corners[i%4]),tuple([int(imgPts[0,i%4]),int(imgPts[1,i%4])]),(255,0,0),3)
def in_hull(p, hull):
if not isinstance(hull,Delaunay):
hull = Delaunay(hull)
res = hull.find_simplex(p)>=0
# print(res)
return res
if __name__=="__main__":
index = [44, 250, 399]
corners = [
np.array([[1145, 567], [1074, 598], [1033, 537], [1104, 508]], dtype=np.float32),
np.array([[1099, 625], [1037, 642], [1004, 582], [1067, 566]], dtype=np.float32),
np.array([[1158, 540], [1134, 597], [1057, 558], [1086, 498]], dtype=np.float32)
]
for i in range(len(index)):
img = cv2.imread("VideoFrames/vid"+ str(index[i])+".jpg")
imgCorner = corners[i]
rect_img = rectify(img, imgCorner)
cv2.imshow("rectified image", rect_img)
oriented_image = orient_img(rect_img)
cv2.imshow("oriented image", oriented_image)
id = find_id(oriented_image)
print (id)
cv2.waitKey(0)