Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi, I am trying to use bowtrainer for image classification(opencv 2.4.6 + VS2010). I am getting an error.

"OpenCV Error: Assertion failed (N >=k) in unknown function, file ......\src\opencv\modules\core\src\matrix.cpp, line 2717"

Here is the code:

cv::initModule_nonfree();
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
SurfFeatureDetector detector(500);

TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int dictionarySize = 1500;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor bowDE(extractor, matcher);

Mat im1 = imread("image1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat im2 = imread("image2.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat descriptor1,descriptor2;
vector<KeyPoint> keypoints1,keypoints2;

detector.detect(im1, keypoints1);
detector.compute(im1, keypoints1,descriptor1);
detector.detect(im2, keypoints2);
extractor->compute(im2, keypoints2,descriptor2);
bowTrainer.add(descriptor1);
bowTrainer.add(descriptor2);
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);