Febin Samuel - 1 year ago 195
Python Question

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

I got this code from OpenCV: use solvePnP to determine homography
But I am getting error as :

``````File "solv.py", line 50, in <module>
rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
ValueError: too many values to unpack
``````

And how will we find world coordinates xyz .

If you get a `ValueError: too many values to unpack` it means that there is a mismatch between the number of variables on the left hand side and the number of values returned by the right hand side.

``````rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
``````

The method `cv2.solvePnP()` is most probably returning more than two values in its return tuple. To check this quickly, do:

``````print len(cv2.solvePnP(XYZ_gcp, UV_cp, K, D))
``````

or better yet, see the actual return tuple:

``````print cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
``````

and make sure you match the left hand side with this. egs. (untested):

``````rtval, rvec, tvec = cv2.solvePnP(XYZ_gcp, UV_cp, K, D)
``````

Check this post for more insight ValueError: too many values to unpack (Python 2.7)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download