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::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) };

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM