我正在使用SolvePnP
库的OpenCv
函数来计算相机的姿态,并且遇到一个问题,当我给出4点时,它给出的结果比我给出更多时要好一些点。这是代码示例,其中有些要点:
#include <opencv2/opencv.hpp>
int main(int argc, char **argv)
{
//with 11 points
std::vector<cv::Point2f> image_points_eleven = {
cv::Point2f(905.63, 694.37),
cv::Point2f(936.403, 696.781),
cv::Point2f(988.424, 700.553),
cv::Point2f(1020.02, 702.846),
cv::Point2f(1016.45, 741.839),
cv::Point2f(1012.79, 781.955),
cv::Point2f(1009.06, 822.609),
cv::Point2f(951.48, 815.937),
cv::Point2f(894.528, 810.86),
cv::Point2f(898.26, 772.418),
cv::Point2f(901.867, 732.744) };
std::vector<cv::Point3f> model_points_eleven = {
cv::Point3f(-155.32,155.32, 0),
cv::Point3f(-70.6,155.32, 0),
cv::Point3f(70.6,155.32, 0),
cv::Point3f(155.32,155.32, 0),
cv::Point3f(155.32,52.95, 0),
cv::Point3f(155.32,-52.95, 0),
cv::Point3f(155.32,-158.85, 0),
cv::Point3f(0, -155.32, 0),
cv::Point3f(-155.32,-155.32, 0),
cv::Point3f(-155.32,-52.95, 0),
cv::Point3f(-155.32, 52.95, 0)};
//with 4 points
std::vector<cv::Point2f> image_points_four = {
cv::Point2f(921.808,714.396),
cv::Point2f(999.474,720.263),
cv::Point2f(992.486,800.519),
cv::Point2f(914.465,793.569) };
std::vector<cv::Point3f> model_points_four = {
cv::Point3f(-211.5 / 2.0f, 211.5 / 2.0f, 0),
cv::Point3f(-211.5 / 2.0f, 211.5 / 2.0f, 0),
cv::Point3f(-211.5 / 2.0f, 211.5 / 2.0f, 0),
cv::Point3f(-211.5 / 2.0f, 211.5 / 2.0f, 0)};
// Camera internals
cv::Mat camera_matrix = (cv::Mat_<double>(3,3) << 1296.2477, 0, 1028, 0 , 1296.2477, 771, 0, 0, 1);
cv::Mat dist_coeffs = (cv::Mat_<double>(1,5) << -0.12658285, 0.13909541, 0, 0, -0.040676277); // Assuming no lens distortion
// Output rotation and translation
cv::Mat rotation_vector; // Rotation in axis-angle form
cv::Mat translation_vector;
// Solve for pose 11 points
cv::solvePnP(model_points_eleven, image_points_eleven, camera_matrix, dist_coeffs, rotation_vector, translation_vector);
rotation_vector.convertTo(rotation_vector, CV_32F);
translation_vector.convertTo(translation_vector, CV_32F);
std::cout << "Rotation Vector 11pts: " << rotation_vector << std::endl;
std::cout << "Translation Vector 11pts: " << translation_vector << std::endl;
// Solve for pose 11 points
cv::solvePnP(model_points_four, image_points_four, camera_matrix, dist_coeffs, rotation_vector, translation_vector);
rotation_vector.convertTo(rotation_vector, CV_32F);
translation_vector.convertTo(translation_vector, CV_32F);
std::cout << "Rotation Vector 4pts: " << rotation_vector << std::endl;
std::cout << "Translation Vector 4pts: " << translation_vector << std::endl;
}
我知道了:
Rotation Vector 11pts: [3.0777242; 0.13331571; -0.26817828]
Translation Vector 11pts: [-187.29686; -36.374325; 3410.5]
Rotation Vector 4pts: [1.5553488; 0.13531595; -0.19250046]
Translation Vector 4pts: [83.4842; -4.0712709; -163.0168]
与外部结果(激光)相比,对我来说,我拥有的更多点或至少相同的结果,我应该获得更高的精度,但是事实并非如此。比较X, Y and Z
我的代码有问题吗?
PS:model_points_four
和model_points_eleven
在3D世界和图像中具有相同的原点。