匹配描述符

时间:2017-11-08 14:48:30

标签: c++ opencv cbir flannbasedmatcher

我使用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. 显示&#34;完成后程序崩溃!&#34;而不是进入下一次迭代。
  5. 如果我删除matcher.match(descriptors1, descriptors2, matches);及以下相关代码,则可以正常使用。所以问题必须是&#34;匹配&#34;功能
  6. 提前多多感谢!

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

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

    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.lib opencv_legacy2413.lib opencv_ml2413.lib

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

1 个答案:

答案 0 :(得分:0)

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