https://learnopencv.com/feature-based-image-alignment-using-opencv-c-python/
def alignImages(im1, im2):
# Convert images to grayscale
im1Gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
im2Gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
# Detect ORB features and compute descriptors.
orb = cv2.ORB_create(MAX_MATCHES)
keypoints1, descriptors1 = orb.detectAndCompute(im1Gray, None)
keypoints2, descriptors2 = orb.detectAndCompute(im2Gray, None)
# Match features.
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(descriptors1, descriptors2, None)
# Sort matches by score
matches.sort(key=lambda x: x.distance, reverse=False)
# Remove not so good matches
numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
matches = matches[:numGoodMatches]
# Draw top matches
imMatches = cv2.drawMatches(im1, keypoints1, im2, keypoints2, matches, None)
cv2.imwrite("matches.jpg", imMatches)
# Extract location of good matches
points1 = np.zeros((len(matches), 2), dtype=np.float32)
points2 = np.zeros((len(matches), 2), dtype=np.float32)
for i, match in enumerate(matches):
points1[i, :] = keypoints1[match.queryIdx].pt
points2[i, :] = keypoints2[match.trainIdx].pt
# Find homography
h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)
# Use homography
height, width, channels = im2.shape
im1Reg = cv2.warpPerspective(im1, h, (width, height))
return im1Reg, h
As mentioned earlier, a homography is nothing but a
3
×
3
3 \times 3
3×3 matrix as shown below.
H
=
[
h
00
h
01
h
02
h
10
h
11
h
12
h
20
h
21
h
22
]
H=\left[\begin{array}{lll} h_{00} & h_{01} & h_{02} \\ h_{10} & h_{11} & h_{12} \\ h_{20} & h_{21} & h_{22} \end{array}\right]
H=⎣⎡h00h10h20h01h11h21h02h12h22⎦⎤
Let
(
x
1
,
y
1
)
\left(x_{1}, y_{1}\right)
(x1,y1) be a point in the first image and
(
x
2
,
y
2
)
\left(x_{2}, y_{2}\right)
(x2,y2) be the coordinates of the same physical point in the second image. Then, the Homography
H
H
H relates them in the following way
[
x
1
y
1
1
]
=
H
[
x
2
y
2
1
]
=
[
h
00
h
01
h
02
h
10
h
11
h
12
h
20
h
21
h
22
]
[
x
2
y
2
1
]
\left[\begin{array}{c} x_{1} \\ y_{1} \\ 1 \end{array}\right]=H\left[\begin{array}{c} x_{2} \\ y_{2} \\ 1 \end{array}\right]=\left[\begin{array}{lll} h_{00} & h_{01} & h_{02} \\ h_{10} & h_{11} & h_{12} \\ h_{20} & h_{21} & h_{22} \end{array}\right]\left[\begin{array}{c} x_{2} \\ y_{2} \\ 1 \end{array}\right]
⎣⎡x1y11⎦⎤=H⎣⎡x2y21⎦⎤=⎣⎡h00h10h20h01h11h21h02h12h22⎦⎤⎣⎡x2y21⎦⎤
If we knew the homography, we could apply it to all the pixels of one image to obtain a warped image that is aligned with the second image.