Python OpenCV solvePnP 转换为欧拉角

标签 python opencv euler-angles

我正在像这样使用 solvePnP..

import cv2
import numpy as np

# Read Image
im = cv2.imread("headPose.jpg");
size = im.shape

#2D image points. If you change the image, you need to change vector
image_points = np.array([
                            (359, 391),     # Nose tip
                            (399, 561),     # Chin
                            (337, 297),     # Left eye left corner
                            (513, 301),     # Right eye right corne
                            (345, 465),     # Left Mouth corner
                            (453, 469)      # Right mouth corner
                        ], dtype="double")

# 3D model points.
model_points = np.array([
                            (0.0, 0.0, 0.0),             # Nose tip
                            (0.0, -330.0, -65.0),        # Chin
                            (-225.0, 170.0, -135.0),     # Left eye left corner
                            (225.0, 170.0, -135.0),      # Right eye right corne
                            (-150.0, -150.0, -125.0),    # Left Mouth corner
                            (150.0, -150.0, -125.0)      # Right mouth corner

                        ])


# Camera internals

focal_length = size[1]
center = (size[1]/2, size[0]/2)
camera_matrix = np.array(
                         [[focal_length, 0, center[0]],
                         [0, focal_length, center[1]],
                         [0, 0, 1]], dtype = "double"
                         )

print "Camera Matrix :\n {0}".format(camera_matrix)

dist_coeffs = np.zeros((4,1)) # Assuming no lens distortion
(success, rotation_vector, translation_vector) = cv2.solvePnP(model_points, image_points, camera_matrix, dist_coeffs, flags=cv2.CV_ITERATIVE)

print "Rotation Vector:\n {0}".format(rotation_vector)
print "Translation Vector:\n {0}".format(translation_vector)

我对旋转向量和平移向量到底是什么感到困惑?我想我需要将这些转换为欧拉角,以便为我提供 3 个值,用于俯仰、滚动和偏航。

这是正确的吗?有人有这方面的例子吗?

最佳答案

rvecs是旋转的轴角表示,通常需要4个数,[v,theta],但是v需要是单位向量,所以它的长度编码为theta,将需要的数减少到3个。

对于代码来说,应该是这样的

def pnp(objectPoints,imgPoints,w,h,f):
    cameraMatrix = np.array([[f,0,w/2.0],
                     [0,f,h/2.0],
                    [0,0,1]])
    distCoeffs = np.zeros((5,1))
    revtval,rvecs, tvecs  =cv2.solvePnP(objectPoints[:,np.newaxis,:], imgPoints[:,np.newaxis,:], cameraMatrix, distCoeffs)#,False,flags=cv2.SOLVEPNP_EPNP)
    return rvecs,tvecs


def rot_params_rv(rvecs):
    from math import pi,atan2,asin
    R = cv2.Rodrigues(rvecs)[0]
    roll = 180*atan2(-R[2][1], R[2][2])/pi
    pitch = 180*asin(R[2][0])/pi
    yaw = 180*atan2(-R[1][0], R[0][0])/pi
    rot_params= [roll,pitch,yaw]
    return rot_params

关于Python OpenCV solvePnP 转换为欧拉角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54970421/

相关文章:

python - Pandas 重新采样不规则时间序列

python - 使用 beautifulsoup 通过 div 标签查找 div 文本

python - 将图像插入基材

c++ - CvSVM(const CvSVM&)’是私有(private)的

jquery - Django 模板使用带有 {{}} 的 jquery 脚本

python - 两个 DataFrame 的复杂合并

c++ - 在图像上应用蒙版以裁剪前景会产生不完整的输出

javascript - 如何将向量转换为欧拉 Angular

python-2.7 - 使用旋转矩阵偏移欧拉角

c# - 将欧拉转换为矩阵并将矩阵转换为欧拉