python - 特征对齐 ORB 过程失败
问题描述
我正在尝试对齐无人机拍摄的几张图像,结合热成像和 RGB 相机。在做 ORB 对齐和计算单应矩阵之后,我调用warpPerspective方法,结果总是一个模糊的图像。我尝试了数百张图片,结果几乎相同。
我尝试将 MAX_FEATURES 提高到 1000、10000、10000,结果始终相同。
为什么warpPerspective会产生这种结果的任何想法?也许单应矩阵是错误的?
先感谢您
from __future__ import print_function
import cv2
import numpy as np
MAX_FEATURES = 500
GOOD_MATCH_PERCENT = 0.15
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_FEATURES)
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
if __name__ == '__main__':
# Read reference image
refFilename = "form.jpg"
print("Reading reference image : ", refFilename)
imReference = cv2.imread(refFilename, cv2.IMREAD_COLOR)
# Read image to be aligned
imFilename = "scanned-form.jpg"
print("Reading image to align : ", imFilename);
im = cv2.imread(imFilename, cv2.IMREAD_COLOR)
print("Aligning images ...")
# Registered image will be resotred in imReg.
# The estimated homography will be stored in h.
imReg, h = alignImages(im, imReference)
# Write aligned image to disk.
outFilename = "aligned.jpg"
print("Saving aligned image : ", outFilename);
cv2.imwrite(outFilename, imReg)
# Print estimated homography
print("Estimated homography : \n", h)
解决方案
推荐阅读
- c# - 如何在 C# WPF 中从 ListView/XML 中完全删除一个项目?
- python - Numpy 数组未更新
- java - Spring Request 处理失败;嵌套异常是 java.lang.NoSuchMethodException:
- python - Pycron 不仅在控制台启动时运行
- react-native - Expo + vagrant,metro 捆绑包不起作用
- javascript - 试图在函数外传递变量
- python - ','运算符在python中代表什么?
- django - 如何使用通配符作为键/对象在 django 中生成 aws 预签名 url
- sql - 我应该将实际数据和预测数据分成 2 个表吗?
- tcp - Erlang TCP 服务器总是在接受时关闭