Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to find matching image

Hello, my code is...

include "stdafx.h"

include <opencv cv.h="">

include <opencv highgui.h="">

include <opencv2 nonfree="" features2d.hpp="">

using namespace cv; using namespace std;

define DICTIONARY_BUILD 0 // set DICTIONARY_BUILD 1 to do Step 1, otherwise it goes to step 2

int _tmain(int argc, _TCHAR* argv[]) {
int minHessian = 400; //Hessian Threshold

if DICTIONARY_BUILD == 1

//Step 1 - Obtain the set of bags of features.

//to store the input file names

char filename[100]={};
//to store the current input image Mat input;

//To store the keypoints that will be extracted by SURF
vector<KeyPoint> keypoints;
//To store the SURF descriptor of current image
Mat descriptor;
//To store all the descriptors that are extracted from all the images.
Mat featuresUnclustered;
//The SURF feature extractor and descriptor 
SurfDescriptorExtractor detector(minHessian,4,2,false);     

//I select 20 (1000/50) images from 1000 images to extract feature descriptors and build the vocabulary
for(int f=1;f<25;f++){      
    //create the file name of an image
    sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image\\%i.jpg",f);
    //open the file
    input = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); //Load as grayscale  
//Mat img =cv::imread("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\22.jpg");
//cv::cvtColor(img,img,CV_BGR2GRAY);
    if(input.empty())
{
    cout << "Error: Image cannot be loaded !" << endl;
    system("Pause");
    return -1;
}
    //detect feature points
    detector.detect(input, keypoints);
    //compute the descriptors for each keypoint
    detector.compute(input, keypoints,descriptor);      
    //put the all feature descriptors in a single Mat object 
    featuresUnclustered.push_back(descriptor);      
    //print the percentage
    printf("%i percent done\n",f/10);
}   


//Construct BOWKMeansTrainer
//the number of bags
int dictionarySize=200;
//define Term Criteria
TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
//retries number
int retries=1;
//necessary flags
int flags=KMEANS_PP_CENTERS;
//Create the BoW (or BoF) trainer
BOWKMeansTrainer bowTrainer(dictionarySize,tc,retries,flags);
bowTrainer.add(featuresUnclustered);
if (featuresUnclustered.type() != CV_32F)

{ featuresUnclustered.convertTo(featuresUnclustered, CV_32F); } Mat vocabulary = bowTrainer.cluster(); //cluster the feature vectors //Mat dictionary=bowTrainer.cluster(featuresUnclustered);
//store the vocabulary FileStorage fs("C:\harshada\OpenCV BoFSURF\Release\image\dictionary.yml", FileStorage::WRITE); fs << "vocabulary" << vocabulary; fs.release();

else

//Step 2 - Obtain the BoF descriptor for given image/video frame. 

//prepare BOW descriptor extractor from the dictionary    
Mat dictionary; 
FileStorage fs("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\dictionary.yml", FileStorage::READ);
fs["vocabulary"] >> dictionary;
fs.release();   

//create a nearest neighbor matcher
Ptr<DescriptorMatcher> matcher(new FlannBasedMatcher);
//create SURF feature point extracter
Ptr<FeatureDetector> detector(new SurfFeatureDetector(minHessian,4,2,false));
//create SURF descriptor extractor
Ptr<DescriptorExtractor> extractor(new SurfDescriptorExtractor(minHessian,4,2,false));  
//create BoF (or BoW) descriptor extractor
BOWImgDescriptorExtractor bowDE(extractor,matcher);
//Set the dictionary with the vocabulary we created in the first step
bowDE.setVocabulary(dictionary);

//To store the image file name
char * filename = new char[100];
//To store the image tag name - only for save the descriptor in a file
char * imageTag = new char[10];

//open the file to write the resultant descriptor
FileStorage fs1("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\descriptor.yml", FileStorage::WRITE);    

//the image file with the location. 
sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image\\8.jpg");    


//read the image
Mat img=imread(filename,CV_LOAD_IMAGE_GRAYSCALE);       
//To store the keypoints that will be extracted by SURF
vector<KeyPoint> keypoints;     
//Detect SURF keypoints (or feature points)
detector->detect(img,keypoints);
//To store the BoW (or BoF) representation of the image
Mat bowDescriptor;      
//extract BoW (or BoF) descriptor from given image
bowDE.compute(img,keypoints,bowDescriptor);

cv::imshow("matcher",bowDescriptor);

//prepare the yml (some what similar to xml) file
sprintf(imageTag,"img1");           
//write the new BoF descriptor to the file
fs1 << imageTag << bowDescriptor;       

//You may use this descriptor for classifying the image.

//release the file storage
fs1.release();

endif

printf("\ndone\n"); 
return 0;

}

i want to extract similar image from vocabulary that was created in above code.this code run successfully creating two descriptors file.My question is that I don't understand that file "descriptr.yml" contains extracted features of query image or matched features of query image with vocabulary. Please help

click to hide/show revision 2
No.2 Revision

how to find matching image

Hello, my code is...

include "stdafx.h"

include <opencv cv.h="">

include <opencv highgui.h="">

include <opencv2 nonfree="" features2d.hpp="">

#include "stdafx.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/nonfree/features2d.hpp>

using namespace cv;
using namespace std;

define std; #define DICTIONARY_BUILD 0 // set DICTIONARY_BUILD 1 to do Step 1, otherwise it goes to step 2

2 int _tmain(int argc, _TCHAR* argv[]) {
int minHessian = 400; //Hessian Threshold

if Threshold #if DICTIONARY_BUILD == 1

1
//Step 1 - Obtain the set of bags of features.
 //to store the input file names

char filename[100]={};
//to store the current input image Mat input;

 //To store the keypoints that will be extracted by SURF
 vector<KeyPoint> keypoints;
 //To store the SURF descriptor of current image
 Mat descriptor;
 //To store all the descriptors that are extracted from all the images.
 Mat featuresUnclustered;
 //The SURF feature extractor and descriptor
 SurfDescriptorExtractor detector(minHessian,4,2,false);
 //I select 20 (1000/50) images from 1000 images to extract feature descriptors and build the vocabulary
 for(int f=1;f<25;f++){
  //create the file name of an image
 sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image\\%i.jpg",f);
 //open the file
  input = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); //Load as grayscale
 //Mat img =cv::imread("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\22.jpg");
 //cv::cvtColor(img,img,CV_BGR2GRAY);
 if(input.empty())
 {
  cout << "Error: Image cannot be loaded !" << endl;
 system("Pause");
  return -1;
 }
  //detect feature points
 detector.detect(input, keypoints);
  //compute the descriptors for each keypoint
 detector.compute(input, keypoints,descriptor);
  //put the all feature descriptors in a single Mat object
 featuresUnclustered.push_back(descriptor);
 //print the percentage
  printf("%i percent done\n",f/10);
 }
 //Construct BOWKMeansTrainer
 //the number of bags
 int dictionarySize=200;
 //define Term Criteria
 TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
 //retries number
 int retries=1;
 //necessary flags
 int flags=KMEANS_PP_CENTERS;
 //Create the BoW (or BoF) trainer
 BOWKMeansTrainer bowTrainer(dictionarySize,tc,retries,flags);
 bowTrainer.add(featuresUnclustered);
 if (featuresUnclustered.type() != CV_32F)

{ featuresUnclustered.convertTo(featuresUnclustered, CV_32F); } Mat vocabulary = bowTrainer.cluster(); //cluster the feature vectors //Mat dictionary=bowTrainer.cluster(featuresUnclustered);
//store the vocabulary FileStorage fs("C:\harshada\OpenCV BoFSURF\Release\image\dictionary.yml", fs("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\dictionary.yml", FileStorage::WRITE); fs << "vocabulary" << vocabulary; fs.release();

else

fs.release();
#else
//Step 2 - Obtain the BoF descriptor for given image/video frame.
 //prepare BOW descriptor extractor from the dictionary
 Mat dictionary;
 FileStorage fs("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\dictionary.yml", FileStorage::READ);
 fs["vocabulary"] >> dictionary;
 fs.release();
 //create a nearest neighbor matcher
 Ptr<DescriptorMatcher> matcher(new FlannBasedMatcher);
 //create SURF feature point extracter
 Ptr<FeatureDetector> detector(new SurfFeatureDetector(minHessian,4,2,false));
 //create SURF descriptor extractor
 Ptr<DescriptorExtractor> extractor(new SurfDescriptorExtractor(minHessian,4,2,false));
 //create BoF (or BoW) descriptor extractor
 BOWImgDescriptorExtractor bowDE(extractor,matcher);
 //Set the dictionary with the vocabulary we created in the first step
 bowDE.setVocabulary(dictionary);
 //To store the image file name
 char * filename = new char[100];
 //To store the image tag name - only for save the descriptor in a file
 char * imageTag = new char[10];
 //open the file to write the resultant descriptor
 FileStorage fs1("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\descriptor.yml", FileStorage::WRITE);
 //the image file with the location.
 sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image\\8.jpg");
 //read the image
 Mat img=imread(filename,CV_LOAD_IMAGE_GRAYSCALE);
 //To store the keypoints that will be extracted by SURF
 vector<KeyPoint> keypoints;
 //Detect SURF keypoints (or feature points)
 detector->detect(img,keypoints);
 //To store the BoW (or BoF) representation of the image
 Mat bowDescriptor;
 //extract BoW (or BoF) descriptor from given image
 bowDE.compute(img,keypoints,bowDescriptor);
 cv::imshow("matcher",bowDescriptor);
 //prepare the yml (some what similar to xml) file
 sprintf(imageTag,"img1");
 //write the new BoF descriptor to the file
 fs1 << imageTag << bowDescriptor;
 //You may use this descriptor for classifying the image.
 //release the file storage
 fs1.release();
#endif
printf("\ndone\n");
return 0;
}

endif

printf("\ndone\n"); 
return 0;

}

i want to extract similar image from vocabulary that was created in above code.this code run successfully creating two descriptors file.My question is that I don't understand that file "descriptr.yml" contains extracted features of query image or matched features of query image with vocabulary. Please help

click to hide/show revision 3
No.3 Revision

updated 2016-03-02 07:30:38 -0600

berak gravatar image

how to find matching image

Hello, my code is...

#include "stdafx.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/nonfree/features2d.hpp>

using namespace cv;
using namespace std;

#define DICTIONARY_BUILD 0 // set DICTIONARY_BUILD 1 to do Step 1, otherwise it goes to step 2

int _tmain(int argc, _TCHAR* argv[])
{   
int minHessian = 400; //Hessian Threshold
#if DICTIONARY_BUILD == 1

    //Step 1 - Obtain the set of bags of features.

    //to store the input file names
char  filename[100]={};     
    //to store the current input image
    Mat input;  

    //To store the keypoints that will be extracted by SURF
    vector<KeyPoint> keypoints;
    //To store the SURF descriptor of current image
    Mat descriptor;
    //To store all the descriptors that are extracted from all the images.
    Mat featuresUnclustered;
    //The SURF feature extractor and descriptor 
    SurfDescriptorExtractor detector(minHessian,4,2,false);     

    //I select 20 (1000/50) images from 1000 images to extract feature descriptors and build the vocabulary
    for(int f=1;f<25;f++){      
        //create the file name of an image
        sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image\\%i.jpg",f);
        //open the file
        input = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); //Load as grayscale  
    //Mat img =cv::imread("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\22.jpg");
    //cv::cvtColor(img,img,CV_BGR2GRAY);
        if(input.empty())
    {
        cout << "Error: Image cannot be loaded !" << endl;
        system("Pause");
        return -1;
    }
        //detect feature points
        detector.detect(input, keypoints);
        //compute the descriptors for each keypoint
        detector.compute(input, keypoints,descriptor);      
        //put the all feature descriptors in a single Mat object 
        featuresUnclustered.push_back(descriptor);      
        //print the percentage
        printf("%i percent done\n",f/10);
    }   


    //Construct BOWKMeansTrainer
    //the number of bags
    int dictionarySize=200;
    //define Term Criteria
    TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
    //retries number
    int retries=1;
    //necessary flags
    int flags=KMEANS_PP_CENTERS;
    //Create the BoW (or BoF) trainer
    BOWKMeansTrainer bowTrainer(dictionarySize,tc,retries,flags);
    bowTrainer.add(featuresUnclustered);
    if (featuresUnclustered.type() != CV_32F)
{
    featuresUnclustered.convertTo(featuresUnclustered, CV_32F);
}
Mat vocabulary = bowTrainer.cluster();
    //cluster the feature vectors
    //Mat dictionary=bowTrainer.cluster(featuresUnclustered);   
    //store the vocabulary
    FileStorage fs("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\dictionary.yml", FileStorage::WRITE);
    fs << "vocabulary" << vocabulary;
    fs.release();

#else
    //Step 2 - Obtain the BoF descriptor for given image/video frame. 

    //prepare BOW descriptor extractor from the dictionary    
    Mat dictionary; 
    FileStorage fs("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\dictionary.yml", FileStorage::READ);
    fs["vocabulary"] >> dictionary;
    fs.release();   

    //create a nearest neighbor matcher
    Ptr<DescriptorMatcher> matcher(new FlannBasedMatcher);
    //create SURF feature point extracter
    Ptr<FeatureDetector> detector(new SurfFeatureDetector(minHessian,4,2,false));
    //create SURF descriptor extractor
    Ptr<DescriptorExtractor> extractor(new SurfDescriptorExtractor(minHessian,4,2,false));  
    //create BoF (or BoW) descriptor extractor
    BOWImgDescriptorExtractor bowDE(extractor,matcher);
    //Set the dictionary with the vocabulary we created in the first step
    bowDE.setVocabulary(dictionary);

    //To store the image file name
    char * filename = new char[100];
    //To store the image tag name - only for save the descriptor in a file
    char * imageTag = new char[10];

    //open the file to write the resultant descriptor
    FileStorage fs1("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\descriptor.yml", FileStorage::WRITE);    

    //the image file with the location. 
    sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image\\8.jpg");    


    //read the image
    Mat img=imread(filename,CV_LOAD_IMAGE_GRAYSCALE);       
    //To store the keypoints that will be extracted by SURF
    vector<KeyPoint> keypoints;     
    //Detect SURF keypoints (or feature points)
    detector->detect(img,keypoints);
    //To store the BoW (or BoF) representation of the image
    Mat bowDescriptor;      
    //extract BoW (or BoF) descriptor from given image
    bowDE.compute(img,keypoints,bowDescriptor);

    cv::imshow("matcher",bowDescriptor);

    //prepare the yml (some what similar to xml) file
    sprintf(imageTag,"img1");           
    //write the new BoF descriptor to the file
    fs1 << imageTag << bowDescriptor;       

    //You may use this descriptor for classifying the image.

    //release the file storage
    fs1.release();
#endif
    printf("\ndone\n"); 
    return 0;
}

i want to extract similar image from vocabulary that was created in above code.this code run successfully creating two descriptors file.My question is that I don't understand that file "descriptr.yml" contains extracted features of query image or matched features of query image with vocabulary. Please help

my code is...

#include "stdafx.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/nonfree/features2d.hpp>

using namespace cv;
using namespace std;

#define DICTIONARY_BUILD 0 // set DICTIONARY_BUILD 1 to do Step 1, otherwise it goes to step 2

int _tmain(int argc, _TCHAR* argv[])
{   
int minHessian = 400; //Hessian Threshold
#if DICTIONARY_BUILD == 1

    //Step 1 - Obtain the set of bags of features.

    //to store the input file names
char  filename[100]={};     
    //to store the current input image
    Mat input;  

    //To store the keypoints that will be extracted by SURF
    vector<KeyPoint> keypoints;
    //To store the SURF descriptor of current image
    Mat descriptor;
    //To store all the descriptors that are extracted from all the images.
    Mat featuresUnclustered;
    //The SURF feature extractor and descriptor 
    SurfDescriptorExtractor detector(minHessian,4,2,false);     

    //I select 20 (1000/50) images from 1000 images to extract feature descriptors and build the vocabulary
    for(int f=1;f<25;f++){      
        //create the file name of an image
        sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image\\%i.jpg",f);
        //open the file
        input = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); //Load as grayscale  
    //Mat img =cv::imread("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\22.jpg");
    //cv::cvtColor(img,img,CV_BGR2GRAY);
        if(input.empty())
    {
        cout << "Error: Image cannot be loaded !" << endl;
        system("Pause");
        return -1;
    }
        //detect feature points
        detector.detect(input, keypoints);
        //compute the descriptors for each keypoint
        detector.compute(input, keypoints,descriptor);      
        //put the all feature descriptors in a single Mat object 
        featuresUnclustered.push_back(descriptor);      
        //print the percentage
        printf("%i percent done\n",f/10);
    }   


    //Construct BOWKMeansTrainer
    //the number of bags
    int dictionarySize=200;
    //define Term Criteria
    TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
    //retries number
    int retries=1;
    //necessary flags
    int flags=KMEANS_PP_CENTERS;
    //Create the BoW (or BoF) trainer
    BOWKMeansTrainer bowTrainer(dictionarySize,tc,retries,flags);
    bowTrainer.add(featuresUnclustered);
    if (featuresUnclustered.type() != CV_32F)
{
    featuresUnclustered.convertTo(featuresUnclustered, CV_32F);
}
Mat vocabulary = bowTrainer.cluster();
    //cluster the feature vectors
    //Mat dictionary=bowTrainer.cluster(featuresUnclustered);   
    //store the vocabulary
    FileStorage fs("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\dictionary.yml", FileStorage::WRITE);
    fs << "vocabulary" << vocabulary;
    fs.release();

#else
    //Step 2 - Obtain the BoF descriptor for given image/video frame. 

    //prepare BOW descriptor extractor from the dictionary    
    Mat dictionary; 
    FileStorage fs("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\dictionary.yml", FileStorage::READ);
    fs["vocabulary"] >> dictionary;
    fs.release();   

    //create a nearest neighbor matcher
    Ptr<DescriptorMatcher> matcher(new FlannBasedMatcher);
    //create SURF feature point extracter
    Ptr<FeatureDetector> detector(new SurfFeatureDetector(minHessian,4,2,false));
    //create SURF descriptor extractor
    Ptr<DescriptorExtractor> extractor(new SurfDescriptorExtractor(minHessian,4,2,false));  
    //create BoF (or BoW) descriptor extractor
    BOWImgDescriptorExtractor bowDE(extractor,matcher);
    //Set the dictionary with the vocabulary we created in the first step
    bowDE.setVocabulary(dictionary);

    //To store the image file name
    char * filename = new char[100];
    //To store the image tag name - only for save the descriptor in a file
    char * imageTag = new char[10];

    //open the file to write the resultant descriptor
    FileStorage fs1("C:\\harshada\\OpenCV BoFSURF\\Release\\image\\descriptor.yml", FileStorage::WRITE);    

    //the image file with the location. 
    sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image\\8.jpg");    


    //read the image
    Mat img=imread(filename,CV_LOAD_IMAGE_GRAYSCALE);       
    //To store the keypoints that will be extracted by SURF
    vector<KeyPoint> keypoints;     
    //Detect SURF keypoints (or feature points)
    detector->detect(img,keypoints);
    //To store the BoW (or BoF) representation of the image
    Mat bowDescriptor;      
    //extract BoW (or BoF) descriptor from given image
    bowDE.compute(img,keypoints,bowDescriptor);

    cv::imshow("matcher",bowDescriptor);

    //prepare the yml (some what similar to xml) file
    sprintf(imageTag,"img1");           
    //write the new BoF descriptor to the file
    fs1 << imageTag << bowDescriptor;       

    //You may use this descriptor for classifying the image.

    //release the file storage
    fs1.release();
#endif
    printf("\ndone\n"); 
    return 0;
}