opencv提取surf特征点出现的错误

opencv实现surf特征的提取。本来是一个非常easy的代码,结果我执行时却出现了各种错误,以下来谈谈我出现的错误及问题的解决过程。

首先,我把提取surf特征的过程整合成了一个函数,我单独建立一个project读取两张图片,然后调用这个surf提取的函数时时不管是debug还是release模式下都是没有问题的。当我把这个函数加入到我如今已有的project代码里面的时候出现了各种奇葩错误。以下是我surf特征提取的函数

void surfdetect(IplImage *image1,IplImage *image2)
{
	using namespace std;
	//IplImage *image1,*image2;
	//image1 = cvLoadImage("lena1.jpg");
	//image2= cvLoadImage("lena2.jpg"); 

	cv::SurfFeatureDetector detector;
	cv::SurfDescriptorExtractor descriptor_extractor;
	cv::BruteForceMatcher<cv::L2<float> > descriptor_matcher;

	//提取特征点
	std::vector<cv::KeyPoint> keypoints1,keypoints2;  
    detector.detect( image1, keypoints1 );//检測img1中的Surf特征点。存储到keypoints1中  
    detector.detect( image2, keypoints2 );  
    cout<<"图像1特征点个数:"<<keypoints1.size()<<endl;  
    cout<<"图像2特征点个数:"<<keypoints2.size()<<endl;  

	//依据特征点计算特征描写叙述子矩阵。即特征向量矩阵  
	cv::Mat descriptors1,descriptors2;  
    descriptor_extractor.compute( image1, keypoints1, descriptors1 );  
    descriptor_extractor.compute( image2, keypoints2, descriptors2 ); 

	cv::Mat img_keypoints1,img_keypoints2;  
    drawKeypoints(image1,keypoints1,img_keypoints1);  
    drawKeypoints(image2,keypoints2,img_keypoints2);  
	//imshow("Src1",img_keypoints1);  
	//cv::waitKey(6000);
    //imshow("Src2",img_keypoints2);  
	//cv::waitKey(6000);

	std::vector<cv::DMatch> matches;//匹配结果  
	descriptor_matcher.match( descriptors1, descriptors2, matches );//匹配两个图像的特征矩阵  
    cout<<"Match个数:"<<matches.size()<<endl;  
	cv::Mat img_matches1,img_matches2;  
	cv::drawMatches(image1,keypoints1,image2,keypoints2,matches,img_matches1); 
	imshow("粗匹配",img_matches1);  
	cv::waitKey(0);

	double max_dist = 0;  
    double min_dist = 100;  
    for(int i=0; i<matches.size(); i++)  
    {  
        double dist = matches[i].distance;  
        if(dist < min_dist) min_dist = dist;  
        if(dist > max_dist) max_dist = dist;  
    }  
    cout<<"最大距离:"<<max_dist<<endl;  
    cout<<"最小距离:"<<min_dist<<endl;  
  
    //筛选出较好的匹配点  
    std::vector<cv::DMatch> goodMatches;  
    for(int i=0; i<matches.size(); i++)  
    {  
        if(matches[i].distance < 0.05 * max_dist)  
        {  
            goodMatches.push_back(matches[i]);  
        }  
    }  
    cout<<"goodMatch个数:"<<goodMatches.size()<<endl; 
	cv::drawMatches(image1,keypoints1,image2,keypoints2,goodMatches,img_matches2);  
	imshow("精匹配",img_matches2);
	cv::waitKey(00);

}


SURF特征提取的整个代码都在上面了。可当我把这个函数在我已有的project中调用的时候,出现了一下错误


经百度说出现这样的错误是内存问题,要么就是内存木有释放,要么就是两个指针指向同一个内存。一个指针把它释放掉了等等,于是我在代码里面找了非常久,改了很多遍 也没有解决,最后我发现我把代码中的vector

  std::vector<cv::KeyPoint> keypoints1,keypoints2;  
  cv::Mat descriptors1,descriptors2;
  std::vector<cv::DMatch> matches;//匹配结果
  std::vector<cv::DMatch> goodMatches;

从函数中拿出来,作为全局变量,问题尽然攻克了,事实上我都没想明确这是什么原因。

vector里面的内存不是不须要手动释放的吗,话说当其在函数里面作为局部变量的时候我也尝试过释放它,先是clear。后来发现clear不能释放其内存。于是我依照网上说的用swap来手动释放,但是终于还是没用。最后误打误撞的,把它作为全局变量,问题才得以解决,只是攻克了就好。

在debug底下程序已经能够执行了,全都通过了。可接下来,我在release底下又遇到错误了。错误类型例如以下:



网上说这样的我是加入opencv库的时候加入错了,我细致看了我的additional dependencies的加入。在release底下我确实没带d啊。可怎么就是出现了这样的错误呢,要是是程序有问题,那怎么debug底下能够执行呢。我非常纳闷,尝试了非常多方法。最后抱着试试的态度,干脆把release和debug底下都加入了带d的库文件,奇葩的是问题尽然结局了。对于这我仅仅能表示相当无语啊。

原文地址:https://www.cnblogs.com/brucemengbm/p/6749004.html