OpenCV SITF 特征提取 FeatureDetector对象函数detect运行报错解决方案

本人在使用OpenCV SIFT特征提取算法时,遇到了问题,具体表现为 .exe触发了一个断点错误,经网上查找,发现是 vector 在析构时,造成了内存错误,解决方案由大神在csdn博客中给出,链接地址:

点击打开链接

按照大神所提示的 ,在使用opencv 函数之前,先对 vector 进行手动分配内存,分配内存后问题解决;

个人声明:本人在VS2013下调用sift算法,已经将问题解决,当然,具体问题,具体分析,本人适用,但不一定适用所有人,也希望大家对该问题的解决方案进行及时补充;

 

代码:

 
  1. initModule_nonfree();//初始化模块,使用SIFT或SURF时用到

  2.  
  3. //FeatureDetector::create()函数的参数可选

  4. // • "FAST" – FastFeatureDetector

  5. // • "STAR" – StarFeatureDetector

  6. // • "SIFT" – SIFT(nonfree module)

  7. // • "SURF" – SURF(nonfree module)

  8. // • "ORB" – ORB

  9. // • "BRISK" – BRISK

  10. // • "MSER" – MSER

  11. // • "GFTT" – GoodFeaturesToTrackDetector

  12. // • "HARRIS" – GoodFeaturesToTrackDetector with Harris detector enabled

  13. // • "Dense" – DenseFeatureDetector

  14. // • "SimpleBlob" – SimpleBlobDetector

  15. Ptr<FeatureDetector> detector = FeatureDetector::create("SIFT");//创建SIFT特征检测器

  16.  
  17. //SiftFeatureDetector detector;

  18. //Ptr<FeatureDetector> detector = FeatureDetector::create("SURF");

  19. // DescriptorExtractor::create()函数的参数如下:

  20. //

  21. // • "SIFT" – SIFT

  22. // • "SURF" – SURF

  23. // • "BRIEF" – BriefDescriptorExtractor

  24. // • "BRISK" – BRISK

  25. // • "ORB" – ORB

  26. // • "FREAK" – FREAK

  27. //SiftDescriptorExtractor descriptor_extractor;

  28. Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create("SIFT");//创建特征向量生成器

  29. //Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create("SURF");

  30. // // DescriptorMatcher::create()函数的参数如下,对应不同的匹配算法:

  31. // // – BruteForce(it uses L2)

  32. // // – BruteForce - L1

  33. // // – BruteForce - Hamming

  34. // // – BruteForce - Hamming(2)

  35. // // – FlannBased

  36. Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create("BruteForce");//创建特征匹配器,暴力匹配(简单匹配)

  37. // if (detector.empty() || descriptor_extractor.empty())

  38. // cout << "fail to create detector!";

  39.  
  40. //读入图像

  41. Mat img1 = imread("001.jpg", 0);

  42. Mat img2 = imread("004.bmp", 0);

  43.  
  44. if (img1.empty() || img1.channels() != 1)

  45. MessageBox(L"图像类型错误");

  46. //CV_Error(Error::StsBadArg, "image is empty or has incorrect depth (!=CV_8U)");

  47.  
  48. //if (!mask.empty() && mask.type() != CV_8UC1)

  49. //CV_Error(Error::StsBadArg, "mask has incorrect type (!=CV_8UC1)");

  50.  
  51. //特征点检测

  52. double t = getTickCount();//当前滴答数

  53. vector<KeyPoint> keypoints1, keypoints2;

  54. keypoints1.resize(100);

  55. keypoints2.resize(100);

  56. detector->detect(img1, keypoints1);//检测img1中的SIFT特征点,存储到keypoints1中

  57. detector->detect(img2, keypoints2);

  58. //detector.detect(img1, keypoints1);

  59. //detector.detect(img2, keypoints2);

  60. // cout << "图像1特征点个数:" << keypoints1.size() << endl;

  61. // cout << "图像2特征点个数:" << keypoints2.size() << endl;

  62. //

  63. // //根据特征点计算特征描述子矩阵,即特征向量矩阵

  64. Mat descriptors1, descriptors2;

  65. descriptor_extractor->compute(img1, keypoints1, descriptors1);

  66. descriptor_extractor->compute(img2, keypoints2, descriptors2);

  67. // t = ((double)getTickCount() - t) / getTickFrequency();

  68. // cout << "SIFT算法用时:" << t << "秒" << endl;

  69. //

  70. //

  71. // cout << "图像1特征描述矩阵大小:" << descriptors1.size()

  72. // << ",特征向量个数:" << descriptors1.rows << ",维数:" << descriptors1.cols << endl;

  73. // cout << "图像2特征描述矩阵大小:" << descriptors2.size()

  74. // << ",特征向量个数:" << descriptors2.rows << ",维数:" << descriptors2.cols << endl;

  75. //

  76. // //画出特征点

  77. Mat img_keypoints1, img_keypoints2;

  78. drawKeypoints(img1, keypoints1, img_keypoints1, Scalar::all(-1), 0);

  79. drawKeypoints(img2, keypoints2, img_keypoints2, Scalar::all(-1), 0);

  80. imshow("Src1",img_keypoints1);

  81. imshow("Src2",img_keypoints2);

  82. waitKey(0);

  83.  
  84. //特征匹配

  85. vector<DMatch> matches;//匹配结果

  86. matches.resize(100);

  87. descriptor_matcher->match(descriptors1, descriptors2, matches);//匹配两个图像的特征矩阵

  88. cout << "Match个数:" << matches.size() << endl;

  89.  
  90. //计算匹配结果中距离的最大和最小值

  91. //距离是指两个特征向量间的欧式距离,表明两个特征的差异,值越小表明两个特征点越接近

  92. double max_dist = 0;

  93. double min_dist = 100;

  94. for (int i = 0; i < matches.size(); i++)

  95. {

  96. double dist = matches[i].distance;

  97. if (dist < min_dist) min_dist = dist;

  98. if (dist > max_dist) max_dist = dist;

  99. }

  100. cout << "最大距离:" << max_dist << endl;

  101. cout << "最小距离:" << min_dist << endl;

  102.  
  103. //筛选出较好的匹配点

  104. vector<DMatch> goodMatches;

  105. for (int i = 0; i < matches.size(); i++)

  106. {

  107. if (matches[i].distance < 0.5 * max_dist)

  108. {

  109. goodMatches.push_back(matches[i]);

  110. }

  111. }

  112. cout << "goodMatch个数:" << goodMatches.size() << endl;

  113.  
  114. //画出匹配结果

  115. Mat img_matches;

  116. //红色连接的是匹配的特征点对,绿色是未匹配的特征点

  117. drawMatches(img1, keypoints1, img2, keypoints2, goodMatches, img_matches,

  118. /*Scalar::all(-1)/ **/CV_RGB(255,0,0), CV_RGB(0, 255, 0), Mat(), 2);

  119.  
  120. namedWindow("MatchSIFT", 0);

  121. imshow("MatchSIFT", img_matches);

  122. waitKey(0);

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值