OpenCV:使用solvePnP来确定单应性

Yor*_*ian 5 python opencv homography perspectivecamera

在过去的几周里,我尝试学习纠正图像,并在这里的人们的帮助下,我已经设法更好地理解它.大约一个星期前,我设置了一个我想要纠正的测试示例(从上面查看图像).这个工作得很好(原:http://sitedezign.net/original.jpg和修正:http://sitedezign.net/rectified.jpg)与函数T = cv2.getPerspectiveTransform(UV_cp,XYZ_gcp)其中,T变为单应.

当我尝试使用真实世界的照片时,它失败了,因为真实世界的坐标在一个平面上并不完美(但是在空间的X,Y和Z坐标中测量的大约10个控制点).因此我决定使用solvePnP,并希望能够创建一个我可以使用的Homography.

我在测试示例中尝试了这个但没有得到我预期的结果:图像没有被纠正,我使用solvePnP计算的Homography不等于用getPerspectiveTransform计算的Homography.

我的代码:

# Set UV (image) and XYZ (real life)
UV_cp = np.array([[1300.0, 2544.0], # left down
                  [1607.0, 1000.0], # left up
                  [3681.0, 2516.0], # right down
                  [3320.0, 983.0]], np.float32) # right up

# Z is on 0 plane, so Z=0.0
XYZ_gcp = np.array([[0.0, 400.0, 0.0],
                    [0.0, 0.0, 0.0],
                    [300.0, 400.0, 0.0],
                    [300.0, 0.0, 0.0]], np.float32)

rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
rotM_cam = cv2.Rodrigues(rvec)[0]

# calculate camera position (= translation), in mm from 0,0,0 point
cameraPosition = -np.matrix(rotM_cam).T * np.matrix(tvec)

# 3x3 Identity matrix
I = np.identity(3)

# [I|-C]
I1_extended = np.hstack((I,-cameraPosition))

# P = K*R*I
P_cam = K.dot(rotM_cam).dot(I1_extended)

# create P2 = image from above: R = 0,0,0, translation = x, y, z = 0,0,-1000 (mm)
R_rec = matr.getR(0.0,0.0,0.0)
newZ = -1000.0
new_cameraPosition = np.array([[0.0],[0.0],[newZ]])
I2_extended = np.hstack((I,new_cameraPosition))
P_rec = K.dot(R_rec).dot(I2_extended)

# correct Homography T from getPerspectiveTransform:
T = np.array([[4.70332834e-01, 9.35182514e-02, -4.24671558e+02],
              [9.62104844e-03, 9.69462117e-01, -4.92461571e+02],
              [3.54859924e-06, 6.80081146e-04, 1.00000000e+00]])

# Homography Matrix = H = P_rect * pinv(P) => P2 * pinv(P1)
H = P_rec.dot(np.linalg.pinv(P_cam))
Run Code Online (Sandbox Code Playgroud)

结果是扭曲的图像,远远不等于上面所示的图像(经过校正的图像).同样应该是正确的Homography T(来自getPerspectiveTransform)不接近等于使用来自solvePnP(H)的结果计算的单应性.

H from solvePnP:
[[  1.01865631e+00   2.68683332e-01  -2.04519580e+03]
 [ -3.24304366e-02   6.82672680e-01  -1.15688010e+03]
 [  2.03399902e-05   1.24191993e-04  -5.41378561e-01]]

H from getPerspectiveTransform:
[[  4.70332834e-01   9.35182514e-02  -4.24671558e+02]
 [  9.62104844e-03   9.69462117e-01  -4.92461571e+02]
 [  3.54859924e-06   6.80081146e-04   1.00000000e+00]]
Run Code Online (Sandbox Code Playgroud)

任何人都知道出了什么问题?

PS:用于确定K矩阵和失真系数的代码(值取自我的相机Pentax K-5,焦距为33mm,根据Adobe Camera Raw):

# Focal length, sensor size (mm and px)
f = 33.0 # mm
pix_width = 4928.0 # sensor size has 4928px in width
pix_height = 3624.0 # sensor size has 4928px in width
sensor_width = 23.7 # mm
sensor_height = 15.7 # mm

# set center pixel
u0 = int(pix_width / 2.0)
v0 = int(pix_height / 2.0)

# determine values of camera-matrix
mu = pix_width / sensor_width # px/mm
alpha_u = f * mu # px

mv = pix_height / sensor_height # px/mm
alpha_v = f * mv # px

# Distortion coefs 
D = np.array([[0.0, 0.0, 0.0, 0.0]])

# Camera matrix
K = np.array([[alpha_u, 0.0, u0],
              [0.0, alpha_v, v0],
              [0.0, 0.0, 1.0]])
Run Code Online (Sandbox Code Playgroud)

BCo*_*nic 1

您的K矩阵似乎合适,但这可能不足以实现真实图像的良好准确性。calibrateCamera我认为您应该使用该功能(文档链接教程)来校准相机,而不是给出先验的合理值(特别是光学中心像素和镜头畸变系数)。不过,我不认为你提到的问题是由此引起的。

我认为你的问题来自于 的定义P_rec

首先,请注意,如果您使用newZ = -1000.0,您实际上是将相机平移 1000 米(而不是毫米)。

其次,您必须非常小心正在考虑的 3D 点以及您希望它们在图像中投影的位置:

  1. 正如您XYZ_gcpsolvePnP函数中所使用的,这意味着您正在使用这些坐标作为 3D 点。

  2. 正如您XYZ_gcpgetPerspectiveTransform函数中使用的那样,这意味着您也将它们用作 2D 坐标。请注意,严格来说,您不能这样做,因为getPerspectiveTransform需要两个 4x2 数组(不是 4x2 和 4x3),但我假设您删除了始终为 0 的第三个坐标。

因此,您P_rec应该定义为 [x; y;1] = P_rec * [x; y;0; 1]。因此,P_rec应定义如下:

P_rec = [ [1 0 0 0] [0 1 0 0] [0 0 0 1] ].
Run Code Online (Sandbox Code Playgroud)