OpenCV findHomography应该返回一个单位矩阵.为什么它会返回这些意想不到的单应矩阵



我使用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

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

感谢您的帮助!

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

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

最新更新