Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

hmm, as long as opncv can't serialize the dmatches properly, either don't save them, or come up with a better way ?

void write(FileStorage& fs, const String& objname, const std::vector<dmatch>& matches) { // cv::internal::masochistic::obstruction::WriteContext("it's all about replacing the first and last line of this function with some RAII pattern, but honestly, my mother is beeer with that than pou"); fs << objname << "["; for (size_t i=0; i<matches.size(); i++)="" {="" const="" dmatch="" &amp;dm="matches[i];" cv::write(fs,="" dm.queryidx);="" cv::write(fs,="" dm.trainidx);="" cv::write(fs,="" dm.imgidx);="" cv::write(fs,="" dm.distance);="" }="" fs="" &lt;&lt;="" "]";="" }="" void="" read(filestorage&amp;="" fs,="" const="" string&amp;="" objname,="" std::vector<dmatch="">& matches) { FileNode pnodes = fs[objname]; for (FileNodeIterator it=pnodes.begin();; ) { DMatch dm; *it >> dm.queryIdx; it++; *it >> dm.trainIdx; it++; *it >> dm.imgIdx; it++; *it >> dm.distance; it++; matches.push_back(dm); if (it==pnodes.end()) break; } } ostream& operator << (ostream &out, const std::vector<dmatch>& matches) { out << "[" ; for (size_t i=0; i<matches.size(); i++)="" {="" const="" dmatch="" &amp;dm="matches[i];" out="" &lt;&lt;="" dm.queryidx="" &lt;&lt;="" ","="" &lt;&lt;="" dm.trainidx="" &lt;&lt;="" ","="" &lt;&lt;="" dm.imgidx="" &lt;&lt;="" ","="" &lt;&lt;="" dm.distance="" &lt;&lt;="" ";"="" &lt;&lt;="" endl;="" }="" out="" &lt;&lt;="" "]"="" &lt;&lt;="" endl;="" return="" out;="" }<="" p="">

//// //// optionally, read it back in: //// // //int main2(void) //{ // vector<dmatch> matches2; // FileStorage fs2("some.xml", FileStorage::READ); // read(fs2,"Matches",matches2); // cout << matches2 << endl; // fs2.release(); // return 0; //}

hmm, as long as opncv can't serialize the dmatches properly, either don't save them, or come up with a better way ?

void write(FileStorage& fs, const String& objname, const std::vector<dmatch>& std::vector<DMatch>& matches) {
    // cv::internal::masochistic::obstruction::WriteContext("it's all about replacing the first and last line of this function with some RAII pattern, but honestly, my mother is beeer with that than pou");
    fs << objname << "[";
    for (size_t i=0; i<matches.size(); i++)="" {="" const="" dmatch="" &amp;dm="matches[i];" cv::write(fs,="" dm.queryidx);="" cv::write(fs,="" dm.trainidx);="" cv::write(fs,="" dm.imgidx);="" cv::write(fs,="" dm.distance);="" }="" fs="" &lt;&lt;="" "]";="" }="" void="" read(filestorage&amp;="" fs,="" const="" string&amp;="" objname,="" std::vector<dmatch="">& i++) {
        const DMatch &dm = matches[i];
        cv::write(fs, dm.queryIdx);
        cv::write(fs, dm.trainIdx);
        cv::write(fs, dm.imgIdx);
        cv::write(fs, dm.distance);
    }
    fs << "]";
}
void read(FileStorage& fs, const String& objname, std::vector<DMatch>& matches) {
    FileNode pnodes = fs[objname];
    for (FileNodeIterator it=pnodes.begin();; ) {
        DMatch dm;
        *it >> dm.queryIdx; it++;
        *it >> dm.trainIdx; it++;
        *it >> dm.imgIdx;   it++;
        *it >> dm.distance; it++;
        matches.push_back(dm);
        if (it==pnodes.end()) break;
    }
}
ostream& operator << (ostream &out, const std::vector<dmatch>& std::vector<DMatch>& matches) {
    out << "[" ;
    for (size_t i=0; i<matches.size(); i++)="" {="" const="" dmatch="" &amp;dm="matches[i];" out="" &lt;&lt;="" dm.queryidx="" &lt;&lt;="" ","="" &lt;&lt;="" dm.trainidx="" &lt;&lt;="" ","="" &lt;&lt;="" dm.imgidx="" &lt;&lt;="" ","="" &lt;&lt;="" dm.distance="" &lt;&lt;="" ";"="" &lt;&lt;="" endl;="" }="" out="" &lt;&lt;="" "]"="" &lt;&lt;="" endl;="" return="" out;="" }<="" p="">
i++) {
        const DMatch &dm = matches[i];
        out << dm.queryIdx << "," << dm.trainIdx << "," << dm.imgIdx << "," << dm.distance << ";" << endl;
    }
    out << "]" << endl;
    return out;
}
 

//// //// optionally, read it back in: //// // //int main2(void) //{ // vector<dmatch> vector<DMatch> matches2; // FileStorage fs2("some.xml", FileStorage::READ); // read(fs2,"Matches",matches2); // cout << matches2 << endl; // fs2.release(); // return 0; //}

//}