Python 估计矩形对象的姿势是不稳定的

Python 估计矩形对象的姿势是不稳定的,python,opencv,pose-estimation,opencv-solvepnp,pose,Python,Opencv,Pose Estimation,Opencv Solvepnp,Pose,我使用下面的代码来找到一个矩形物体的旋转和平移向量。 矩形物体的高度和宽度分别为33和44厘米。所以我使用下面的代码来创建对象点 width = 44 height = 33 objPoints = np.array( [(0, 0, 0), (width * 0.01, 0, 0), (width * 0.001, -(height* 0.001) , 0), (0, -(height* 0.001), 0)] ) 我使用下面的代码来确定旋转、平移向量 def findPose(i

我使用下面的代码来找到一个矩形物体的旋转和平移向量。 矩形物体的高度和宽度分别为33和44厘米。所以我使用下面的代码来创建对象点

width = 44
height = 33

objPoints = np.array(
    [(0, 0, 0), (width * 0.01, 0, 0), (width * 0.001, -(height* 0.001) , 0), (0, -(height* 0.001), 0)]
)
我使用下面的代码来确定旋转、平移向量

def findPose(imagePoints):

    (success, rotation_vector, translation_vector) = cv2.solvePnP(objPoints, imagePoints, camera_matrix,
                                                                  dist_coeffs, flags=cv2.SOLVEPNP_ITERATIVE)
    print("Rotation Vector:\n {0}".format(rotation_vector))
    print("Translation Vector:\n {0}".format(translation_vector))
    (end_point2D, jacobian) = cv2.projectPoints(np.array([(0.0, 0.0, 1000.0)]), rotation_vector,
                                                     translation_vector, camera_matrix, dist_coeffs)

由于某些原因,结果总是错误的。我是否正确创建了对象点?

我猜您尝试将点从厘米转换为米,但将最后两个点从毫米转换为米(按
0.001
而不是
0.01
缩放)

我认为你打算使用:

objPoints = np.array(
    [(0, 0, 0), (width * 0.01, 0, 0), (width * 0.01, -(height* 0.01) , 0), (0, -(height* 0.01), 0)]
)
我不是摄影测量专家,但我认为解决方案是“比例不变”,因此您可以通过
1.0
缩放坐标,并获得相同的结果(我不确定)


我使用了来自示例的代码

我将坐标(按
0.01而不是
0.001
缩放后)放入MATLAB 3D绘图中。
我旋转了绘图,使其接近示例中的头部姿势

代码如下:

import numpy as np
import cv2

width = 44
height = 33

objPoints = np.array(
    #[(0, 0, 0), (width * 0.01, 0, 0), (width * 0.001, -(height* 0.001) , 0), (0, -(height* 0.001), 0)]
    [(0, 0, 0), (width * 0.01, 0, 0), (width * 0.01, -(height* 0.01) , 0), (0, -(height* 0.01), 0)]
)

# Read Image
im = cv2.imread("img.png");
size = im.shape

#2D image points. If you change the image, you need to change vector
# https://www.learnopencv.com/head-pose-estimation-using-opencv-and-dlib/
#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")

image_points = np.array([
                            (273, 100),
                            (478, 182),
                            (313, 275),
                            (107, 190)
                        ], dtype="double")


# 3D model points (from WEB sample).
# https://www.learnopencv.com/head-pose-estimation-using-opencv-and-dlib/
#objPoints = 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"
                         )



def findPose(imagePoints):
    dist_coeffs = np.zeros((4,1)) # Assuming no lens distortion

    (success, rotation_vector, translation_vector) = cv2.solvePnP(objPoints, imagePoints, camera_matrix,
                                                                  dist_coeffs, flags=cv2.SOLVEPNP_ITERATIVE)
    print("Rotation Vector:\n {0}".format(rotation_vector))
    print("Translation Vector:\n {0}".format(translation_vector))
    (end_point2D, jacobian) = cv2.projectPoints(np.array([(0.0, 0.0, 1000.0)]), rotation_vector,
                                                     translation_vector, camera_matrix, dist_coeffs)

    # Project a 3D point (0, 0, 1000.0) onto the image plane.
    # We use this to draw a line sticking out of the nose
    for p in image_points:
        cv2.circle(im, (int(p[0]), int(p[1])), 5, (255,0,0), -1)

    p1 = ( int(image_points[0][0]), int(image_points[0][1]))
    p2 = ( int(end_point2D[0][0][0]), int(end_point2D[0][0][1]))

    cv2.line(im, p1, p2, (0,255,0), 3)


findPose(image_points)


# Display image
cv2.imshow("Output", im)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果:

你的帖子遗漏了信息,所以我真的不能说这个解决方案是否正确