在过去几周中,我尝试学习校正图像,并在这里的人们的帮助下成功地理解了它。大约一周前,我设置了一个测试示例,我想要矫正它(从上方查看图像)。使用函数 T = cv2.getPerspectiveTransform(UV_cp, XYZ_gcp) 这个Homography已经可以实现了 (原始: http://sitedezign.net/original.jpg 和 矫正后: http://sitedezign.net/rectified.jpg)。
当我尝试使用真实世界的照片时,它失败了,因为真实世界坐标不完全位于平面上(但仅围绕空间中测量的X、Y和Z坐标的大约10个控制点)。因此我决定使用solvePnP,并希望能够创建一个我可以使用的Homography。
我在测试示例上尝试过这种方法,但是没有得到我预期的结果:图像没有被校正,而且我使用solvePnP计算出的Homography与使用getPerspectiveTransform计算出的Homography不相等。
我的代码:
当我尝试使用真实世界的照片时,它失败了,因为真实世界坐标不完全位于平面上(但仅围绕空间中测量的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))
结果是一张扭曲的图像,远远不如上面显示的那个(矫正后的图像)。此外,应该正确的单应性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]]
有人知道出了什么问题吗?
附注:用于确定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]])
solvePnP
调用相对应的代码示例以及如何从中获取H
吗? - BConic