Выравнивание / Наложение двух изображений в Python автоматически с использованием OpenCV / PIL - PullRequest
0 голосов
/ 28 октября 2019

Мне нужно сделать выходное изображение, объединяющее два изображения (изображения ниже) одного и того же объекта, снятые двумя разными устройствами. Для этого я думал, что использование гомографической композиции было бы идеально. Для выравнивания этих двух изображений я сделал:

import cv2 
import numpy as np 

# Open the image files. 
img1_color = cv2.imread("oct.jpg")  # Image to be aligned. 
img2_color = cv2.imread("MP.jpg")    # Reference image. 

# Convert to grayscale. 
img1 = cv2.cvtColor(img1_color, cv2.COLOR_BGR2GRAY) 
img2 = cv2.cvtColor(img2_color, cv2.COLOR_BGR2GRAY) 
height, width = img2.shape 

# Create ORB detector with 5000 features. 
orb_detector = cv2.ORB_create(5000) 

# Find keypoints and descriptors. 
# The first arg is the image, second arg is the mask 
#  (which is not reqiured in this case). 
kp1, d1 = orb_detector.detectAndCompute(img1, None) 
kp2, d2 = orb_detector.detectAndCompute(img2, None) 

# Match features between the two images. 
# We create a Brute Force matcher with  
# Hamming distance as measurement mode. 
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True) 

# Match the two sets of descriptors. 
matches = matcher.match(d1, d2) 

# Sort matches on the basis of their Hamming distance. 
matches.sort(key = lambda x: x.distance) 

# Take the top 90 % matches forward. 
matches = matches[:int(len(matches)*90)] 
no_of_matches = len(matches) 

# Define empty matrices of shape no_of_matches * 2. 
p1 = np.zeros((no_of_matches, 2)) 
p2 = np.zeros((no_of_matches, 2)) 

for i in range(len(matches)): 
  p1[i, :] = kp1[matches[i].queryIdx].pt 
  p2[i, :] = kp2[matches[i].trainIdx].pt 

# Find the homography matrix. 
homography, mask = cv2.findHomography(p1, p2, cv2.RANSAC) 

# Use this matrix to transform the 
# colored image wrt the reference image. 
transformed_img = cv2.warpPerspective(img1_color, 
                    homography, (width, height)) 

# Save the output. 
cv2.imwrite('output.jpg', transformed_img) 

, но код выдал следующий вывод вместо желаемого вывода: False Output

Изображение 1: Picture 1 Изображение 2: Picture 2

Выходные данные должны быть комбинацией 1 и 2 и должны выглядеть следующим образом: Desired Output

Источник изображения

...