Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Saving and Loading flann::Indices

Hello

I am trying to store and load flann::indices to speed up some experiments. I extract features and descriptors using OpenCV 2.4 and SurfFeatureDetector/SurfDescriptorExtractor, create an Index and save everything:

    Mat img = imread(img_file.toStdString(),CV_LOAD_IMAGE_GRAYSCALE);

    SurfFeatureDetector detector(minHessian);
    SurfDescriptorExtractor extractor;


    vector<KeyPoint> keypoints;
    detector.detect( img, keypoints);

    Mat descriptors;
    extractor.compute( img, keypoints, descriptors);

    flann::Index ndx(descriptors, flann::KDTreeIndexParams(4));

    QString kpts_file = sub_folder + "/keypoints_descriptors.yml";

    FileStorage fs(kpts_file.toStdString().c_str(), FileStorage::WRITE);

    cv::write(fs,"descriptors",descriptors);
    cv::write(fs,"keypoints",keypoints);

    fs.release();

    QString ndx_file = sub_folder + "/flann.txt";

    ndx.save(ndx_file.toStdString().c_str());

So far, everything works fine. To check if everything is okay, I read the files again and write the index again into a file:

FileStorage fs(kpts_file.toStdString().c_str(), FileStorage::READ);

fs["descriptors"] >> info.descriptors;
cv::read(fs["keypoints"],info.keypoints);

QString kpts_file2 = sub_folder + "/keypoints_descriptors_copy.yml";

FileStorage fs2(kpts_file2.toStdString().c_str(), FileStorage::WRITE);
cv::write(fs2,"descriptors",info.descriptors);
cv::write(fs2,"keypoints",info.keypoints);

info.flann_tree.load(info.descriptors,ndx_file.toStdString().c_str());

QString ndx_file2 = sub_folder + "/flann_copy.txt";


printf("Img: %s, kpts: %zu\n", img_name.toStdString().c_str(), info.keypoints.size());

info.flann_tree.save(ndx_file2.toStdString().c_str());

I then compute the md5sum of the two files I wrote. The yml-File containing the keypoints and the descriptors have the same checksum, but the two flann-files don't match (although they have the same size).

Can someone tell me what happens during saving and loading that could change the flann::Index?