我的猜测是您尝试将点从 cm 转换为米,但将最后两个点从 mm 转换为(按比例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
,并得到相同的结果(我不确定)。
我使用 OpenCV 和 Dlib 的 Head Pose Estimation 中的代码示例
我将协调后的(按比例缩放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()
结果:
您的帖子缺少信息,所以我真的不能说解决方案是否正确。