-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPanorama_stitch.py
213 lines (170 loc) · 7.01 KB
/
Panorama_stitch.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
201
202
203
204
205
206
207
208
209
210
211
212
213
import numpy as np
from PIL import Image
import matplotlib
import matplotlib.image as image
import matplotlib.pyplot as plt
from skimage.feature import (corner_harris, corner_peaks, plot_matches, BRIEF, match_descriptors)
from skimage.transform import warp, ProjectiveTransform
from skimage.color import rgb2gray
from skimage.measure import ransac
import os
folder = os.path.dirname(os.path.abspath(__file__))
# join the paths
matplot = os.path.join(folder, 'matplot')
if not os.path.exists('matplot'):
os.makedirs('matplot')
print(folder)
print(matplot)
def blankImage(imL):
imLalpha = imL.copy()
imLalpha[:,:,0] = 0
imLalpha[:,:,1] = 0
imLalpha[:,:,2] = 0
return imLalpha
def calculateAlpha(dtransP, dtransS):
alpha =dtransP[:] / (dtransP[:] + dtransS[:])
m = np.isnan(alpha) # mask of NaNs
alpha[m] = 0
return alpha
def applyAlpha(image,alpha):
imagealpha = image.copy()
imagealpha[:,:,0] = np.multiply(alpha,image[:,:,0])
imagealpha[:,:,1] = np.multiply(alpha,image[:,:,1])
imagealpha[:,:,2] = np.multiply(alpha,image[:,:,2])
return imagealpha
def calculate_dtrans(image):
length = image.shape[1]
height = image.shape[0]
a = np.fromfunction(lambda i, j: i,
(height, length),
dtype=int)
b = np.fromfunction(lambda i, j: j,
(height, length),
dtype=int)
c = height - 1 + np.fromfunction(lambda i, j: -i,
(height, length),
dtype=int)
d = length - 1 + np.fromfunction(lambda i, j: -j,
(height, length),
dtype=int)
# got no idea how to merge the 4 matricies please look at piazza @20
mask = np.minimum(np.minimum(a, b), np.minimum(c, d)) + 1
return mask / np.max(mask)
# read "images/CMU_left.jpg" in pillow format
imL = image.imread("images/CMU_left.jpg")
imR = image.imread("images/CMU_right.jpg")
imLgray = rgb2gray(imL)
imRgray = rgb2gray(imR)
# NOTE: corner_peaks and many other feature extraction functions return point coordinates as (y,x), that is (rows,cols)
keypointsL = corner_peaks(corner_harris(imLgray), threshold_rel=0.0005, min_distance=5)
keypointsR = corner_peaks(corner_harris(imRgray), threshold_rel=0.0005, min_distance=5)
extractor = BRIEF()
extractor.extract(imLgray, keypointsL)
keypointsL = keypointsL[extractor.mask]
descriptorsL = extractor.descriptors
extractor.extract(imRgray, keypointsR)
keypointsR = keypointsR[extractor.mask]
descriptorsR = extractor.descriptors
matchesLR = match_descriptors(descriptorsL, descriptorsR, cross_check=True)
print ('the number of matches is {:2d}'.format(matchesLR.shape[0]))
fig = plt.figure(1,figsize = (12, 4))
axA = plt.subplot(111)
plt.gray()
plot_matches(axA, imL, imR, keypointsL, keypointsR, matchesLR) #, matches_color = 'r')
axA.axis('off')
# save the plt as an image
# if matplot folder does not exist, create it
# fig.savefig('matplot/P1.1-PLOT-matches-all.jpg', dpi=300)
# use os.path.join to join the paths
# give title to the plt
plt.title("matches between the left and right images")
fig.savefig(os.path.join(matplot, 'P1.1-PLOT-matches-all.jpg'), dpi=300)
enlargedL = blankImage(np.resize(imR,(imR.shape[0], imR.shape[1] * 2, 3)))
enlargedL[:,:imR.shape[1] ,0] = imL[:,:,0]
enlargedL[:,:imR.shape[1] ,1] = imL[:,:,1]
enlargedL[:,:imR.shape[1] ,2] = imL[:,:,2]
enlargedR = blankImage(np.resize(imR,(imR.shape[0] , imR.shape[1] * 2, 3)))
enlargedR[:,:imR.shape[1] ,0] = imR[:,:,0]
enlargedR[:,:imR.shape[1] ,1] = imR[:,:,1]
enlargedR[:,:imR.shape[1] ,2] = imR[:,:,2]
dst = np.roll( keypointsL[matchesLR[:,0]], 1, axis = 1)
src = np.roll( keypointsR[matchesLR[:,1]], 1, axis = 1)
#src = src + imL.shape[1]*np.column_stack((np.zeros(len(src)),np.ones(len(src))))
model_robust, inliers = ransac((src, dst), ProjectiveTransform, min_samples=4, residual_threshold=1, max_trials=1000000)
fig = plt.figure(2,figsize = (12, 4))
axA = plt.subplot(111)
plt.gray()
plot_matches(axA, imL, imR, keypointsL, keypointsR, matchesLR[inliers]) #, matches_color = 'r')
axA.axis('off')
plt.title("matches between the left and right images (inliers)")
# fig.savefig('matplot/P2.1-PLOT-matches-inliers.jpg', dpi=300)
fig.savefig(os.path.join(matplot, 'P2.1-PLOT-matches-inliers.jpg'), dpi=300)
fig = plt.figure(3,figsize = (12, 14))
plt.subplot(311)
plt.imshow(enlargedL)
plt.title("reference frame with the left image")
plt.subplot(312)
transformedRight = warp(enlargedR, model_robust.inverse)
transformedRight = (transformedRight * 255).astype(int)
plt.imshow(transformedRight)
plt.title("reference frame with the right image (reprojected)")
plt.subplot(313)
model_bad = ProjectiveTransform()
model_bad.estimate(src, dst)
plt.imshow( warp(enlargedR, model_bad.inverse))
plt.title("reference frame with the right image (reprojected badly)")
# fig.savefig('matplot/P1.2-PLOT-reprojected-matches-all.jpg', dpi=300)
fig.savefig(os.path.join(matplot, 'P1.2-PLOT-reprojected-matches-all.jpg'), dpi=300)
enlargedR_1 = blankImage(np.resize(imR,(imR.shape[0] , imR.shape[1] * 2, 3)))
enlargedR_1[:,:imR.shape[1] ,0] = imR[:,:,0]
enlargedR_1[:,:imR.shape[1] ,1] = imR[:,:,1]
enlargedR_1[:,:imR.shape[1] ,2] = imR[:,:,2]
fig = plt.figure(4,figsize = (12, 3))
plt.subplot(121)
plt.title("dtrans1 in Ref. frame (LdtRef)")
imL_dtrans =np.zeros((imL.shape[0] , imL.shape[1] * 2))
imL_dtrans[:,:imL.shape[1]] = calculate_dtrans(imL)
plt.imshow(imL_dtrans)
plt.subplot(122)
plt.title("dtrans2 in Ref. frame (RdtRef)")
imR_dtrans = imL_dtrans.copy()
imR_dtrans = warp(imR_dtrans, model_robust.inverse)
#plt.imshow(img_2_alpha)
plt.imshow( imR_dtrans)
# fig.savefig('matplot/APPDX-1.1-dtrans1-dtrans2.jpg', dpi=300)
fig.savefig(os.path.join(matplot, 'APPDX-1.1-dtrans1-dtrans2.jpg'), dpi=300)
alphaL = calculateAlpha(imL_dtrans,imR_dtrans)
alphaR = calculateAlpha(imR_dtrans,imL_dtrans)
fig = plt.figure(4,figsize = (12, 3))
plt.subplot(121)
plt.title("alpha1 in Ref. frame (LdtRef)")
plt.imshow(alphaL )
plt.subplot(122)
plt.title("alpha2 in Ref. frame (RdtRef)")
plt.imshow(alphaR)
# fig.savefig('matplot/APPDX-1.2-alpha1-alpha2.jpg', dpi=300)
fig.savefig(os.path.join(matplot, 'APPDX-1.2-alpha1-alpha2.jpg'), dpi=300)
fig = plt.figure(5, figsize=(12, 14))
plt.subplot(311)
img_1 = applyAlpha(enlargedL, alphaL)
plt.imshow(img_1)
plt.title("alpha based on DT (for RransacRef)")
plt.subplot(312)
# plt.imshow(...)
img_2R = applyAlpha(transformedRight, alphaR)
plt.imshow(img_2R)
plt.title("alpha based on DT (for RransacRef)")
plt.subplot(313)
Panorama = img_2R + img_1
plt.imshow(Panorama)
plt.title("Panorama")
# plt.show()
# fig.savefig('matplot/P2.2-PLOT-reprojected-matches-inliers.jpg', dpi=300)
fig.savefig(os.path.join(matplot, 'P2.2-PLOT-reprojected-matches-inliers.jpg'), dpi=300)
# save the Panorama as an image
# if Panorama folder does not exist, create it
if not os.path.exists('Panorama'):
os.makedirs('Panorama')
# use pillow to save the image
Panorama = Image.fromarray(Panorama.astype(np.uint8))
Panorama.save('Panorama/Panorama.jpg')