【问题标题】:c++ Bag Of Words - OpenCV: Assertion Failedc++ 词袋 - OpenCV:断言失败
【发布时间】:2014-12-15 14:14:51
【问题描述】:

我正在尝试掌握 c++ 中的 Bag Of Words 并且我有一些示例代码,但是这个错误一直在抛出它,我不知道为什么。

我对此完全陌生,非常迷茫。

这是完整的代码:

#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 to do Step 1, otherwise it goes to step 2

int _tmain(int argc, _TCHAR* argv[])
{   
#if DICTIONARY_BUILD == 1

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

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

//To store the keypoints that will be extracted by SIFT
vector<KeyPoint> keypoints;
//To store the SIFT descriptor of current image
Mat descriptor;
//To store all the descriptors that are extracted from all the images.
Mat featuresUnclustered;
//The SIFT feature extractor and descriptor
SiftDescriptorExtractor detector;   

//I select 20 (1000/50) images from 1000 images to extract feature descriptors and build the vocabulary
for(int f=0;f<999;f+=50){       
    //create the file name of an image
    sprintf(filename,"G:\\testimages\\image\\%i.jpg",f);

    //open the file
    input = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); // -- Forgot to add in

    //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);
//cluster the feature vectors
Mat dictionary;


dictionary=bowTrainer.cluster(featuresUnclustered); // -- BREAKS


//store the vocabulary
FileStorage fs("dictionary.yml", FileStorage::WRITE);
fs << "vocabulary" << dictionary;
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("dictionary.yml", FileStorage::READ);
fs["vocabulary"] >> dictionary;
fs.release();   

//create a nearest neighbor 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 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("descriptor.yml", FileStorage::WRITE);  

//the image file with the location. change it according to your image file location
sprintf(filename,"G:\\testimages\\image\\1.jpg");       
//read the image
Mat img=imread(filename,CV_LOAD_IMAGE_GRAYSCALE);       
//To store the keypoints that will be extracted by SIFT
vector<KeyPoint> keypoints;     
//Detect SIFT 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);

//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;
}

但后来它抛出了这个:

OpenCV 错误:在 cv::kmeans、文件 C:\buildslave64\win64_amdoc1\2_4_PackSlave-win32-vc11-shared\ 中断言失败(data.dims 0) opencv\modules\core\src\matrix.cpp,第 2701 行

请帮忙。

编辑

它中断的行:

dictionary = bowTrainer.cluster(featuresUnclustered); // -- Breaks

编辑 2

Ive come across this,但我不确定如何翻译它以帮助我的事业。

【问题讨论】:

  • 因此,您可以做的一件事是找出触发断言的行。之后,您将知道哪个功能正在破坏。然后您可以进入 OpenCV 文档并阅读有关该功能的信息,并查看您使用的参数之一是否不尊重其接口。
  • @Svalorzen 已编辑以显示中断它的行
  • @MLMLTL 与您的问题无关,但您确实应该使用std::string 而不是那些对new char[] 的调用。
  • 请检查一下,如果 imgs 实际加载:if (input.empty()) ... cerr &lt;&lt; featuresUnclustered.size(); 在集群调用之前

标签: c++ opencv


【解决方案1】:

我不是 100% 确定代码在做什么,因为我不是 OpenCV 专家。但是我可以看到您没有以任何方式初始化input。这可能会导致你没有得到你想要的描述符,因此没有真正做任何事情。然后代码可能会中断,因为它需要实际数据,但没有。

一般来说,在处理 OpenCV 或其他大型“杂乱”库时,我建议您逐步进行,并检查结果是否符合您对每一步的预期。复制粘贴一大段代码并期望它能够正常工作从来都不是最好的做法。

【讨论】:

  • 输入 = imread(filename,0); // 是的,这是缺失的。很好发现!
  • 我不知道为什么在我复制代码时它没有出现(它最初在那里 - 很可能在玩弄代码以使其工作时丢失它),但即使使用它添加了,它没有多大用处。 @Svaloren
  • @MLMLTL 问题源于 OpenCV 代码中的一个断言,该断言检查特定矩阵(我认为它是 featuresUnclustered)是否具有合理的值;特别是它必须有dims&lt;=2,类型为CV_32F,并且您的集群使用K&gt;0。您必须检查代码中的变量回溯,并确定您正在处理的值是否正常。我不知道您正在使用的调用对您的输入数据会产生什么影响,因此您比我更有能力找出值出错的地方。
【解决方案2】:
if (allDescriptors.type() != CV_32F)
{
    allDescriptors.convertTo(allDescriptors, CV_32F);
}

【讨论】:

    【解决方案3】:

    确保您在第一步中的图像目录是正确的。它应该存在训练图像为0.jpg,50.jpg,...等。原因在很多情况下,当图像未加载时会发生此错误。您可以在 imread 后添加以下代码进行检查。希望它可以工作。

        if(input.empty())
        {
            cout << "Error: Image cannot be loaded !" << endl;
            system("Pause");
            return -1;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      • 2016-07-21
      • 1970-01-01
      • 2012-11-26
      相关资源
      最近更新 更多