0

我正在使用 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 教程中的不同代码段,但在显示结果后它仍然崩溃。opencv 教程中的示例代码请参见此处
  3. 我也尝试initModule_nonfree();过一些答案,但仍然没有帮助。
  4. 显示“完成!”后程序崩溃 并且不进入下一次迭代。
  5. 如果我删除matcher.match(descriptors1, descriptors2, matches);下面的和相关代码,它可以正常工作。所以问题一定出在“匹配”功能上。

提前非常感谢!

- - - - - - - - - - - - - - -更新 - - - - - - - - - - ---------

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

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

C:\Program Files (x86)\opencv\build\x64\vc12\staticlib C:\Program Files (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.库 opencv_legacy2413.lib opencv_ml2413.lib

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

4

1 回答 1

0

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

于 2017-11-19T08:48:58.700 回答