如何写向量< Dmatch>.到文件存储 [英] How to write vector<Dmatch> to FileStorage

查看:80
本文介绍了如何写向量< Dmatch>.到文件存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够使用FileStorage成功地将关键点(向量),描述符(垫)和其他内容写入yml文件.但是,在计算完一对图像之间的匹配之后,我无法将匹配写入文件.

I've been able to successfully write keypoints (vector), descriptors (Mat) and other things to yml files using FileStorage. However, after I compute the matches between a pair of images I am unable to write the matches to a file.

我认为这是因为DMatch似乎是具有距离,trainIdx等字段的结构,但是有人能将其写入文件吗?

I am assuming it is because DMatch seems to be a struct that has distance, trainIdx etc. fields, but does anyone have a good way of writing this to a file?

还是我应该只写距离矢量,trainIdx矢量等?

Or should I just write a distance vector, a trainIdx vector etc?

编辑后显示一些代码:

std::vector<cv::DMatch> matches;
for(int i=0;i<numims-1;i++){    
    for(int j=i+1;j<min(i+10,numims);j++){
        matches = MatchImagePair(i,j);
        //write the matches
        std::string matchfile = matchpath + "matchesij.yml";
        cv::FileStorage fs(matchfile, cv::FileStorage::WRITE);
        write(fs, "matches", matches);
        fs.release();
    }
}

matchesij应该包含i和j的数字,但是为了简洁起见,我对其进行了编辑.

matchesij should contain the numbers for i and j but I edited for brevity.

此外,MatchImagePair类似于:

std::vector<cv::DMatch> Matches::MatchImagePair(int idx1, int idx2){
    //matcher type
    cv::BFMatcher matcher(cv::NORM_L2);

    //given descrs1 and descrs2, match the 2 descriptors
    matcher.knnMatch(descrs1, descrs2, matches12, 2);
    matcher.knnMatch(descrs2, descrs1, matches21, 2);

    //various tests, returns vector<DMatch> given keypts1, keypts2 (for RANSAC)
    matches = DoVariousTests(matches12, matches21, keypts1, keypts2);

    return matches;
}

推荐答案

您可以重载>>运算符进行文件存储.

You could overload >> operator for file storage.

来自的cv :: TermCriteria的工作示例代码这里:

void operator>>(const cv::FileNode &node,
        cv::TermCriteria &termCrit) {
    node["type"] >> termCrit.type;
    node["maxCount"] >> termCrit.maxCount;
    node["epsilon"] >> termCrit.epsilon;
}

cv::FileStorage &operator<<(cv::FileStorage &fs,
        const cv::TermCriteria &termCrit) {
    fs << "{" << "type" << termCrit.type << "maxCount"
            << termCrit.maxCount << "epsilon" << termCrit.epsilon
            << "}";
    return fs;
}

这篇关于如何写向量&lt; Dmatch&gt;.到文件存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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