c++单词袋-OpenCV:断言失败
c++ Bag Of Words - OpenCV: Assertion Failed
我正在努力掌握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 donen",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("ndonen");
return 0;
}
但后来它抛出了这个:
OpenCV错误:在CV::kmeans,文件C:\buildslave64\win64_amdoc1\2_4_PackSlave-win32-vc11-shared\OpenCV\modules\core\src\matrix.cpp,第2701行中断言失败(data.dims<=2&type=CV_32F&&K>0)
请帮帮我。
编辑
它中断的线路:
dictionary = bowTrainer.cluster(featuresUnclustered); // -- Breaks
编辑2
我遇到过这个问题,但我不确定如何翻译它来帮助我的事业。
我不能100%确定代码在做什么,因为我不是OpenCV专家。但是,我可以看到,您并没有以任何方式初始化input
。这可能会导致你得不到你想要的描述符,从而什么都没做。然后代码可能会中断,因为它需要实际的数据,但没有。
一般来说,在处理OpenCV或其他"有点混乱"的大型库时,我建议您循序渐进,并检查每一步的结果是否符合您的期望。复制粘贴一大块代码并期望它工作从来都不是最好的做法。
if (allDescriptors.type() != CV_32F)
{
allDescriptors.convertTo(allDescriptors, CV_32F);
}
确保第一步中的图像目录是正确的。它应该以0.jpg、50.jpg等形式存在训练图像。因为在很多情况下,当图像未加载时会出现此错误。您可以在imread之后添加以下代码进行检查。希望它能起作用。
if(input.empty())
{
cout << "Error: Image cannot be loaded !" << endl;
system("Pause");
return -1;
}
相关文章:
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- uint_not_usable_without_attribute在业力规则中使用数字生成器时静态断言失败
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 在 CppUnit 中测试中止断言失败
- 使用扫描的调试断言失败
- 为什么我的Qt程序在断言失败后继续运行?
- 图片不显示,关闭时出错 --> 调试断言失败!表达式:is_block_type_valid(标头>_block_use)
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 错误:断言失败 (src.type() == CV_8UC1) 在阈值中
- Boost Beast 异步服务器失败,断言失败:(id_ != T::id) 在多个 aync 调用中
- 使用imwrite OpenCV时断言失败
- 调试断言失败的缓冲区!=nullptr
- 使用 ofstream 写入文本文件时断言失败
- 提升继续恢复断言失败
- 获取有关调试断言失败的错误:表达式:"(_Ptr_user &(_BIG_ALLOCATION_ALIGNMENT -1)) == 0" &&0
- OpenCV(3.4.1) 错误:断言失败(变暗 <= 2 && step[0] > 0) in cv::Mat::locateROI
- 在Win10上使用带有Qt5的OpenCV3.0,调试断言失败
- MFC GetDC() 断言失败 - "Not a window"
- OpenCV 错误:断言失败 - 矩阵减去