VS2017下opencv_4.0.1使用SIFT算法时为何总是在detect函数(detectAndCompute也是)处报内存错误?

环境:

  • windows
  • vistual studio2017
  • opencv_4.0.1

前提:

  • 已配置过opencv_contrib-4.0.1
  • 使用 debug ✖64

问题描述:

总是在detect(detectAndCompute也是)这个函数位置处出现错误:

0x00007FFB87B3A388 处(位于 image.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x0000001D36F1E9F0 处。

代码如下:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <vector>

using namespace std;
using namespace cv;
using namespace xfeatures2d;
using namespace cv::xfeatures2d;

int main()
{
    //Feature2D siftfd;
    SIFT siftfd;
    //Ptr<SIFT> siftfd = SIFT::create();
    //SiftFeatureDetector  siftfd;
    //sift s;
    //s.create();
    Mat image1 = imread("car1.jpg");
    Mat image2 = imread("car2.jpg");
    namedWindow("car1_before", WINDOW_FREERATIO);
    namedWindow("car2_before", WINDOW_FREERATIO);
    imshow("car1_before", image1);
    imshow("car2_before", image2);

    //ptr<siftfeaturedetector> siftfd = siftfeaturedetector::create();
    //ptr<sift> siftfd = sift::create();
    //feature2d siftfd;
    //vector<KeyPoint>kp1, kp2; //keypoint类型的容器
    vector<KeyPoint>kp1;
    Mat draw;
    //kp1.resize(100);
    //kp2.resize(100);

    //siftfd.detectAndCompute(image1, Mat(), kp1, draw);
    siftfd.detect(image1, kp1);
    Mat outimage1;
    drawKeypoints(image1, kp1, outimage1);
    namedWindow("car1_after", WINDOW_FREERATIO);
    imshow("car1_after", outimage1);

    waitKey();
    return 0;

报错情况:
图片说明

因为使用了智能指针....Ptr,而且为他进行了初始化。。。
而且在新的版本中用SIFT和SURF都需要用到nofree模块,在用Cmake重新编译opencv库后,使用SURF,SIFT定义的时候直接写类名会报错,使用智能指针Ptr就可以解决。。。。这里我也是有疑惑的.....具体不是很理解,望各位大佬给个解释.

忘记回来看这个问题了,这个我解决了,具体做什么解决的忘记了,现在能运行的代码如下:

#include <iostream>
#include <vector>
#include <opencv.hpp>
#include <core/core.hpp>
#include <highgui/highgui.hpp>
#include <imgproc/imgproc.hpp>
#include <xfeatures2d.hpp>
#include <xfeatures2d/nonfree.hpp>
#include <features2d/features2d.hpp>
#include <calib3d/calib3d.hpp>
#include <stitching/detail/seam_finders.hpp>

using namespace std;
using namespace cv;
using namespace xfeatures2d;
using namespace cv::xfeatures2d;
using namespace cv::detail;

int main()
{
    //Feature2D detector;
    //SIFT detector;
    //SiftFeatureDetector  detector;
    Ptr <SIFT> detector = SIFT::create();
    //Ptr <Feature2D> detector = Feature2D::create();
    //Ptr<SiftFeatureDetector> detector = SiftFeatureDetector::create();

    const char* imagename1 = "E1.jpg";
    const char* imagename2 = "E2.jpg";
    Mat image1 = imread(imagename1);
    Mat image2 = imread(imagename2);

    if (image1.empty())
    {
        cout << "Can not load image: ";
        cout << imagename1 << endl;
        return -1;
    }
    if (image2.empty())
    {
        cout << "Can not load image: ";
        cout << imagename2 << endl;
        return -1;
    }

    namedWindow("image1_before", WINDOW_FREERATIO);
    namedWindow("image2_before", WINDOW_FREERATIO);
    imshow("image1_before", image1);
    imshow("image2_before", image2);

    vector <KeyPoint> kp1, kp2; 
    Mat descriptor1, descriptor2;

    detector->detectAndCompute(image1, noArray(), kp1, descriptor1);
    detector->detectAndCompute(image2, noArray(), kp2, descriptor2);

    Mat outimage1;
    Mat outimage2;
    drawKeypoints(image1, kp1, outimage1);
    drawKeypoints(image2, kp2, outimage2);
    namedWindow("image1_after", WINDOW_FREERATIO);
    namedWindow("image2_after", WINDOW_FREERATIO);
    imshow("image1_after", outimage1);
    imshow("image2_after", outimage2);

    waitKey();
    return 0;
}

评论中提出的可能是图像的原因我觉得也有可能,太大的图片不行,这里我用的两张图片都是433×577大小的

https://blog.csdn.net/Winder_Sky/article/details/79380836