匹配描述符后 opencv 程序崩溃

opencv program crash after matching descriptors

我正在使用 SIFT 描述符和 FLANN 匹配器来获取两张图片之间匹配的最小距离。第一张图片是查询图片,第二张图片来自数据集。我想循环加载一张一张的第二张图片,但是第一次迭代后,程序在显示第一次迭代结果后运行时间崩溃,无法进入第二次迭代。我正在使用 opencv 2.4.13 和 vs2017。下面是我的代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <opencv2/legacy/legacy.hpp>

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;
using namespace cv;

#define IMAGE_folder "D:\Project\dataset1"  // change to your folder location

int main()
{
    initModule_nonfree();
    const int filename_len = 900;
    char tempname1[filename_len];
    sprintf_s(tempname1, filename_len, "%s\%s", IMAGE_folder, "995.jpg");
    Mat i1 = imread(tempname1);
    Mat img1, img2;
    cvtColor(i1, img1, COLOR_BGR2GRAY);

    if (!i1.data)
    {
        printf("Cannot find the input image!\n");
        system("pause");
        return -1;
    }

    std::vector<KeyPoint> key_points1, key_points2;
    Mat descriptors1, descriptors2;

    SIFT sift;
    sift(img1, Mat(), key_points1, descriptors1);

    for (int i = 990; i < 1000; i++)
    {
        initModule_nonfree();
        cout << i << endl;
        char tempname2[filename_len];
        sprintf_s(tempname2, filename_len, "%s\%d.jpg", IMAGE_folder, i);
        Mat i2 = imread(tempname2);
        if (!i2.data)
        {
            printf("Cannot find the input image!\n");
            system("pause");
            return -1;
        }
        cvtColor(i2, img2, COLOR_BGR2GRAY);
        sift(img2, Mat(), key_points2, descriptors2);

        FlannBasedMatcher matcher;
        vector<DMatch> matches;
        matches.clear();
        matcher.match(descriptors1, descriptors2, matches);  //if I comment this line and the code below to show the distance, it can work

        double max_dist = 0;
        double min_dist = 100;

        for (int j = 0; j < descriptors1.rows; j++)
        {
            double dist = matches[j].distance;
            if (dist < min_dist)
                min_dist = dist;
            if (dist > max_dist)
                max_dist = dist;
        }

        //matcher.clear();  //I've tried these four but still cannot help
        //matches.clear();
        //key_points1.clear();
        //key_points2.clear();

        printf("-- Max dist : %f \n", max_dist);
        printf("-- Min dist : %f \n", min_dist);
        cout << "Done!" << endl;
    }

    //waitKey(0);
    return 0;
}

我已经尝试了很多,这里是我的问题和观察:

  1. 不是因为我用了迭代。如果我只运行 matcher.match(descriptors1, descriptors2, matches); ,执行完也会崩溃。
  2. 它也不适用于 SURF 描述符或 BruteForceMatcher,两者都与上面的代码存在相同的问题。我使用 SURF 使用了 opencv 教程中的不同代码片段,但在显示结果后它仍然崩溃。 Example code from opencv tutorial see here
  3. 我也试过 initModule_nonfree(); 正如一些答案所说,但仍然没有帮助。
  4. 显示"Done!"后程序崩溃,没有进入下一次迭代。
  5. 如果我删除matcher.match(descriptors1, descriptors2, matches);和下面的相关代码,它可以正常工作。所以问题一定出在"match"函数上。

非常感谢!

----------------------------已更新---------------- --------------

我的包含库和链接库如下所示:

C:\Program 文件 (x86)\opencv\build\include C:\Program Files (x86)\opencv\build\include\opencv C:\Program Files (x86)\opencv\build\include\opencv2

C:\Program 文件 (x86)\opencv\build\x64\vc12\staticlib C:\Program 文件 (x86)\opencv\build\x64\vc12\lib

opencv_objdetect2413.lib opencv_ts2413.lib opencv_video2413.lib opencv_nonfree2413.lib opencv_ocl2413.lib opencv_photo2413.lib opencv_stitching2413.lib opencv_superres2413.lib opencv_videostab2413.lib opencv_calib3d2413.lib opencv_contrib2413.lib opencv_core2413.lib opencv_features2d2413.lib opencv_flann2413.lib opencv_gpu2413.lib opencv_highgui2413.lib opencv_imgproc2413.lib opencv_legacy2413.lib opencv_ml2413.lib

我认为配置可能没有问题...我在x86下以发布模式使用它,谢谢!

我发现了问题。该代码适用于 opencv2.4.13 和 vs2012 而不是 vs2017。可能只是因为opencv2.4.13不兼容vs2017.