Hi,
I am using flann index to implement image matching. In the first program I create feature cluster Mat including all descriptors of images in the database.
Then I use this feature cluster to create Index :
flann::Index flannIndex(featurecluster, flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN);
Then save the index:
flannIndex.save("../../index");
========================================
In another program I am trying to load his index by using:
flann::Index flannIndex(featurecluster, flann::SavedIndexParams("../../index"), cvflann::FLANN_DIST_EUCLIDEAN);
and then executed knn search
flannIndex.knnSearch(srcIMG, indices, dists, knn, flann::SearchParams());
But the system had a warning:
OpenCV Error: Assertion failed (query.type() == type && indices.type() == CV_32S && dists.type() == dtype)
I am not sure how to assign correct types to query, indices and dists. And if there is a better way to save and load index please let me know.
I really appreciate your help!
The following is my code:
// SURF.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <queue>
#include <time.h>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
clock_t begin, end;
begin = clock();
/*
SurfFeatureDector() finds feature point
SurfDescriptorExtractor() extracts descriptor of images
@para minHessian , a control parameter of SurfFeatureDetector, controlling the number of feature points
@para DB_SIZE number of images in database
@para despSRC descriptor of src img
@para despDB descriptor of database img
*/
const int minHessian = 3000;
const int DB_SIZE = 100;
SurfFeatureDetector detector(minHessian);
vector<KeyPoint> keyPoints1, keyPoints2;
SurfDescriptorExtractor extractor;
Mat despSRC, despDB;
Mat srcIMG = imread("../../IMG_DB/IMG_67.jpg");
if (!srcIMG.data){
cout << "source img not exsit!" << endl;
return false;
}
detector.detect(srcIMG, keyPoints1);
extractor.compute(srcIMG, keyPoints1, despSRC);
//build FLANN index
//@para featurecluster all descriptor of database
Mat featurecluster;
//@para index_IMG documents the relations between indices and img. eg: input a number of index, it gives back its corresponding img
vector<int>index_IMG;
clock_t timeBegin, timeEnd;
timeBegin = clock();
for (int i = 1; i <= DB_SIZE; i++)
{
char filename[100];
sprintf_s(filename, "../../IMG_DB/IMG_%d.jpg", i);
Mat DB_IMG = imread(filename);
if (!DB_IMG.data){
cout << "database img not exsit" << endl;
return 0;
}
detector.detect(DB_IMG, keyPoints2);
extractor.compute(DB_IMG, keyPoints2, despDB);
//cout << despDB.rows << endl;
featurecluster.push_back(despDB);
for (int j = 0; j < despDB.rows; j++){
index_IMG.push_back(i);
}
}
timeEnd = clock();
cout << "database calculate time "<<timeEnd - timeBegin;
Mat indices;
Mat dists;
int knn = 2;
flann::Index flannIndex(featurecluster, flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN);
//flannIndex.knnSearch(despSRC, indices, dists, knn, flann::SearchParams());
flannIndex.save("../../hehe");
flannIndex.knnSearch(despSRC, indices, dists, knn, flann::SearchParams());
/*
cout << "featurecluster " << featurecluster.rows << endl;
cout << "keyPoints1 " << keyPoints1.size() << endl;
cout << "despSRC " << despSRC.rows << endl;
cout <<"indices size" <<indices << endl;
cout << "dists" << dists << endl;
*/
//implement Lowe optimization and retrieve corresponding img
float nndrRatio = 0.6f;
const int maxMatchImgNum = 20;
struct ImgFreq{
int ImgIndex;
int Freq;
};
vector<ImgFreq>imgFreq;
for (int i = 0; i < indices.rows; i++){
if (dists.at<float>(i, 0) < dists.at<float>(i ...
(more)