我正在使用库的SolvePnP
功能计算相机的姿势,OpenCv
我面临的问题是,当我给四分时,它给我的结果比我给更多分时稍微好一点。以下是代码示例,其中包含一些要点:
#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世界和图像中具有相同的原点。