gpt4 book ai didi

Python:OpenCV findHomography 输入

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:26 25 4
gpt4 key购买 nike

我试图在 Python 中使用 opencv 为 rgbrotated 找到两个图像的单应矩阵:

print(rgb.shape, rotated.shape)
H = cv2.findHomography(rgb, rotated)
print(H)

我得到的错误是

(1080, 1920, 3) (1080, 1920, 3)
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-37-26874dc47f1f> in <module>()
1 print(rgb.shape, rotated.shape)
----> 2 H = cv2.findHomography(rgb, rotated)
3 print(H)

error: OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\calib3d\src\fundam.cpp:372: error: (-5) The input arrays should be 2D or 3D point sets in function cv::findHomography

我还尝试使用 cv2.findHomography(rgb[:,:,0], rotated[:,:,0]) 来查看 channel 或 channel 顺序是否导致任何问题,但是它甚至不适用于 2D 矩阵。

输入应该如何?

最佳答案

cv2.findHomography() 不接收两个图像并返回 H

如果您需要为两个 RGB 图像找到 H 作为 np.arrays:

import numpy as np
import cv2

def findHomography(img1, img2):

# define constants
MIN_MATCH_COUNT = 10
MIN_DIST_THRESHOLD = 0.7
RANSAC_REPROJ_THRESHOLD = 5.0

# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)

# find matches
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)

flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)

# store all the good matches as per Lowe's ratio test.
good = []
for m, n in matches:
if m.distance < MIN_DIST_THRESHOLD * n.distance:
good.append(m)


if len(good) > MIN_MATCH_COUNT:
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

H, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, RANSAC_REPROJ_THRESHOLD)
return H

else: raise Exception("Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT))

注意:

  • Python 3OpenCV 3.4 上测试
  • 您需要 opencv-contrib-python 包,因为 SIFT 存在专利问题并且已从 opencv-python 中删除
  • 这给出了用于转换 img1 的 H 矩阵并将其重叠在 img2 上。如果你想知道如何做到这一点,那就是 here

关于Python:OpenCV findHomography 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50945385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com