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?
Maybe just some default variables of the flann-index are set differently. Please check your files via
diff
orvimdiff
to see which values are actually different.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
hm, no clue then, could be a bug, maybe you want to create an issue-ticket at http://code.opencv.org/projects/opencv/issues/new.
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.