OpenCV的:转换MatOfDMatch,MatOfPoint2f,MatOfKeypoint,aaproach找到FundamentalMatrix [英] OpenCV: conversion MatOfDMatch,MatOfPoint2f, MatOfKeypoint, aaproach to find FundamentalMatrix

查看:2506
本文介绍了OpenCV的:转换MatOfDMatch,MatOfPoint2f,MatOfKeypoint,aaproach找到FundamentalMatrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它可能是一个简单/愚蠢的问题,但我有在OpenCV的(机器人)的转化问题。

its may be a simple / stupid question, but I have a conversion problem in opencv (android).

我的目标是从两个连续图像计算fundamentalMatrix了相应的比赛。

my goal is to calculate the fundamentalMatrix out of corresponding matches from two consecutive images.

我这个程序到目前为止(和工作):

i programmed this so far (and working):

detector.detect(actImg, actKP);
detector.detect(prevImg, prevKP);
descExtractor.compute(prevImg, prevKP, descriptorPrev);
descExtractor.compute(actImg, actKP, descriptorAct);
descMatcher.match(descriptorPrev, descriptorAct, matches);
Features2d.drawMatches(prevImg, prevKP, actImg, actKP,matches, mRgba);

匹配的类型MatOfDMatch的。

matches are of the type MatOfDMatch.

现在我会计算fundamentalMatrix出相互匹配的点。为此,我必须知道哪些第一图像(preVKP)在关键点的被第二图像(actKP)中找到。

now i would calculate the fundamentalMatrix out of the points that matches against each other. therefor i must know which of the keypoints in the first image (prevKP) were found in the second image (actKP).

Mat fundamental_matrix = Calib3d.findFundamentalMat(nextPts, prevPts, Calib3d.FM_RANSAC,3, 0.99);

第一个问题:
我怎么可以提取/转换MatOfKeyPoints到MatOfPoint2f(它们可以被传递到findFundamentalMatrix)

first question: how can i extract / convert MatOfKeyPoints to MatOfPoint2f (that they can be passed to findFundamentalMatrix)

第二个问题:
如何通过只匹配关键点的功能findFundamentalMatrix。
这是做一个好办法?

second question: how to pass only the matched keypoints to the function findFundamentalMatrix. is this a good way of doing it?

由于很多advace!

thanks a lot in advace!

修改

非常感谢您详细的答复!
我写你的code到两个功能:

thanks a lot for your detailed response! i wrote your code into two functions:

private MatOfPoint2f getMatOfPoint2fFromDMatchesTrain(MatOfDMatch matches2,
        MatOfKeyPoint prevKP2) {
    DMatch dm[] = matches2.toArray();
    List<Point> lp1 = new ArrayList<Point>(dm.length);
    KeyPoint tkp[] = prevKP2.toArray();
    for (int i = 0; i < dm.length; i++) {
        DMatch dmm = dm[i];
        if (dmm.trainIdx < tkp.length) 
            lp1.add(tkp[dmm.trainIdx].pt);
    }
    return new MatOfPoint2f(lp1.toArray(new Point[0]));
}

private MatOfPoint2f getMatOfPoint2fFromDMatchesQuery(MatOfDMatch matches2,
        MatOfKeyPoint actKP2) {
    DMatch dm[] = matches2.toArray();
    List<Point> lp2 = new ArrayList<Point>(dm.length);
    KeyPoint qkp[] = actKP2.toArray();
    for (int i = 0; i < dm.length; i++) {
        DMatch dmm = dm[i];
        if (dmm.queryIdx < qkp.length)
            lp2.add(qkp[dmm.queryIdx].pt);
    }
    return new MatOfPoint2f(lp2.toArray(new Point[0]));
}

但是当我打电话

prevPts = getMatOfPoint2fFromDMatchesTrain(matches, prevKP);
nextPts = getMatOfPoint2fFromDMatchesQuery(matches, actKP);
Mat fundamental_matrix = Calib3d.findFundamentalMat(
        nextPts, prevPts, Calib3d.FM_RANSAC, 3, 0.99);

问题是,我得到的错误-215。
错误:

the problem is that i get the error -215. the error:

错误:(-215)npoints> = 0&放大器;&安培; points2.checkVector(2)== npoints&放大器;&放大器; points1.type()== points2.type()函数CV ::垫
  CV :: findFundamentalMat(...

我证明了prevPts和nextPts阿伦德低于10点(RANSAC)。
所以我猜的错误是点阿伦德浮点。但我认为这些点的浮动点调试检查这一点。

i proved that prevPts and nextPts arend below 10 points (for ransac). so i would guess that the error is that the points arend floating points. but i checked this with the debugger that these points are floating points.

您的建议$ C $克莱因:

your suggested codeline:

return new MatOfPoint2f(lp2.toArray(new Point[0]));

应点转换为浮点还是我错了?

should convert the points to floating point or am i wrong?

再次感谢

推荐答案

不幸的是,没有比在所有匹配循环更好的方式(甚至在C ++ API),并复制值,以新材料(或载体)。

Unfortunately there is no better way (even in C++ API) than loop through all matches and copy values to new Mat (or vector).

在Java中,你可以按如下方式做到这一点:

In Java you can do it as follows:

DMatch dm[] = matches.toArray();
List<Point> lp1 = new ArrayList<Point>(dm.length);
List<Point> lp2 = new ArrayList<Point>(dm.length);
KeyPoint tkp[] = prevKP.toArray();
KeyPoint qkp[] = actKP.toArray();
for (int i = 0; i < dm.length; i++) {
    DMatch dm = dm[i];
    lp1.add(tkp[dm.trainIdx].pt);
    lp2.add(qkp[dm.queryIdx].pt);
}

MatOfPoint2f pointsPrev = new MatOfPoint2f(lp1.toArray(new Point[0]));
MatOfPoint2f pointsAct  = new MatOfPoint2f(lp2.toArray(new Point[0]));

这篇关于OpenCV的:转换MatOfDMatch,MatOfPoint2f,MatOfKeypoint,aaproach找到FundamentalMatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆