OpenCV - 程序停止运行@检测图像关键点的步骤

时间:2013-11-23 09:05:02

标签: c++ opencv

我正在尝试使用opencv库开发一个包含可视单词的c ++实现

我开始测试关键点检测功能,但是当我插入此指令时

detector->detect(img, keypoints);,程序停止运行@第一个图像而不会抛出任何异常。

任何人都可以帮我解决这个问题吗?

谢谢

void extractTrainingVocabulary(const path& basepath) {
for (directory_iterator iter = directory_iterator(basepath); iter
        != directory_iterator(); iter++) {
    directory_entry entry = *iter;

    if (is_directory(entry.path())) {

        cout << "Processing directory " << entry.path().string() << endl;
        extractTrainingVocabulary(entry.path());

    } else {

        path entryPath = entry.path();
        if (entryPath.extension() == ".jpg") {

            cout << "Processing file " << entryPath.string() << endl;

            Mat img = imread(entryPath.string());
            if (!img.empty())
            {

                cout << "not empty"<< endl;


                try{
                    // ...
                    vector<KeyPoint> keypoints;
                    cout << "not empty"<< keypoints.empty()<<endl;
                    //detector->detect(img, keypoints);
                } catch (const std::exception& ex) {
                }



            }
            else
            {
                cout << " empty"<< endl;

            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

假设该代码中的detector-&gt; detect()函数实际上没有在您的实现中注释掉。我唯一的猜测是img.empty()函数没有返回正确的值。检查图像是否正确加载的OpenCV方法是使用image.data数据成员,如下所示:

if(! image.data )  // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}