Converting Mat to Keypoint?

杀马特。学长 韩版系。学妹 提交于 2019-12-31 05:00:48

问题


I'm writing both descriptors (SurfDescriptorExtractor output) and keypoints (SurfFeatureDetector output) to an XML file. Before writing keypoints (std::vector) conversion to Mat is done ( following this: convert keypoints to mat or save them to text file opencv ). For descriptors isn't neccesary, they're Mat already. So both are saved as Mat, there's no problem on reading either. But when using a FlannBasedMatcher, and then drawMatches, this method asks for keypoint data.

The question is: how would you convert Mat to Keypoint's vector, and which would be the best approach?


回答1:


This is how the opencv source code does the conversion in Java, I couldn't find this conversion in C++, it might not exist. You might be able to translate this to C++, it is not very complex.

 //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]));
        }
    }



回答2:


Just found this by looking at OpenCV source (under /modules/java/generator/src/cpp/converters.cpp, around line 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;
}

And I'm using it as:

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;

};


来源:https://stackoverflow.com/questions/14898540/converting-mat-to-keypoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!