OpenCV 3.0分词故障(视觉词袋)

OpenCV 3.0 Segmentation Fault (Bag of visual words)

本文关键字:视觉 故障 分词 OpenCV      更新时间:2023-10-16

我正在尝试使用openCV 3.0建立一个视觉词包。我到处都看了一下,我似乎能找到的只是与2中的版本兼容的代码。x域。到目前为止,这是我所拥有的:

#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(int argc, const char** argv) {
    Ptr<FeatureDetector> features;
    Ptr<DescriptorExtractor> descriptors;
    Ptr<DescriptorMatcher> matcher;
    int MAX_ITER = 100;
    int EPS = 2;
    TermCriteria tc(MAX_ITER + EPS,1,0.001);
    int dictSize = 1000;
    int retries = 1;
    int flags = KMEANS_PP_CENTERS;
    BOWKMeansTrainer bowTrainer(dictSize,tc,retries,flags);
    BOWImgDescriptorExtractor bowDE(descriptors,matcher);

    Mat img1 = imread("/Users/Lucas/Desktop/pic2.jpg");
    Mat img2 = imread("/Users/Lucas/Desktop/2.jpg");
    vector<KeyPoint> keypoints,keypoints2;
    features->detect(img1, keypoints);
    features->detect(img2, keypoints2);

    Mat myFeatures;
    Mat myFeatures2;
    descriptors->compute(img1, keypoints, myFeatures);
    descriptors->compute(img2, keypoints2, myFeatures2);
    bowTrainer.add(myFeatures);
    bowTrainer.add(myFeatures2);
    Mat dictionary = bowTrainer.cluster();
    bowDE.setVocabulary(dictionary);
    cout << dictionary << endl;

    return 0;
}

我通过使用一些教程和片段把这些放在一起,但是我遇到了一个问题。当程序到达

features->detect(img1, keypoints);

退出时出现分段错误11,不管这意味着什么。有人能帮我指出我做错了什么吗?

你必须先创建你的FeatureDetector, DescriptorExtractor。现在,您得到了空指针实例(这是您的段错误)。

   #include <opencv2/xfeatures2d.hpp>
   ...
   Ptr<FeatureDetector> features = xfeatures2d::SIFT::create();
   Ptr<DescriptorExtractor> descriptors = xfeatures2d::SIFT::create();
   Ptr<DescriptorMatcher> matcher = makePtr<BFMatcher>(NORM_L2);

注意,由于必须使用SIFT或SURF,因此需要为这个

安装opencv_contrib repo。