在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
如果使用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::vectorcv::Point2f和std::vectorcv::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) };
参考
- Camera Calibration and 3D Reconstruction
- solvePnP object_points / image_points shape?
- [OpenCV Error: Assertion failed in undistort.cpp at line 293 [closed]](