opencv 我试图使用cv2.projectPoints(),但我得到一个错误

vhmi4jdf  于 2023-03-30  发布在  其他
关注(0)|答案(2)|浏览(260)

这是错误:

imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\calib3d\src\calibration.cpp:603: 
error: (-5:Bad argument) Rotation must be represented by 1x3 or 3x1 floating-point rotation vector, or 
3x3 rotation matrix in function 'cvProjectPoints2Internal'

这是我的代码:

axis = np.float32([[3, 0, 0], [0, 10, 0], [0, 0, -50]]).reshape(-1, 3)#axis of coordinates

# PnP calculates the rotation vector and translation vector
rvecs, tvecs, inliers = cv2.solvePnP(obj_points, image_points, mtx, dist)

print(f"mtx shape: {mtx.shape}, tvecs shape: {tvecs.shape}")
print(f"mtx:\n {mtx}")
print(f"tvecs:\n {tvecs}")

# Calculate the coordinates of 3D points projected on 2D image plane
imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)

这是一个outpot:

mtx shape: (3, 3), tvecs shape: (3, 1)

mtx:
 [[1.71223579e+03 0.00000000e+00 1.02990683e+03]
 [ 0.00000000e+00 1.70818827e+03 7.83446773e+02]
 [ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]

tvecs:
 [[-0.09038089]
 [ -0.05386737]
 [ -0.01652085]]

我不知道如何解决这个问题.它似乎是在形状的参数.但当我检查他们似乎在正确的形状.所以我不知道是什么问题,但如果你这样做是非常有帮助的.

zf9nrax1

zf9nrax11#

也许你正在尝试从旧教程中获得一些代码,或者你误解了函数文档中关于返回值的内容。尝试从solvePnP中分配返回值,如下所示:

result, rvecs, tvecs = cv2.solvePnP(obj_points, image_points, mtx, dist)

在solvePnP函数中不应该有“内点”(至少从opencv4开始)。

siv3szwd

siv3szwd2#

试试这个:
imagePoints,jacobian = cv.projectPoints(np.array(obj_points),np.float32(rvec),np.float32(tvec),camera_matrix1,None)
组件的形状真的很重要,它对我很有效。

相关问题