-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_allign.py
139 lines (120 loc) · 4.62 KB
/
image_allign.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
from __future__ import print_function
import cv2
import numpy as np
import matplotlib.pyplot as plt
def get_images_by_frames(img_path: str, frames: list):
images = []
for frame in frames:
file = img_path + f"buspas_2_lane_3_1_{frame}.jpg"
images.append(cv2.imread(file, cv2.IMREAD_COLOR))
return images
def get_keypoints_and_matches(
images: list,
max_features: int = 500,
good_matches_percent: float = 0.5,
edge_threshold: int = 1,
matching_with_first: bool = True,
show_matches: bool = True,
):
"""Find keypoint in every img than find matches beetwen images
:param matching_with_first: If true all images are matching its keypoints. If False every image matches with the next one, defaults to True
:type matching_with_first: bool, optional
:return: _description_
:rtype: _type_
"""
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_SL2)
orb = cv2.ORB_create(max_features, 1, edgeThreshold=edge_threshold, patchSize=20)
# Detect keypoints
keypoints = []
descriptors = []
matches = []
for img in images:
im_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
one_img_keypoints, one_img_descriptors = orb.detectAndCompute(im_gray, None)
keypoints.append(one_img_keypoints)
descriptors.append(one_img_descriptors)
# Match features
for i in range(1, len(descriptors)):
if matching_with_first:
one_img_matches = list(matcher.match(descriptors[i], descriptors[0], None))
else:
one_img_matches = list(
matcher.match(descriptors[i], descriptors[i - 1], None)
)
# Sort matches by score
one_img_matches.sort(key=lambda x: x.distance)
numGoodMatches = int(len(one_img_matches) * good_matches_percent)
one_img_matches = one_img_matches[:numGoodMatches]
matches.append(one_img_matches)
# Draw top matches from the current and next image
if show_matches:
if matching_with_first:
print(
f"number of matches between img_{i} and img_{0}-> {len(one_img_matches)}"
)
imMatches = cv2.drawMatches(
images[i],
keypoints[i],
images[0],
keypoints[0],
one_img_matches,
None,
)
plt.imshow(imMatches)
plt.show()
else:
print(
f"number of matches between img_{i} and img_{i-1}-> {len(one_img_matches)}"
)
imMatches = cv2.drawMatches(
images[i],
keypoints[i],
images[i - 1],
keypoints[i - 1],
one_img_matches,
None,
)
plt.imshow(imMatches)
plt.show()
return keypoints, matches
def get_homography_matrix(keypoints, matches):
homography_matrix = np.zeros((3, 3))
# Extract location of matches
for i in range(len(keypoints) - 1):
if len(matches[i]) < 1:
continue
else:
points1 = np.zeros((len(matches[i]), 2), dtype=np.float32)
points2 = np.zeros((len(matches[i]), 2), dtype=np.float32)
for u, match in enumerate(matches[i]):
points1[u] = keypoints[i][match.trainIdx].pt
points2[u] = keypoints[i + 1][match.queryIdx].pt
# Find homography between current and next image
if points1.shape[0] >= 4 or points2.shape[0] >= 4:
homography_matrix = np.zeros((3, 3))
h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)
homography_matrix += h
else:
print(
"you need at least 4 coresponing points to calculate matrix. Probably you have too few matches"
)
mean_h_mat = homography_matrix / len(keypoints)
return mean_h_mat
def transform_image(
image,
homography_matrix,
show_image: bool = False,
save_path=None,
):
(h, w) = image.shape[:2]
aligned = cv2.warpPerspective(image, homography_matrix, (w, h))
if show_image:
plt.imshow(aligned)
plt.title("aligned")
plt.show()
plt.imshow(image)
plt.title("original")
plt.show()
if save_path:
cv2.imwrite(save_path, aligned)
return aligned