关于第7讲3D-2D实践程序中DMatch m:matches的理解

昨晚卡在这个程序的这句话,今天看了robinhjwy的解释,我再写几句补充:


原程序如下:

int main(int argc, char** argv)
{
    if ( argc != 5 )
    {
        cout<<"usage: pose_estimation_3d2d img1 img2 depth1 depth2"<<endl;
        return 1;
    }

    //-- 读取图像
    Mat img_1 = imread ( argv[1], CV_LOAD_IMAGE_COLOR );
    Mat img_2 = imread ( argv[2], CV_LOAD_IMAGE_COLOR );

    vector<KeyPoint> keypoints_1, keypoints_2;
    vector<DMatch> matches;
    find_feature_matches ( img_1, img_2, keypoints_1, keypoints_2, matches );
    cout<<"图一找到"<<keypoints_1.size() <<"个关键点"<<endl;
    cout<<"图二找到"<<keypoints_2.size() <<"个关键点"<<endl;
    cout<<"筛选后一共"<<matches.size() <<"组匹配点"<<endl;

    // 建立3D点
    Mat d1 = imread ( argv[3], CV_LOAD_IMAGE_UNCHANGED );       // 深度图为16位无符号数,单通道图像
    Mat K = (Mat_<double>(3, 3) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1);
    vector<Point3f> pts_3d;
    vector<Point2f> pts_2d;
    for ( DMatch m:matches )
    {
        //这一步应该是取得匹配点的深度,queryIdx查询描述子索引,pt关键点的坐标
        cout<<"输出索引为:"<<m.queryIdx<<endl;
        ushort d = d1.ptr<unsigned short> (int ( keypoints_1[m.queryIdx].pt.y )) [ int ( keypoints_1[m.queryIdx].pt.x ) ];
        if ( d == 0 )   // bad depth
            continue;
        float dd = float(d/1000.0);
        Point2d p1 = pixel2cam ( keypoints_1[m.queryIdx].pt, K );
        pts_3d.push_back ( Point3f ( float(p1.x*dd), float(p1.y*dd), dd ) );
        pts_2d.push_back ( keypoints_2[m.trainIdx].pt );
    }

    cout<<"3d-2d pairs: "<<pts_3d.size() <<endl;

    Mat r, t;
    solvePnP ( pts_3d, pts_2d, K, Mat(), r, t, false ); // 调用OpenCV 的 PnP 求解,可选择EPNP,DLS等方法
    Mat R;
    cv::Rodrigues ( r, R ); // r为旋转向量形式,用Rodrigues公式转换为矩阵

    cout<<"R="<<endl<<R<<endl;
    cout<<"t="<<endl<<t<<endl;

    cout<<"calling bundle adjustment"<<endl;

    bundleAdjustment ( pts_3d, pts_2d, K, R, t );

    return 0;
}
 


关键的一句在

 
 ushort d = d1.ptr<unsigned short> (int ( keypoints_1[m.queryIdx].pt.y )) [ int ( keypoints_1[m.queryIdx].pt.x ) ];

以下是对这句话我的理解:


首先,

(int ( keypoints_1[m.queryIdx].pt.y ))
m在遍历匹配好的特征点,在每个for循环返回一个特征点的行地址。
d1.ptr<unsigned short> (int ( keypoints_1[m.queryIdx].pt.y ))
在这部分,返回的行地址作为Mat类的实例d1的ptr函数的参数,被ptr使用,然后ptr函数返回该特征点的行地址,结合后面的
 [ int ( keypoints_1[m.queryIdx].pt.x ) ]    这是列地址
就得到了匹配点的地址……
为什么这么绕……猜是因为要强制类型转换,但是没根据不能乱猜的,唉。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值