opencv使用cv::solvePnP中输入参数问题

在opencv3.3文档中的Camera Calibration and 3D Reconstruction

bool cv::solvePnP   (   InputArray  objectPoints,
InputArray  imagePoints,
InputArray  cameraMatrix,
InputArray  distCoeffs,
OutputArray     rvec,
OutputArray     tvec,
bool    useExtrinsicGuess = false,
int     flags = SOLVEPNP_ITERATIVE 
)   
bool cv::solvePnPRansac (   InputArray  objectPoints,
InputArray  imagePoints,
InputArray  cameraMatrix,
InputArray  distCoeffs,
OutputArray     rvec,
OutputArray     tvec,
bool    useExtrinsicGuess = false,
int     iterationsCount = 100,
float   reprojectionError = 8.0,
double  confidence = 0.99,
OutputArray     inliers = noArray(),
int     flags = SOLVEPNP_ITERATIVE 
)   

在文档中,对于objectPoints和imagePoints的类型要求是:

objectPoints: Array of object points in the object coordinate space, Nx3 1-channel or 1xN/Nx1 3-channel, where N is the number of points. vector can be also passed here.
imagePoints: Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, where N is the number of points. vector can be also passed here.

如果使用Nx3 1-channel 和Nx2 1-channel作为输入, 那么编译会出现以下错误

CV_IS_MAT(_src) && CV_IS_MAT(_dst) && (_src->rows == 1 || _src->cols == 1) && (_dst->rows == 1 || _dst->cols == 1) && _src->cols + _src->rows - 1 == _dst->rows + _dst->cols - 1 && (CV_MAT_TYPE(_src->type) == CV_32FC2 || CV_MAT_TYPE(_src->type) == CV_64FC2) && (CV_MAT_TYPE(_dst->type) == CV_32FC2 || CV_MAT_TYPE(_dst->type) == CV_64FC2)

从错误中可知:

(_src->rows == 1 || _src->cols == 1) && (_dst->rows == 1 || _dst->cols == 1)

所以需要将数据reshape成多通道的形式:

objectPoints = objectPoints.reshape(4, 1, 3);
imagePoints = imagePoints.reshape(4, 1, 2);

或者使用std::vector<:point2f>和std::vector<:point3f>等形式作为输入参数

using namespace std;
using namespace cv;

vector<Point2f> objectPoints = { Point2f(433,50),Point2f(512,109),Point2f(425,109),Point2f(362,106) };
vector<Point3f> imagePoints = { Point3f(0,0,0),Point3f(6.5,0,0),Point3f(0,0,6.5),Point3f(0,6.5,0) };

参考

转载于:https://www.cnblogs.com/ChrisCoder/p/10083151.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值