Saving and Loading flann::Indices

asked 2013-05-21 11:44:16 -0500

NikolasE gravatar image


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);



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

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;

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

FileStorage fs2(kpts_file2.toStdString().c_str(), FileStorage::WRITE);


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

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

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?

edit retag flag offensive close merge delete


Maybe just some default variables of the flann-index are set differently. Please check your files via diff or vimdiff to see which values are actually different.

Guanta gravatar imageGuanta ( 2013-05-21 15:10:51 -0500 )edit

cv::flann stores the index as a data-file so that diff can only tell that both files are different: Binary files flann_copy.txt and flann.txt differ

NikolasE gravatar imageNikolasE ( 2013-05-22 11:30:59 -0500 )edit

hm, no clue then, could be a bug, maybe you want to create an issue-ticket at

Have you checked, if you get different results? So maybe until it is fixed, you could use it anyways.

And one last tipp: flann of opencv is pretty outdated (v1.6), so maybe it is worth using a current version of flann. It could be, that this bug is also a flann bug and already fixed in the new version.

Guanta gravatar imageGuanta ( 2013-05-22 13:01:38 -0500 )edit