OpenCV python: solvePnP ValueError: too many values to unpack
OpenCV python: solvePnP ValueError: too many values to unpack
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
,这意味着左侧的变量数量与右侧 return 的值数量不匹配。
你的情况:
rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
方法 cv2.solvePnP()
很可能在其 return 元组中 returning 了两个以上的值。要快速检查,请执行以下操作:
print len(cv2.solvePnP(XYZ_gcp, UV_cp, K, D))
或者更好的是,查看实际的 return 元组:
print cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
并确保将左侧与此匹配。例如。 (未经测试):
rtval, rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
检查此 post 以获得更多见解 ValueError: too many values to unpack (Python 2.7)
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
,这意味着左侧的变量数量与右侧 return 的值数量不匹配。
你的情况:
rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
方法 cv2.solvePnP()
很可能在其 return 元组中 returning 了两个以上的值。要快速检查,请执行以下操作:
print len(cv2.solvePnP(XYZ_gcp, UV_cp, K, D))
或者更好的是,查看实际的 return 元组:
print cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
并确保将左侧与此匹配。例如。 (未经测试):
rtval, rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
检查此 post 以获得更多见解 ValueError: too many values to unpack (Python 2.7)