c++opencvcbirflannbasedmatcher

opencv program crash after matching descriptors


I'm using SIFT descriptor and FLANN matcher to get the minimum distance of the matches between two pictures. The first picture is a query picture and the second picture is from the dataset. I want to load the second picture one by one using a loop, but right after the first iteration, the program crashes at runtime after displaying the result of the first iteration and fails to enter the second iteration. I'm using opencv 2.4.13 and vs2017. Below is my code:

#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;
}

I've tried a lot and here are my problems and observation:

  1. It is not because I use iterations. If I only run the matcher.match(descriptors1, descriptors2, matches); , it will also crash after execution.
  2. It is also not working for SURF descriptor or BruteForceMatcher, both have the same problem with the code above. I've used different pieces of code from opencv tutorial using SURF, and it still crashes after displaying the result. Example code from opencv tutorial see here
  3. I've also tried initModule_nonfree(); as some answers said, but still didn't help.
  4. The program crashes after displaying "Done!" and not entering the next iteration.
  5. If I delete the matcher.match(descriptors1, descriptors2, matches); and the relevant code below, it can work properly. So the problem must be with the "match" function.

Thanks a lot in advance!

-----------------------------updated-----------------------------

My includes and linked libraries are shown below:

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

I think the configuration may not have problems... I use it in release mode under x86, thanks!


Solution

  • I've found out the problem. The code works for opencv2.4.13 and vs2012 instead of vs2017. Maybe it's just because opencv2.4.13 is not compatible with vs2017.