Ask Your Question

How to use BOW file for testing

asked 2013-09-28 10:05:35 -0600

FLY gravatar image

updated 2013-10-01 15:43:43 -0600

I trained the data using BOG with SVM , i trained it , now i just want to open it through xml , i don't want to train it again and again on every run , I trained the svm and now loading it using its loading function

CvSVM svm;
svm.load( "trainsvm.xml" );

But how to do the same with vocabulary file which i use for clustering using BOG algorithm , below is my code , which is training the data , but i want to open it now like svm

for (i = files.begin(); i != files.end(); ++i)
    Mat row_img = imread( Dir +*i, 0 );

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);




Mat vocabulary = bow.cluster();


Mat training_mat(num_img , dictionarySize,CV_32FC1);
Mat labels(num_img,1,CV_32FC1);

This is clustering the data , now i don't want to run it again and again because it takes too much time , when it trained once , i just want to use it , so that i can speed up my program

Edit :

I used this approach in the above code and got error :

FileStorage fs;"Vocabulary.xml", FileStorage::WRITE);

Mat vocabulary = bow.cluster();

fs["Vocabulary"]>>vocabulary ; 

cv::Mat training_mat(num_img , dictionarySize,CV_32FC1);
cv::Mat labels(num_img,1,CV_32FC1);

CvSVM svm;
svm.load( "trainsvm.xml" );

Error :

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-09-29 11:10:18 -0600

Guanta gravatar image
edit flag offensive delete link more


@Guanta but i am also using svm with it , so do i need to deal with both files seprately ?

FLY gravatar imageFLY ( 2013-09-29 11:47:44 -0600 )edit

Yes. So far, you created a vocabulary to compute bag-of-words (BoW) descriptors from your local descriptors (SIFT or SURF or whatever you chose). Then you trained your svm with these BoW-descriptors. When you want to test your set-up with other images which you haven't seen before and for which you haven't computed the BoW-descriptors so far, you need to load the created vocabulary and set it to your dextract to compute them. Afterwards you can also load your svm and see what the output is.

Guanta gravatar imageGuanta ( 2013-09-30 04:14:33 -0600 )edit

... so don't create the vocabulary again for your image-test-set but use the old one!

Guanta gravatar imageGuanta ( 2013-09-30 04:18:18 -0600 )edit

+1 ,You want to deliver something like this FileStorage fs;"Vocalbury.xml", FileStorage::WRITE); Mat vocabulary = bow.cluster(); fs["Vocalbury"]>>vocabulary ; dextract.setVocabulary(vocabulary); Mat training_mat(num_img , dictionarySize,CV_32FC1); Mat labels(num_img,1,CV_32FC1);

FLY gravatar imageFLY ( 2013-09-30 07:20:37 -0600 )edit

For your edit (btw.: I prefer new questions instead of editing old ones): First of all you are using filestorage wrongly. For saving use: fs["Vocabulary"] << vocabulary ; and for loading fs["Vocabulary"] >> vocabulary ; . I don't know where your new error comes from, somewhere you must use flann and there you are using a wrong format of a matrix or a wrong option. I'd try to run the code in debug mode and see where the error comes from.

Guanta gravatar imageGuanta ( 2013-10-01 10:48:08 -0600 )edit

Question Tools


Asked: 2013-09-28 10:05:35 -0600

Seen: 854 times

Last updated: Oct 01 '13