c++和python openCV代码中的不同结果

siv3szwd  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(200)

看起来我用Pyhton和C写了同样的东西,但它产生了完全不同的结果。
C
代码

std::vector<cv::Point2f> imgpnts { { 1095.5f, 1013.f },
                                       { 2382.f , 983.f  },
                                       { 2813.f , 1079.f },
                                     };

    std::vector<cv::Point3f> objpnts { { -2.74f    , -2.822f    , 58.028999f },
                                       { 2.4030001f, -3.0179999f, 60.153f    },
                                       { 2.438f    , -3.029f    , 29.926001f },
                                     }; 

    cv::Mat matrix = ( cv::Mat_<float>( 3, 3 ) << 15600, 0, 1920, 0, 15600, 960, 0, 0, 1 );
    cv::Mat dist   = ( cv::Mat_<float>( 1, 5 ) << 0, 0, 0, 0, 0 );

    cv::Mat tvec;
    cv::Mat rvec;

    cv::solvePnP( objpnts,
                  imgpnts,
                  matrix,
                  dist,
                  rvec,
                  tvec,
                  false, 
                  cv::SOLVEPNP_SQPNP );

产生这个

R [3.114110321750843;                                              
             -0.08173940943806265;                                                                         
             -0.1683807137960772]

          T [5.679242612461503;                                                
             -1.657279779587383;                                                                                 
             117.6574177733892]

Python代码

img_pnts = [ [ 1095.5, 1013 ],
                 [ 2382  , 983  ],
                 [ 2813  , 1079 ],
               ]

    obj_pnts = [ [ -2.74    , -2.822    , 58.028999 ],
                 [ 2.4030001, -3.0179999, 60.153    ],
                 [ 2.438    , -3.029    , 29.926001 ],
               ]

    K = np.asarray([15600, 0, 1920, 0, 15600, 960, 0, 0, 1]).reshape(3, 3).astype(np.float32)
    D = np.asarray([0, 0, 0, 0, 0]).reshape(1, 5).astype(np.float32)
 
    f, R, T = cv.solvePnP(np.asarray(obj_pnts).astype(np.float32),       np.asarray(img_pnts).astype(np.float32), K, D, flags=cv.SOLVEPNP_SQPNP)

给了我这个
R [[5.69669708e-03] [7.75121694e-05] [1.80173293e-02]]
T [[-0.5764531 ] [ 3.41078371] [ 3.62528898]]
值得一提的是,python的结果是好的,而c的结果是错误的,我只是不明白为什么!
c
openCV版本为4.5.3,python openCV版本为4.6.0.66,安装为opencv-contrib-python

b1uwtaje

b1uwtaje1#

实际上,如果我使用Point3d而不是Point3f,它在c++中产生相同的结果

相关问题