我试图下载一套用于功能袋的代码。并尝试了一下,尽管我建议将代码与原始代码(使用SURF检测器和提取器)对比使用SIFT检测器。我的词汇发现中存在kmeans的一些未知问题 阶段。该程序会自行终止,并在某个内存位置声明未知源
有人可以帮助检查代码有什么问题吗?我试图逐行编码出另一套特征编码袋。这次没有这样的问题。
我也可以查询
谢谢
bool createVocabulary(const DatabaseType& trainingDb,
cv::Mat& outVocabulary)
{
CV_Assert(!trainingDb.empty());
cv::Ptr<cv::xfeatures2d::SIFT> detector = cv::xfeatures2d::SIFT::create();
cv::Ptr<cv::DescriptorExtractor> extractor = cv::xfeatures2d::SIFT::create();
cv::Mat trainingDescriptors(1, extractor->descriptorSize(), extractor->descriptorType());
outVocabulary.create(0, 1, CV_32FC1);
int counter = 0;
for (auto& it : trainingDb)
{
std::vector<cv::KeyPoint> keypoints;
detector->detect(it.second, keypoints);
if (!keypoints.empty())
{
cv::Mat descriptors;
extractor->compute(it.second, keypoints, descriptors);
if (!descriptors.empty())
{
std::cout << "Image " << counter
<< "- Adding " << descriptors.rows << " training descriptors." << std::endl;
counter++;
trainingDescriptors.push_back(descriptors);
}
else
{
std::cout << "- No descriptors found." << std::endl;
}
}
else
{
std::cout << "- No keypoints found." << std::endl;
}
}
if (trainingDescriptors.empty())
{
std::cout << "- Training descriptors are empty." << std::endl;
return false;
}
std::cout << "Total Descriptors extracted " << trainingDescriptors.rows << std::endl;
//Construct BOWKMeansTrainer
//the number of bags
int dictionarySize=200;
//define Term Criteria
cv::TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
//retries number
int retries= 3;
//necessary flags
int flags=cv::KMEANS_PP_CENTERS;
//Create the BoW (or BoF) trainer
cv::BOWKMeansTrainer bowTrainer(dictionarySize,tc,retries,flags);
bowTrainer.add(trainingDescriptors);
//cluster the feature vectors
outVocabulary=bowTrainer.cluster();
return true;
}