【发布时间】:2016-07-23 04:42:20
【问题描述】:
我正在尝试结合 SIFT 特征、视觉词袋和 SVM 对图像进行分类。
现在我在训练部分。我需要为每个训练图像获取 BoW 直方图才能训练 SVM。为此,我使用来自 OpenCV 的 BOWImgDescriptorExtractor。我正在使用 OpenCV 3.1.0 版。
问题是它为某些图像计算直方图,但对于某些图像它给了我这个错误:
OpenCV Error: Assertion failed (queryIdx == (int)i) in compute,
file /Users/opencv-3.1.0/modules/features2d/src/bagofwords.cpp, line 200
libc++abi.dylib: terminating with uncaught exception of type
cv::Exception: /Users/opencv-3.1.0/modules/feature/src/bagofwords.cpp:200: error: (-215) queryIdx == (int)i in function compute
训练图像大小相同,通道数相同。 为了创建字典,我使用另一个图像集而不是训练 SVM。
下面是部分代码:
Ptr<FeatureDetector> detector(cv::xfeatures2d::SIFT::create());
Ptr<DescriptorMatcher> matcher(new BFMatcher(NORM_L2, true));
BOWImgDescriptorExtractor bow_descr(det, matcher);
bow_descr.setVocabulary(dict);
Mat features_svm;
for (int i = 0; i < num_svm_data; ++i) {
Mat hist;
std::vector<KeyPoint> keypoints;
detector->detect(data_svm[i], keypoints);
bow_descr.compute(data_svm[i], keypoints, hist);
features_svm.push_back(hist);
}
data_svm 是 vector<Mat> 类型。这是我将在 SVM 中使用的训练集图像。
问题可能是什么?
【问题讨论】:
-
在调用计算方法之前打印
data[i]、keypoints和hist的值。他们有什么奇怪的地方吗? -
@DanM,我正在这样做。实际上,我在带有
drawKeypoints()功能的图像上显示keypoints,hist用于程序给我结果看起来不错的图像,keypoints用于所有图像看起来都不错,data[i]这是一张图像也看起来不错. -
@DanM,
hist在调用计算方法之前为空。 -
是的,那个应该是输出,所以没关系。查看源代码:
sources\modules\features2d\src\bagofwords.cpp从第 143 行开始。首先它使用 SIFT 计算描述符,然后使用 BFMatcher 查找匹配项。处理这些匹配然后会导致错误。也许关键点对于这张照片来说不够好?尝试自己计算并检查结果(或在 opencv 代码中中断)。也许尝试调整检测器或匹配器的参数,或者尝试不同的算法? -
@DanM,你说的“关键点对这张照片来说不够好”是什么意思?我已经看过源代码,但我不明白
matches[i].queryIdx、matches[i].trainIdx的含义。你能给我解释一下吗?
标签: c++ opencv image-processing svm sift