-
Notifications
You must be signed in to change notification settings - Fork 32
/
pano_stitcher.py
179 lines (139 loc) · 5.76 KB
/
pano_stitcher.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
"""Project 1: Panorama stitching.
In this project, you'll stitch together images to form a panorama.
A shell of starter functions that already have tests is listed below.
TODO: Implement!
"""
import cv2
import numpy as np
from matplotlib import pyplot as plt
import math
def homography(image_a, image_b, bff_match=False):
"""Returns the homography mapping image_b into alignment with image_a.
Arguments:
image_a: A grayscale input image.
image_b: A second input image that overlaps with image_a.
Returns: the 3x3 perspective transformation matrix (aka homography)
mapping points in image_b to corresponding points in image_a.
"""
sift = cv2.SIFT(edgeThreshold=10, sigma=1.5, contrastThreshold=0.08)
kp_a, des_a = sift.detectAndCompute(image_a, None)
kp_b, des_b = sift.detectAndCompute(image_b, None)
# Brute force matching
bf = cv2.BFMatcher()
matches = bf.knnMatch(des_a, trainDescriptors=des_b, k=2)
# Lowes Ratio
good_matches = []
for m, n in matches:
if m.distance < .75 * n.distance:
good_matches.append(m)
src_pts = np.float32([kp_a[m.queryIdx].pt for m in good_matches])\
.reshape(-1, 1, 2)
dst_pts = np.float32([kp_b[m.trainIdx].pt for m in good_matches])\
.reshape(-1, 1, 2)
if len(src_pts) > 4:
M, mask = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5)
else:
M = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
return M
def warp_image(image, homography):
"""Warps 'image' by 'homography'
Arguments:
image: a 3-channel image to be warped.
homography: a 3x3 perspective projection matrix mapping points
in the frame of 'image' to a target frame.
Returns:
- a new 4-channel image containing the warped input, resized to contain
the new image's bounds. Translation is offset so the image fits exactly
within the bounds of the image. The fourth channel is an alpha channel
which is zero anywhere that the warped input image does not map in the
output, i.e. empty pixels.
- an (x, y) tuple containing location of the warped image's upper-left
corner in the target space of 'homography', which accounts for any
offset translation component of the homography.
"""
image = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
h, w, z = image.shape
# Find min and max x, y of new image
p = np.array([[0, w, w, 0], [0, 0, h, h], [1, 1, 1, 1]])
p_prime = np.dot(homography, p)
yrow = p_prime[1] / p_prime[2]
xrow = p_prime[0] / p_prime[2]
ymin = min(yrow)
xmin = min(xrow)
ymax = max(yrow)
xmax = max(xrow)
# Make new matrix that removes offset and multiply by homography
new_mat = np.array([[1, 0, -1 * xmin], [0, 1, -1 * ymin], [0, 0, 1]])
homography = np.dot(new_mat, homography)
# height and width of new image frame
height = int(round(ymax - ymin))
width = int(round(xmax - xmin))
size = (width, height)
# Do the warp
warped = cv2.warpPerspective(src=image, M=homography, dsize=size)
return warped, (int(xmin), int(ymin))
def create_mosaic(images, origins):
"""Combine multiple images into a mosaic.
Arguments:
images: a list of 4-channel images to combine in the mosaic.
origins: a list of the locations upper-left corner of each image in
a common frame, e.g. the frame of a central image.
Returns: a new 4-channel mosaic combining all of the input images. pixels
in the mosaic not covered by any input image should have their
alpha channel set to zero.
"""
# find central image
for i in range(0, len(origins)):
if origins[i] == (0, 0):
central_index = i
break
central_image = images[central_index]
central_origin = origins[central_index]
# zip origins and images together
zipped = zip(origins, images)
# sort by distance from origin (highest to lowest)
func = lambda x: math.sqrt(x[0][0] ** 2 + x[0][1] ** 2)
dist_sorted = sorted(zipped, key=func, reverse=True)
# sort by x value
x_sorted = sorted(zipped, key=lambda x: x[0][0])
# sort by y value
y_sorted = sorted(zipped, key=lambda x: x[0][1])
# determine the coordinates in the new frame of the central image
if x_sorted[0][0][0] > 0:
cent_x = 0 # leftmost image is central image
else:
cent_x = abs(x_sorted[0][0][0])
if y_sorted[0][0][1] > 0:
cent_y = 0 # topmost image is central image
else:
cent_y = abs(y_sorted[0][0][1])
# make a new list of the starting points in new frame of each image
spots = []
for origin in origins:
spots.append((origin[0]+cent_x, origin[1] + cent_y))
zipped = zip(spots, images)
# get height and width of new frame
total_height = 0
total_width = 0
for spot, image in zipped:
total_width = max(total_width, spot[0]+image.shape[1])
total_height = max(total_height, spot[1]+image.shape[0])
# print "height ", total_height
# print "width ", total_width
# new frame of panorama
stitch = np.zeros((total_height, total_width, 4), np.uint8)
# stitch images into frame by order of distance
for image in dist_sorted:
# offset_y = image[0][1] + cent_y
# offset_x = image[0][0] + cent_x
# for i in range(0, image[1].shape[0]):
# for j in range(0, image[1].shape[1]):
# # print i, j
# if image[1][i][j][3] != 0 :
# stitch[i+offset_y][j+offset_x][:4] = image[1][i][j]
offset_y = image[0][1] + cent_y
offset_x = image[0][0] + cent_x
end_y = offset_y + image[1].shape[0]
end_x = offset_x + image[1].shape[1]
stitch[offset_y:end_y, offset_x:end_x, :4] = image[1]
return stitch