【问题标题】:OpenCV findHomography should return an identity matrix. Why is it instead returning these unexpected homography matrices?OpenCV findHomography 应该返回一个单位矩阵。为什么它会返回这些意想不到的单应矩阵?
【发布时间】:2020-11-12 20:27:09
【问题描述】:

我使用 findHomography 将图像拼接在一起。但是当我用已经完全重叠的图片测试它时,我得到了一些意想不到的结果。我原以为单应矩阵总是一个单位矩阵,而且大多数时候这是正确的,但有一次它返回一个完全不同的矩阵。

我用返回这个意外矩阵的点做了一个简单的例子,我得到了不同的结果,但又不是单位矩阵。

import numpy as np
import cv2

image1_points = np.array([[56., 96.], [56., 219.], [56., 219.], [37., 667.], [56., 720.], [56., 780.], [56., 837.]])
image2_points = np.array([[56., 96.], [56., 219.], [56., 219.], [37., 667.], [56., 720.], [56., 780.], [56., 837.]])

homography, mask = cv2.findHomography(image2_points, image1_points, cv2.RANSAC)
# In stitching.py I get:
#  -23.58183,  -0.00000,  547.67250
# -176.30191, -13.80196, 9872.90692
#   -0.26432,  -0.00000,    1.00000

# Here I get:
#   -2.95431,  -0.00000,   88.10041
#  -28.36051,  -1.38109, 1588.18848
#   -0.04252,  -0.00000,    1.00000

那么,有人能解释一下这里发生了什么吗?这是一个错误还是一些可以解决的特殊情况?

感谢任何帮助!

【问题讨论】:

  • 您正在使用 [56., 219.] 两次。尝试删除其中一个并检查。
  • 后端单应性代码无法正常工作,因为您为其提供了一组琐碎的点。在 7 个坐标中,其中 6 个共享相同的 x 坐标。输出是错误的,因为当 7 个点中有 6 个在同一条线上时,您无法对图像进行单应化。
  • 正如@zwang 提到的,单应性的 4 个必要点中没有 3 个可能是共线的
  • 谢谢!我今天会试试这个并报告回来。问题是这些点只是随机选择的最好的。但我想我必须确保每个点列表中至少有 3 个不同的点,以便可以将 2 个平面相互比较。如果有人想在我反馈后撰写答案,请随意!

标签: python numpy opencv homography image-stitching


【解决方案1】:

所以现在我测试了@ZWang 和@Micka 的建议。这是我的问题中提供的代码的更新版本,并添加了 cmets 以进行解释。我希望这会帮助一些人!

    import numpy as np
    import cv2
    
    # Original arrays were the problem occurs. There are enough points, but they lack the property to span a plane, wich is needed for finding a homography
    image1_points = np.array([[56., 96.], [56., 219.], [56., 219.], [37., 667.], [56., 720.], [56., 780.], [56., 837.]])
    image2_points = np.array([[56., 96.], [56., 219.], [56., 219.], [37., 667.], [56., 720.], [56., 780.], [56., 837.]])
    
    homography, mask = cv2.findHomography(image2_points, image1_points, cv2.RANSAC)
    print(homography)
    # In stitching.py I get:
    #  -23.58183,  -0.00000,  547.67250
    # -176.30191, -13.80196, 9872.90692
    #   -0.26432,  -0.00000,    1.00000
    
    # Here I get:
    #   -2.95431,  -0.00000,   88.10041
    #  -28.36051,  -1.38109, 1588.18848
    #   -0.04252,  -0.00000,    1.00000

    # No problems as long as there are at least 4 corresponding points and they span a plane!
    image1_points = np.array([[40., 96.], [56., 219.], [37., 667.], [56., 720.]])
    image2_points = np.array([[40., 96.], [56., 219.], [37., 667.], [56., 720.]])

    homography, mask = cv2.findHomography(image2_points, image1_points, cv2.RANSAC)
    print(homography)
    # Now I get the identity matrix that is to be expected:
    # 1.00000, 0.00000, 0.00000
    # 0.00000, 1.00000, 0.00000
    # 0.00000, 0.00000, 1.00000

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2015-03-24
    相关资源
    最近更新 更多