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

来源:互联网 发布:帝国时代2网络对战 编辑:程序博客网 时间:2024/05/16 10:33

opencv实现surf特征的提取,本来是一个很简单的代码,结果我运行时却出现了各种错误,下面来谈谈我出现的错误及问题的解决过程。

首先,我把提取surf特征的过程整合成了一个函数,我单独建立一个工程读取两张图片,然后调用这个surf提取的函数时时无论是debug还是release模式下都是没有问题的,当我把这个函数添加到我现在已有的工程代码里面的时候出现了各种奇葩错误。下面是我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特征提取的整个代码都在上面了,可当我把这个函数在我已有的工程中调用的时候,出现了一下错误


经百度说出现这种错误是内存问题,要么就是内存木有释放,要么就是两个指针指向同一个内存,一个指针把它释放掉了等等,于是我在代码里面找了很久,改了许多遍 也没有解决,最后我发现我把代码中的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的库文件,奇葩的是问题尽然结局了。对于这我只能表示相当无语啊!

0 0