根据前面surf简化版的结构,重新把ORB检测的代码给简化以下,发现虽然速度一样,确实能省好多行代码,关键是有

BruteForceMatcher<HammingLUT>matcher的帮忙,直接省的写了一个函数;

NB类型:class gpu::BruteForceMatcher_GPU

再加上findHomography,之后perspectiveTransform就可以location,但是这样速度很慢;

于是改动一下,求matches的keypoints的x与y坐标和的平均值,基本上就是对象中心!!!

以这个点为中心画与原对象大小相同的矩形框,就可以定位出大概位置,但是肯定不如透视变换准确,而且不具有尺度不变性。

但是鲁棒性应该更好,因为,只要能match成功,基本都能定位中心,但是透视变换有时却因为尺度变换过大等因素,画出很不靠谱的矩形框!

  1. #include "opencv2/objdetect/objdetect.hpp"
  2. #include "opencv2/features2d/features2d.hpp"
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include "opencv2/calib3d/calib3d.hpp"
  5. #include "opencv2/imgproc/imgproc_c.h"
  6. #include "opencv2/imgproc/imgproc.hpp"
  7. #include <string>
  8. #include <vector>
  9. #include <iostream>
  10. using namespace cv;
  11. using namespace std;
  12. char* image_filename1 = "D:/src.jpg";
  13. char* image_filename2 = "D:/Demo.jpg";
  14. int main()
  15. {
  16. Mat img1 = imread( image_filename1, CV_LOAD_IMAGE_GRAYSCALE );
  17. Mat img2 = imread( image_filename2, CV_LOAD_IMAGE_GRAYSCALE );
  18. int64 st,et;
  19. ORB orb1(30,ORB::CommonParams(1.2,1));
  20. ORB orb2(100,ORB::CommonParams(1.2,1));
  21. vector<KeyPoint>keys1,keys2;
  22. Mat descriptor1,descriptor2;
  23. orb1(img1,Mat(),keys1,descriptor1,false);
  24. st=getTickCount();
  25. orb2(img2,Mat(),keys2,descriptor2,false);
  26. et=getTickCount()-st;
  27. et=et*1000/(double)getTickFrequency();
  28. cout<<"extract time:"<<et<<"ms"<<endl;
  29. vector<DMatch> matches;
  30. //<em>class </em><tt class="descclassname">gpu::</tt><tt class="descname"><span class="highlighted">BruteForce</span>Matcher_GPU</tt>
  31. BruteForceMatcher<HammingLUT>matcher;//BruteForceMatcher支持<Hamming> <L1<float>> <L2<float>>
  32. //FlannBasedMatcher matcher;不支持
  33. st=getTickCount();
  34. matcher.match(descriptor1,descriptor2,matches);
  35. et=getTickCount()-st;
  36. et=et*1000/getTickFrequency();
  37. cout<<"match time:"<<et<<"ms"<<endl;
  38. Mat img_matches;
  39. drawMatches( img1, keys1, img2, keys2,
  40. matches, img_matches, Scalar::all(-1), Scalar::all(-1),
  41. vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
  42. imshow("match",img_matches);
  43. cout<<"match size:"<<matches.size()<<endl;
  44. /*
  45. Mat showImg;
  46. drawMatches(img1,keys1,img2,keys2,matchs,showImg);
  47. imshow( "win", showImg );
  48. */
  49. waitKey(0);
  50. st=getTickCount();
  51. vector<Point2f>pt1;
  52. vector<Point2f>pt2;
  53. float x=0,y=0;
  54. for(size_t i=0;i<matches.size();i++)
  55. {
  56. pt1.push_back(keys1[matches[i].queryIdx].pt);
  57. pt2.push_back(keys2[matches[i].trainIdx].pt);
  58. x+=keys2[matches[i].trainIdx].pt.x;
  59. y+=keys2[matches[i].trainIdx].pt.y;
  60. }
  61. x=x/matches.size();
  62. y=y/matches.size();
  63. Mat homo;
  64. homo=findHomography(pt1,pt2,CV_RANSAC);
  65. vector<Point2f>src_cornor(4);
  66. vector<Point2f>dst_cornor(4);
  67. src_cornor[0]=cvPoint(0,0);
  68. src_cornor[1]=cvPoint(img1.cols,0);
  69. src_cornor[2]=cvPoint(img1.cols,img1.rows);
  70. src_cornor[3]=cvPoint(0,img1.rows);
  71. perspectiveTransform(src_cornor,dst_cornor,homo);
  72. Mat img=imread(image_filename2,1);
  73. line(img,dst_cornor[0],dst_cornor[1],Scalar(255,0,0),2);
  74. line(img,dst_cornor[1],dst_cornor[2],Scalar(255,0,0),2);
  75. line(img,dst_cornor[2],dst_cornor[3],Scalar(255,0,0),2);
  76. line(img,dst_cornor[3],dst_cornor[0],Scalar(255,0,0),2);
  77. /*
  78. line(img,cvPoint((int)dst_cornor[0].x,(int)dst_cornor[0].y),cvPoint((int)dst_cornor[1].x,(int)dst_cornor[1].y),Scalar(255,0,0),2);
  79. line(img,cvPoint((int)dst_cornor[1].x,(int)dst_cornor[1].y),cvPoint((int)dst_cornor[2].x,(int)dst_cornor[2].y),Scalar(255,0,0),2);
  80. line(img,cvPoint((int)dst_cornor[2].x,(int)dst_cornor[2].y),cvPoint((int)dst_cornor[3].x,(int)dst_cornor[3].y),Scalar(255,0,0),2);
  81. line(img,cvPoint((int)dst_cornor[3].x,(int)dst_cornor[3].y),cvPoint((int)dst_cornor[0].x,(int)dst_cornor[0].y),Scalar(255,0,0),2);
  82. */
  83. circle(img,Point(x,y),10,Scalar(0,0,255),3,CV_FILLED);
  84. line(img,Point(x-img1.cols/2,y-img1.rows/2),Point(x+img1.cols/2,y-img1.rows/2),Scalar(0,0,255),2);
  85. line(img,Point(x+img1.cols/2,y-img1.rows/2),Point(x+img1.cols/2,y+img1.rows/2),Scalar(0,0,255),2);
  86. line(img,Point(x+img1.cols/2,y+img1.rows/2),Point(x-img1.cols/2,y+img1.rows/2),Scalar(0,0,255),2);
  87. line(img,Point(x-img1.cols/2,y+img1.rows/2),Point(x-img1.cols/2,y-img1.rows/2),Scalar(0,0,255),2);
  88. imshow("location",img);
  89. et=getTickCount()-st;
  90. et=et*1000/getTickFrequency();
  91. cout<<"location time:"<<et<<"ms"<<endl;
  92. waitKey(0);
  93. }

from: http://blog.csdn.net/yangtrees/article/details/7545820

学习OpenCV——ORB简化版&Location加速版的更多相关文章

  1. 学习OpenCV——Surf简化版

    之前写过一遍关于学习surf算法的blog:http://blog.csdn.net/sangni007/article/details/7482960 但是代码比较麻烦,而且其中还涉及到flann算 ...

  2. Keras学习环境配置-GPU加速版(Ubuntu 16.04 + CUDA8.0 + cuDNN6.0 + Tensorflow)

    本文是个人对Keras深度学习框架配置的总结,不周之处请指出,谢谢! 1. 首先,我们需要安装Ubuntu操作系统(Windows下也行),这里使用Ubuntu16.04版本: 2. 安装好Ubunt ...

  3. 转:基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)【模式识别中的翘楚】

    文章来自于:http://blog.renren.com/share/246648717/8171467499 基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴 ...

  4. 《学习OpenCV(中文版)》

    <模式识别中文版(希)西奥多里蒂斯> <学习OpenCV(中文版)> 矩阵计算 英文版 第四版 Matrix Computations OpenCV 3.x with Pyth ...

  5. 前端学习 node 快速入门 系列 —— 简易版 Apache

    其他章节请看: 前端学习 node 快速入门 系列 简易版 Apache 我们用 node 来实现一个简易版的 Apache:提供静态资源访问的能力. 实现 直接上代码. - demo - stati ...

  6. 学习opencv之路(一)

    先看一下<学习opencv> 找几个demo 学会相机标定 我做的是单目相机的标定.

  7. 实验楼课程管理程序-深入学习《C++ Primer第五版》实验报告&学习笔记1

    本片博客为实验楼的训练营课程深入学习<C++ Primer第五版>的实验报告和学习笔记. 原课程地址为:https://www.shiyanlou.com/courses/405# 原文出 ...

  8. [纯小白学习OpenCV系列]官方例程00:世界观与方法论

    2015-11-11 ----------------------------------------------------------------------------------- 其实,写博 ...

  9. 《学习OpenCV》中求给定点位置公式

    假设有10个三维的点,使用数组存放它们有四种常见的形式: ①一个二维数组,数组的类型是CV32FC1,有n行,3列(n×3) ②类似①,也可以用一个3行n列(3×n)的二维数组 ③④用一个n行1列(n ...

随机推荐

  1. Linux下监控磁盘使用量并在超过阀值后自动发送报警邮件

    最近Linux服务器磁盘使用量经常到100%,直到影响到正常服务出现故障才会去注意,做不到防患于未然,今天在网上搜集了资料,加上自己修改,写了一个shell脚本用于实时监控磁盘使用量并在超过阀值后自动 ...

  2. IO流学习小结

    今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬板.内存.键盘等处理 ...

  3. php获得文件夹下所有文件的递归算法

    function my_scandir($dir){ $files=array(); if(is_dir($dir)) { if($handle=opendir($dir)) { while(($fi ...

  4. 规则html表单对象赋值

    function grid_load_callback(data, status) {            if (data.rows.length > 0)            {     ...

  5. 【SQL Server】左联接,右联接,内联接的比较

    首先需要解释一下这几个联接的意思: left join(左联接): 返回包括左表中的所有记录和右表中联结字段相等的记录. right join(右联接): 返回包括右表中的所有记录和左表中联结字段相等 ...

  6. css3 transition属性变化与animation动画的相似性以及不同点

    下面列子中的2个图片的效果. http://zqtest.e-horse.cn/DongXueImportedCar/assets/mouseOverAnimate.html 第一个为transiti ...

  7. php创建网站问题

    网站在本地浏览的时候链接点击都提示The requested URL was not found on this server. 本地装的wamp,apache和php.ini都是好的 最后更改: 在 ...

  8. The Four Stages of Recovering a Project

    If a project is in trouble, the project manager needs to work to recover it and get the schedule bac ...

  9. Base64解码中文部分中文乱码的原因

    参考这篇博客 http://blog.sina.com.cn/s/blog_4eb5ae750101cq16.html 需要做的就是 filename=filename.replace(" ...

  10. (转)linux运行tomcat时JRE_HOME显示不对怎么办?

    PS:以前也没出现这些问题,重装了一下系统,感觉什么都让我撞上了.Using CATALINA_BASE:   /usr/share/tomcat7Using CATALINA_HOME:   /us ...