1

我正在将描述符(SurfDescriptorExtractor 输出)和关键点(SurfFeatureDetector 输出)写入 XML 文件。在将关键点(std::vector)转换为 Mat 之前完成(如下:将关键点转换为 mat 或将它们保存到文本文件 opencv)。因为描述符不是必需的,它们已经是 Mat 了。所以两者都保存为Mat,阅读也没有问题。但是当使用 FlannBasedMatcher,然后使用 drawMatches 时,此方法要求提供关键点数据。

问题是:如何将 Mat 转换为 Keypoint 的向量,哪种方法最好?

4

2 回答 2

1

这就是opencv 源代码Java中进行转换的方式,我在 C++ 中找不到这种转换,它可能不存在。你也许可以把它翻译成 C++,它不是很复杂。

 //Code from Opencv4Android: utils/Converters.java
 public static void Mat_to_vector_KeyPoint(Mat m, List<KeyPoint> kps) {
        if (kps == null)
            throw new java.lang.IllegalArgumentException("Output List can't be null");

        int count = m.rows();
        if (CvType.CV_64FC(7) != m.type() || m.cols() != 1)
            throw new java.lang.IllegalArgumentException(
                    "CvType.CV_64FC(7) != m.type() ||  m.cols()!=1\n" + m);

        kps.clear();
        double[] buff = new double[7 * count];
        m.get(0, 0, buff);

        for (int i = 0; i < count; i++) {
            kps.add(new KeyPoint((float) buff[7 * i], (float) buff[7 * i + 1], (float) buff[7 * i + 2], (float) buff[7 * i + 3],
                    (float) buff[7 * i + 4], (int) buff[7 * i + 5], (int) buff[7 * i + 6]));
        }
    }
于 2013-02-15T17:53:32.403 回答
1

刚刚通过查看 OpenCV 源代码(在 /modules/java/generator/src/cpp/converters.cpp 下,第 185 行附近)发现了这一点:

void Mat_to_vector_KeyPoint(Mat& mat, vector<KeyPoint>& v_kp)
{
    v_kp.clear();
    CHECK_MAT(mat.type()==CV_32FC(7) && mat.cols==1);
    for(int i=0; i<mat.rows; i++)
    {
        Vec<float, 7> v = mat.at< Vec<float, 7> >(i,0);
        KeyPoint kp(v[0], v[1], v[2], v[3], v[4], (int)v[5], (int)v[6]);
        v_kp.push_back(kp);
    }
    return;
}

我将其用作:

vector<KeyPoint> mat_to_keypoints(Mat* mat) {

    vector<KeyPoint>  c_keypoints;

    for ( int i = 0; i < mat->rows; i++) {
        Vec<float, 7> v = mat.at< Vec<float, 7> >(i,0);

        KeyPoint kp(v[0], v[1], v[2], v[3], v[4], (int)v[5], (int)v[6]);

        c_keypoints.push_back(kp);

    };

    return c_keypoints;

};
于 2013-02-16T04:09:36.273 回答