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?
- #include "opencv2/objdetect/objdetect.hpp"
- #include "opencv2/features2d/features2d.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/calib3d/calib3d.hpp"
- #include "opencv2/imgproc/imgproc_c.h"
- #include "opencv2/imgproc/imgproc.hpp"
- #include <string>
- #include <vector>
- #include <iostream>
- Using namespace CV;
- Using namespace std;
- char* image_filename1 = "D:/src.jpg";
- char* image_filename2 = "D:/demo.jpg";
- int main ()
- {
- Mat img1 = Imread (image_filename1, Cv_load_image_grayscale);
- Mat Img2 = Imread (image_filename2, Cv_load_image_grayscale);
- Int64 St,et;
- ORB Orb1 (30,orb::commonparams (1.2,1));
- ORB Orb2 (100,orb::commonparams (1.2,1));
- vector<keypoint>keys1,keys2;
- Mat Descriptor1,descriptor2;
- ORB1 (Img1,mat (), Keys1,descriptor1,false);
- St=gettickcount ();
- ORB2 (Img2,mat (), Keys2,descriptor2,false);
- Et=gettickcount ()-st;
- Et=et*1000/(Double) gettickfrequency ();
- cout<<"Extract time:" <<et<<"MS" <<endl;
- Vector<dmatch> matches;
- //<em>class </em><tt class= "descclassname" >gpu::</tt><tt class= "Descname" >< Span class= "highlighted" >BruteForce</span>Matcher_GPU</tt>
- bruteforcematcher//bruteforcematcher support <Hamming> <L1<float>> <L2<float>>
- //flannbasedmatcher Matcher; not supported
- St=gettickcount ();
- Matcher.match (descriptor1,descriptor2,matches);
- Et=gettickcount ()-st;
- Et=et*1000/gettickfrequency ();
- cout<<"Match time:" <<et<<"MS" <<endl;
- Mat img_matches;
- Drawmatches (IMG1, keys1, Img2, Keys2,
- Matches, Img_matches, Scalar::all ( -1), Scalar::all (-1),
- vector<char> (), drawmatchesflags::not_draw_single_points);
- Imshow ("Match", img_matches);
- cout<<"Match size:" <<matches.size () <<endl;
- /*
- Mat showimg;
- Drawmatches (IMG1,KEYS1,IMG2,KEYS2,MATCHS,SHOWIMG);
- Imshow ("Win", showimg);
- */
- Waitkey (0);
- St=gettickcount ();
- vector<point2f>pt1;
- vector<point2f>pt2;
- float x=0,y=0;
- For (size_t i=0;i<matches.size (); i++)
- {
- Pt1.push_back (keys1[matches[i].queryidx].pt);
- Pt2.push_back (keys2[matches[i].trainidx].pt);
- X+=keys2[matches[i].trainidx].pt.x;
- Y+=keys2[matches[i].trainidx].pt.y;
- }
- X=x/matches.size ();
- Y=y/matches.size ();
- Mat Homo;
- Homo=findhomography (PT1,PT2,CV_RANSAC);
- Vector<point2f>src_cornor (4);
- Vector<point2f>dst_cornor (4);
- Src_cornor[0]=cvpoint (0,0);
- Src_cornor[1]=cvpoint (img1.cols,0);
- Src_cornor[2]=cvpoint (img1.cols,img1.rows);
- Src_cornor[3]=cvpoint (0,img1.rows);
- Perspectivetransform (Src_cornor,dst_cornor,homo);
- Mat Img=imread (image_filename2,1);
- Line (Img,dst_cornor[0],dst_cornor[1],scalar (255,0,0), 2);
- Line (Img,dst_cornor[1],dst_cornor[2],scalar (255,0,0), 2);
- Line (Img,dst_cornor[2],dst_cornor[3],scalar (255,0,0), 2);
- Line (Img,dst_cornor[3],dst_cornor[0],scalar (255,0,0), 2);
- /*
- 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);
- 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);
- 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);
- 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);
- */
- Circle (Img,point (x, y), 10,scalar (0,0,255), 3,cv_filled);
- 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);
- 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);
- 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);
- 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);
- Imshow ("Location", IMG);
- Et=gettickcount ()-st;
- Et=et*1000/gettickfrequency ();
- cout<<"Location time:" <<et<<"MS" <<endl;
- Waitkey (0);
- }
from:http://blog.csdn.net/yangtrees/article/details/7545820
Learn Opencv--orb simplified version &location accelerated version