Sunday 15 April 2012

c++ Bag Of Words - OpenCV: Assertion Failed -



c++ Bag Of Words - OpenCV: Assertion Failed -

i'm trying grips handbag of words in c++ , have sample code, error keeps on throwing , don't know why.

i'm new , much lost.

here's entirety of code:

#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 1 // set dictionary_build 1 step 1, otherwise goes step 2 int _tmain(int argc, _tchar* argv[]) { #if dictionary_build == 1 //step 1 - obtain set of bags of features. //to store input file names char * filename = new char[100]; //to store current input image mat input; //to store keypoints extracted sift vector<keypoint> keypoints; //to store sift descriptor of current image mat descriptor; //to store descriptors extracted images. mat featuresunclustered; //the sift feature extractor , descriptor siftdescriptorextractor detector; //i select 20 (1000/50) images 1000 images extract feature descriptors , build vocabulary for(int f=0;f<999;f+=50){ //create file name of image sprintf(filename,"g:\\testimages\\image\\%i.jpg",f); //open file input = imread(filename, cv_load_image_grayscale); // -- forgot add together in //detect feature points detector.detect(input, keypoints); //compute descriptors each keypoint detector.compute(input, keypoints,descriptor); //put feature descriptors in single mat object featuresunclustered.push_back(descriptor); //print 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 bow (or bof) trainer bowkmeanstrainer bowtrainer(dictionarysize,tc,retries,flags); //cluster feature vectors mat dictionary; dictionary=bowtrainer.cluster(featuresunclustered); // -- breaks //store vocabulary filestorage fs("dictionary.yml", filestorage::write); fs << "vocabulary" << dictionary; fs.release(); #else //step 2 - obtain bof descriptor given image/video frame. //prepare bow descriptor extractor dictionary mat dictionary; filestorage fs("dictionary.yml", filestorage::read); fs["vocabulary"] >> dictionary; fs.release(); //create nearest neighbour matcher ptr<descriptormatcher> matcher(new flannbasedmatcher); //create sift feature point extracter ptr<featuredetector> detector(new siftfeaturedetector()); //create sift descriptor extractor ptr<descriptorextractor> extractor(new siftdescriptorextractor); //create bof (or bow) descriptor extractor bowimgdescriptorextractor bowde(extractor,matcher); //set dictionary vocabulary created in first step bowde.setvocabulary(dictionary); //to store image file name char * filename = new char[100]; //to store image tag name - save descriptor in file char * imagetag = new char[10]; //open file write resultant descriptor filestorage fs1("descriptor.yml", filestorage::write); //the image file location. alter according image file location sprintf(filename,"g:\\testimages\\image\\1.jpg"); //read image mat img=imread(filename,cv_load_image_grayscale); //to store keypoints extracted sift vector<keypoint> keypoints; //detect sift keypoints (or feature points) detector->detect(img,keypoints); //to store bow (or bof) representation of image mat bowdescriptor; //extract bow (or bof) descriptor given image bowde.compute(img,keypoints,bowdescriptor); //prepare yml (some similar xml) file sprintf(imagetag,"img1"); //write new bof descriptor file fs1 << imagetag << bowdescriptor; //you may utilize descriptor classifying image. //release file storage fs1.release(); #endif printf("\ndone\n"); homecoming 0; }

but throws up:

opencv error: assertion failed (data.dims <= 2 && type == cv_32f && k > 0) in cv::kmeans, file c:\buildslave64\win64_amdoc1\2_4_packslave-win32-vc11-shared\opencv\modules\core\src\matrix.cpp, line 2701

help, please.

edit

line breaks on:

dictionary = bowtrainer.cluster(featuresunclustered); // -- breaks

edit 2

ive come across this, unsure how translate help cause.

i'm not 100% sure of code doing since i'm not opencv expert. can see not initializing input in way. results in not getting descriptors want, , not doing anything. code breaks since expects actual info in, there none.

in general, when dealing opencv or other big "kind of messy" libraries advise proceed step step, , checking results expect every step of way. copy-pasting big blob of code , expecting work never best course of study of action.

c++ opencv

No comments:

Post a Comment