首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python: OpenCV findHomography输入

Python: OpenCV findHomography输入
EN

Stack Overflow用户
提问于 2018-06-20 09:56:28
回答 1查看 9K关注 0票数 3

我试图用Python中的opencv找到两个图像rgbrotated的同形矩阵:

代码语言:javascript
运行
复制
print(rgb.shape, rotated.shape)
H = cv2.findHomography(rgb, rotated)
print(H)

我得到的错误是

代码语言:javascript
运行
复制
(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])来查看通道或频道排序是否会导致任何问题,但它甚至不能用于2D矩阵。

输入应该如何?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 13:36:16

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

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

代码语言:javascript
运行
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50945385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档