Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

congratulations, you've come quite far already !

after you successfully extracted bowDescriptors for your images, you can start training some machinelearning with those. since you want the closest 10-15 images returned from your matching, we'll use a flann::Index:

static cv::Ptr<cv::flann::Index> train_index(const Mat &trainData)
{
    // binary descriptor (ORB,BRISK,..)
    if (trainData.type() == CV_8U)
    {
        return makePtr<cv::flann::Index>(trainData,
               cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_HAMMING);
    }
    // float descriptor (SIFT,SURF,..)
    return makePtr<cv::flann::Index>(trainData,
           cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);
}

Mat getBow(const Mat &img, Ptr<FeatureDetector> detector, BOWImgDescriptorExtractor &bowDE)
{
    //Detect SIFT keypoints (or feature points)
    vector<KeyPoint> keypoints;             
    detector->detect(img,keypoints);

    //extract BoW (or BoF) descriptor from given image
    Mat bowDescriptor;             
    bowDE.compute(img,keypoints,bowDescriptor);

    return bowDescriptor;
}


// 1.a: collect train data (from your image db):
Mat trainData;
for (int i=0; i<numTrainImages; i++)
{
     Mat bowFeature = getBow(trainimage, detector, bowDE);
     trainData.push_back(bowFeature.reshape(1,1));
}

// 1.b: train your index:
Ptr<cv::flann::Index> index  = train_index(trainData);

// 2.a: prepare test feature:
Mat bowTest = getBow(testimage, detector, bowDE);

// 2.b: no you can predict closest items:
int K=15;
cv::Mat dists, indices;
index->knnSearch(bowTest, indices, dists, K);

cerr << indices << endl;

there's a caveat here: you have to make sure, your trainData is still valid, when you're trying to predict (the flann code is running away with a raw pointer to the float data)

congratulations, you've come quite far already !

after you successfully extracted bowDescriptors for your images, you can start training some machinelearning with those. since you want the closest 10-15 images returned from your matching, we'll use a flann::Index:

static cv::Ptr<cv::flann::Index> train_index(const Mat &trainData)
{
    // binary descriptor (ORB,BRISK,..)
    if (trainData.type() == CV_8U)
    {
        return makePtr<cv::flann::Index>(trainData,
               cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_HAMMING);
    }
    // float descriptor (SIFT,SURF,..)
    return makePtr<cv::flann::Index>(trainData,
           cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);
}

Mat getBow(const Mat &img, Ptr<FeatureDetector> detector, BOWImgDescriptorExtractor &bowDE)
{
    //Detect SIFT keypoints (or feature points)
    vector<KeyPoint> keypoints;             
    detector->detect(img,keypoints);

    //extract BoW (or BoF) descriptor from given image
    Mat bowDescriptor;             
    bowDE.compute(img,keypoints,bowDescriptor);

    return bowDescriptor;
}


// 1.a: collect train data (from your image db):
Mat trainData;
for (int i=0; i<numTrainImages; i++)
{
     Mat bowFeature = getBow(trainimage, detector, bowDE);
     trainData.push_back(bowFeature.reshape(1,1));
}

// 1.b: train your index:
Ptr<cv::flann::Index> index  = train_index(trainData);

// 2.a: prepare test feature:
Mat bowTest = getBow(testimage, detector, bowDE);

// 2.b: no you can predict closest items:
int K=15;
cv::Mat dists, indices;
index->knnSearch(bowTest, indices, dists, K);

cerr << indices << endl;

there's a

caveat here: you have to make sure, your trainData is still valid, when you're trying to predict (the flann code is running away with a raw pointer to the float data)