gpt4 book ai didi

python - OpenCV python:solvePnP ValueError:太多值无法解压

转载 作者:行者123 更新时间:2023-12-02 16:56:28 24 4
gpt4 key购买 nike

import numpy as np
import cv2

# 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]])
import numpy as np
import cv2

# 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 import numpy as np
import cv2

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



# Set UV (image) and XYZ (real life)
UV_cp = np.array([[1300, 2544], # left down
[1607, 1000], # left up
[3681, 2516], # right down
[3320, 983]],np.float32 ) # right up

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

rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
rotM_cam = cv2.Rodrigues(rvec)[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]])



# Set UV (image) and XYZ (real life)
UV_cp = np.array([[1300, 2544], # left down
[1607, 1000], # left up
[3681, 2516], # right down
[3320, 983]],np.float32 ) # right up

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

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



# Set UV (image) and XYZ (real life)
UV_cp = np.array([[1300, 2544], # left down
[1607, 1000], # left up
[3681, 2516], # right down
[3rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
rotM_cam = cv2.Rodrigues(rvec)[0]



# Set UV (image) and XYZ (real life)
UV_cp = np.array([[1300, 2544], # left down
[1607, 1000], # left up
[3681, 2516], # right down
[3320, 983]],np.float32 ) # right up

# Z is on 0 plane, so Z=0.0
XYZ_gcp = np.array([[0, 400,(rvec)[0]
0],
[0, 0, 0],
[300, 400, 0],
[300, 0, 0]],np.float32)

rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
rotM_cam = cv2.Rodrigues(rvec)[0]320, 983]],np.float32 ) # right up

# Z is on 0 plane, so Z=0.0
XYZ_gcp = np.array([[0, 400,(rvec)[0]
0],
[0, 0, 0],
[300, 400, 0],
[300, 0, 0]],np.float32)

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

我从 OpenCV: use solvePnP to determine homography得到了这段代码
但我得到错误为:
File "solv.py", line 50, in <module>
rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
ValueError: too many values to unpack

以及如何找到世界坐标xyz。
请帮我.........!!!!!

最佳答案

如果获得ValueError: too many values to unpack,则意味着左侧的变量数与右侧返回的值数不匹配。

在您的情况下:

rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
cv2.solvePnP()方法很可能在其返回元组中返回两个以上的值。要快速检查,请执行以下操作:
print len(cv2.solvePnP(XYZ_gcp, UV_cp, K, D))

或更妙的是,查看实际的返回元组:
print cv2.solvePnP(XYZ_gcp, UV_cp, K, D)

并确保您与此匹配。例。 (未试用):
rtval, rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)

检查这篇文章以获得更多见解 ValueError: too many values to unpack (Python 2.7)

关于python - OpenCV python:solvePnP ValueError:太多值无法解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27996978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com