python 只用opencv吗,python – OpenCV:使用solvePnP来确定单应性

在过去的几周里,我尝试学习纠正图像,并在这里的人们的帮助下,我已经设法更好地理解它.大约一个星期前,我设置了一个我想要纠正的测试示例(从上面查看图像).这很好(原始:

http://sitedezign.net/original.jpg和纠正:

http://sitedezign.net/rectified.jpg)功能T = cv2.getPerspectiveTransform(UV_cp,XYZ_gcp),其中T成为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))

结果是扭曲的图像,远远不等于上面所示的图像(经过校正的图像).同样应该是正确的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]]

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

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]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值