简体   繁体   English

以下3个opencv Mat实例有什么区别?

[英]What is the difference between the following 3 opencv Mat instances?

In my program i need a 3*3 matrix K(camera calibration matrix) and a 3*3 matrix F(Fundamental matrix) to calculate E(Essential matrix):E = Kt() F K,but when i use 在我的程序中,我需要一个3 * 3矩阵K(摄像机校准矩阵)和一个3 * 3矩阵F(基本矩阵)来计算E(基本矩阵):E = Kt() F K,但是当我使用时

cv::Matx33d K (518.0, 0,325.5, 0 ,519.0,253.5, 0 , 0, 1); cv :: Matx33d K(518.0,0,325.5,0,519.0,253.5,0,0,1);

it yields error: no match for 'operator*' in the line: 它产生错误:行中的'operator *'不匹配:

E = Kt() * F * K; E = Kt()* F * K;

but when i use 但是当我使用的时候

cv::Mat K(3,3,CV_64FC1);      //cannot be 32 (float)
K.at<double>(0,0) = 518.0;
K.at<double>(1,1) = 519.0;
K.at<double>(0,2) = 325.5;
K.at<double>(1,2) = 253.5;
K.at<double>(2,2) = 1;

it works,but when i use 它有效,但当我使用

cv::Mat_<double> K = ( cv::Mat_<double>(3, 3) <<
         518.0,    0,325.5,
         0    ,519.0,253.5,
         0    ,    0,    1);

it works too, but yields totally different result compared with above 它也有效,但与上述相比,产量完全不同

Can anyone tell me what is the difference between the 3 opencv Mat insatances?Thanks. 任何人都可以告诉我3个opencv Mat insatances有什么区别?谢谢。

Here's all of the code 这是所有的代码

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>

using namespace std;
using namespace cv;

int main()
{
    cout<<"Hello SLAM!"<<endl;
    cv::Mat img1,img2,img;


                                                              // first method :can run ,but yeild fault result
    cv::Mat K(3,3,CV_64FC1);      //cannot be 32 (float)
    K.at<double>(0,0) = 518.0;
    K.at<double>(1,1) = 519.0;
    K.at<double>(0,2) = 325.5;
    K.at<double>(1,2) = 253.5;
    K.at<double>(2,2) = 1;


                                                              // second method :cannont build
//    cv::Matx33d K (518.0,    0,325.5,
//                   0    ,519.0,253.5,
//                   0    ,    0,    1);//calibration matrix

//        cv::Matx33d K=cv::Matx33d(518.0,    0,325.5,
//                       0    ,519.0,253.5,
//                       0    ,    0,    1);//calibration matrix

                                                              // third method :can run ,yeild correcy result
    cv::Mat_<double> K = ( cv::Mat_<double>(3, 3) <<
             518.0,    0,325.5,
             0    ,519.0,253.5,
             0    ,    0,    1);


    img1= cv::imread("/home/kylefan/program/cmake_demo/image/rgb1.png",IMREAD_COLOR);
    img2= cv::imread("/home/kylefan/program/cmake_demo/image/rgb2.png",IMREAD_COLOR);

    vector<cv::Point2f> point1;
    vector<cv::Point2f> point2;
    cv::ORB orb;
    vector<cv::KeyPoint>kp1,kp2;
    cv::Mat desp1,desp2;

    orb(img1,cv::Mat(),kp1,desp1,false);
    orb(img2,cv::Mat(),kp2,desp2,false);

    cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");

    double knn_match_ratio = 0.3;
    vector< vector<cv::DMatch>> matches_knn;

    matcher->knnMatch(desp1,desp2,matches_knn,2);

    vector<cv::DMatch> goodmatches;

    for(int i=0;i<matches_knn.size();i++)
    {
        if(matches_knn[i][0].distance < knn_match_ratio * matches_knn[i][1].distance)goodmatches.push_back(matches_knn[i][0]);
    }

    if(goodmatches.size()<20)
    {
        cout<<"too less goodmatches "<<endl;
    }

    for(auto m:goodmatches)
    {
        point1.push_back(kp1[m.queryIdx].pt);
        point2.push_back(kp2[m.queryIdx].pt);
    }
//    for(int j=0;j<goodmatches.size();j++)
//    {
//        //cout<<j<<":"<<point1[j].x<<","<<point1[j].y<<","<<point2[j].x<<","<<point2[j].y<<","<<endl;
//    }

    cv::Mat F = cv::findFundamentalMat(point1,point2,FM_RANSAC,3,0.99);//in calib3d

    cv::Mat_<double> E = K.t() *F*  K;

    SVD svd(E);

    cv::Matx33d W(0,-1,0,
                  1,0,0,
                  0,0,1);

    cv::Mat_<double> R = svd.u * Mat(W) * svd.vt;
    cv::Mat_<double> t = svd.u.col(2);

    cv::Matx34d P1(R(0,0),R(0,1),R(0,2),t(0),
                   R(1,0),R(1,1),R(1,2),t(1),
                   R(2,0),R(2,1),R(2,2),t(2));

    cout<<goodmatches.size()<<" goodmatches "<<endl;

    cout<<"F="<<F<<endl;
    cout<<"K="<<K<<endl;
    cout<<"E="<<E<<endl;
    cout<<"R="<<R<<endl;
    cout<<"t="<<t<<endl;
    cout<<"P1="<<P1<<endl;

    if(fabsf(determinant(R))-1.0 > 1e-07)
    {
        cerr << "det(R) != +-1.0, this is not a rotation matrix" << endl;
    }
    else
    {
        cout<<"rotation matrix is right"<<endl;
    }
    return 0;
}

In your second version you didn't initialize the matrix elements and don't set them all. 在第二个版本中,您没有初始化矩阵元素,也没有将它们全部设置。

cv::Mat K(3,3,CV_64FC1);      //cannot be 32 (float)
K.at<double>(0,0) = 518.0;
K.at<double>(1,1) = 519.0;
K.at<double>(0,2) = 325.5;
K.at<double>(1,2) = 253.5;
K.at<double>(2,2) = 1;

this means that there can be ANY values in indices (0,1); 这意味着索引(0,1)中可以有任何值; (1,0); (1,0); (2,0) and (2,1). (2,0)和(2,1)。

To initialize with 0 on creation try: cv::Mat::zeros() 要在创建时使用0初始化,请尝试: cv::Mat::zeros()

cv::Mat K = cv::Mat::zeros(3,3,CV_64FC1);      //cannot be 32 (float)
K.at<double>(0,0) = 518.0;
K.at<double>(1,1) = 519.0;
K.at<double>(0,2) = 325.5;
K.at<double>(1,2) = 253.5;
K.at<double>(2,2) = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM