Learn Opencv--orb simplified version &location accelerated version

Source: Internet
Author: User
Tags scalar

According to the previous surf simplified version of the structure, re-The Orb detection code to simplify the following, found that although the same speed, it can save a lot of code, the key is to have

Bruteforcematcher

NB Type:class GPU::Bruteforcematcher_gpu

Add Findhomography, then perspectivetransform can location, but this speed is very slow;

So change, ask matches keypoints x and Y coordinates and the average, basically is the object Center!!!

With this point as the center to draw the same size as the original object rectangle, you can locate the approximate position, but certainly not as accurate as the perspective transformation, and does not have scale invariance.

But the robustness should be better, because, as long as the match is successful, the basic can locate the center, but the perspective transformation is sometimes because of the scale transformation too large and other factors, draw a very unreliable rectangular box!

[CPP]View PlainCopy print?
  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//bruteforcematcher support <Hamming> <L1<float>> <L2<float>>
  32. //flannbasedmatcher Matcher; not supported
  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

Learn Opencv--orb simplified version &location accelerated version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.