SIFT記述子とFLANNマッチャーを使用して、2つの画像間の一致の最小距離を取得しています。最初の画像はクエリの画像で、2 番目の画像はデータセットからのものです。ループを使用して 2 番目の画像を 1 つずつロードしたいのですが、最初の反復の直後に、最初の反復の結果を表示した後に実行時にプログラムがクラッシュし、2 番目の反復に入ることができません。私は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;
}
私は多くのことを試しましたが、ここに私の問題と観察があります:
- 反復を使用しているからではありません。のみを実行すると
matcher.match(descriptors1, descriptors2, matches);
、実行後にクラッシュします。 - また、SURF記述子またはBruteForceMatcherでも機能しません。どちらも上記のコードで同じ問題を抱えています。SURF を使用して opencv チュートリアルのさまざまなコードを使用しましたが、結果を表示した後もクラッシュします。opencv チュートリアルのサンプル コードはこちらを参照してください
- いくつかの回答が言ったように私も試しまし
initModule_nonfree();
たが、それでも役に立ちませんでした。 - 「Done!」と表示された後、プログラムがクラッシュします。次の反復に入らない。
- 以下の および関連するコードを削除する
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. lib opencv_legacy2413.lib opencv_ml2413.lib
構成に問題はないと思います...x86でリリースモードで使用しています、ありがとう!