mirArnold
mirArnold

Reputation: 117

OpenCV Image Alignment gone wrong

I want to align this image

enter image description here

so that i looks like this:

enter image description here

To do so, I use openCV to identify the keypoints in both images. After that, the matching keypoints are brought together and with the warpPerspective-function of openCV I want to align the rotated one, properly. At the end, both images should look like the same.

import cv2
import numpy as np
from matplotlib import pyplot as plt
import os

original_img = r"C:\car.png"
rotated_img = r"C:\car_rotated.png"

# Read imgs:
img_original = cv2.imread(original_img, cv2.IMREAD_COLOR)
img_original = cv2.cvtColor(img_original, cv2.COLOR_BGR2RGB)

img_rotated = cv2.imread(rotated_img, cv2.IMREAD_COLOR)
img_rotated = cv2.cvtColor(img_rotated, cv2.COLOR_BGR2RGB)

# convert to grayscale:
img_original_gray = cv2.cvtColor(img_original, cv2.COLOR_BGR2GRAY)
img_rotated_gray = cv2.cvtColor(img_rotated, cv2.COLOR_BGR2GRAY)


# Identify KeyPoints:
MAX_NUM_FEATURES = 500
orb = cv2.ORB_create(MAX_NUM_FEATURES)

keypoints1, descriptors1 = orb.detectAndCompute(img_original_gray, None)
keypoints2, descriptors2 = orb.detectAndCompute(img_rotated_gray, None)

# Create a Brute Force Matcher object.
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_BRUTEFORCE_HAMMING)
matches = matcher.match(descriptors1, descriptors2, None)

# Matches sort:
#matches.sort(key=lambda x: x.distance, reverse=False)

# The matches with shorter distance are the ones we want.
matches = sorted(matches, key = lambda x : x.distance)

# Remove not so good matches
numGoodMatches = int(len(matches)*0.1)
matches = matches[:numGoodMatches]


# Perform Homographie
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.queryIdx].pt
    
# Homographie
h, mask = cv2.findHomography(points2, points1, cv2.RANSAC)

height, width, channels = img_original.shape
im2_reg = cv2.warpPerspective(img_rotated, h, (width, height))

The output is the following:

enter image description here

Why the strange output?

Upvotes: 1

Views: 261

Answers (0)

Related Questions