Ask Your Question
0

Setting Vocabulary in creating "Bag of features" to do SVM classification

asked 2014-01-24 06:03:42 -0600

user1234 gravatar image

updated 2014-01-25 06:35:53 -0600

berak gravatar image

I am trying to do 3-class classification using SVM. For that, i am preparing vocabulary during the SVM training. But as i am getting random results during SVM prediction, so i suspect that there is some problem in my vocabulary creation method. My code to create vocabulary is following:

//Mat train --- it should contain the feature vectors
//Mat response-- it will contain the class labels

void svm::createTrainingDateUsingBOW(int flag,Mat& train, Mat& response, int label)
{

    int cluster = 9; // Common for all classes  
    cv::Mat imageForTraining;
    std::vector<cv::KeyPoint> keypoints; 
    cv::SurfFeatureDetector detector(500);
    cv::Ptr<cv::DescriptorExtractor> cvDescExt = new cv::SurfDescriptorExtractor();
    cv::Mat descriptors; 

    cv::BOWKMeansTrainer bow(cluster, cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, FLT_EPSILON), 1, cv::KMEANS_PP_CENTERS);
    cv::Mat vocabulary; 

    if (flag == 1)
    {
        for(int i=1; i<=400; i++)
        {
            counter++;
            cout<<"\n counter:  "<<counter;

            char filepath[255];
            sprintf(filepath, "class1/%d.JPG",i); // we need class1/1.JPG etc

            imageForTraining = cv::imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);

            // Preparing keypoints using detector
            detector.detect(imageForTraining, keypoints);

            // now getting the DESCRIPTORS for the given keypoints
            cvDescExt->compute(imageForTraining, keypoints, descriptors);

            //BOW
            if(keypoints.size() > cluster)  // so that (N<k) error won't come
            {
                if (!descriptors.empty()) bow.add(descriptors); 

                //VOCABULARY
                vocabulary = bow.cluster();

                cv::Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
                cv::Ptr<DescriptorMatcher> matcher = cv::DescriptorMatcher::create("FlannBased");
                cv::BOWImgDescriptorExtractor descExtractor (extractor, matcher);

                descExtractor.setVocabulary(vocabulary); 
                Mat bowDescriptors; 
                descExtractor.compute(imageForTraining, keypoints, bowDescriptors); 
                if ( !bowDescriptors.empty())
                {
                    train.push_back(bowDescriptors);
                    response.push_back(label);
                }

            }// ending if loop

        } // ending For loop    
    }// ending if(flag ) loop


    if (flag == 2)
    {
        for(int i=1; i<=400; i++)
        {
            counter++;
            cout<<"\n counter:  "<<counter;

            char filepath[255];
            sprintf(filepath, "class2/%d.JPG",i); // we need class1/1.JPG etc

            imageForTraining = cv::imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);

            // Preparing keypoints using detector
            detector.detect(imageForTraining, keypoints);

            // now getting the DESCRIPTORS for the given keypoints
            cvDescExt->compute(imageForTraining, keypoints, descriptors);

            //BOW
            if(keypoints.size() > cluster)  // so that (N<k) error won't come
            {
                if (!descriptors.empty()) bow.add(descriptors); 

                //VOCABULARY
                vocabulary = bow.cluster();

                cv::Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
                cv::Ptr<DescriptorMatcher> matcher = cv::DescriptorMatcher::create("FlannBased");
                cv::BOWImgDescriptorExtractor descExtractor (extractor, matcher);

                descExtractor.setVocabulary(vocabulary); 
                Mat bowDescriptors; 
                descExtractor.compute(imageForTraining, keypoints, bowDescriptors); 
                if ( !bowDescriptors.empty())
                {
                    train.push_back(bowDescriptors);
                    response.push_back(label);
                }

            }// ending if loop

        } // ending For loop    
    }// ending if(flag ) loop

....Similary for Class-3

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-01-25 06:13:32 -0600

Guanta gravatar image

The typical bag of words (or bag of features) works by using one visual vocabulary, i.e. you cluster once for all (or a random selection if they don't fit into memory) features of all your training images. Afterwards you compute for each training image a histogram and then train your classifier, see also http://answers.opencv.org/question/8677/image-comparison-with-a-database/#8686 for some additional information and links.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-01-24 06:03:42 -0600

Seen: 2,152 times

Last updated: Jan 25 '14