Not much to say, directly on the code bar:
#include <iostream> #include <stdio.h> #include "opencv2/core.hpp" #include "opencv2/core/utility.hpp" # Include "Opencv2/core/ocl.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/ Features2d.hpp "#include" opencv2/calib3d.hpp "#include" opencv2/imgproc.hpp "#include" opencv2/flann.hpp "#include"
Opencv2/xfeatures2d.hpp "#include" opencv2/ml.hpp "using namespace CV;
using namespace Std;
using namespace cv::xfeatures2d;
using namespace cv::ml; int main () {Mat a = Imread ("Box.png", Imread_grayscale);
Read grayscale image Mat B = imread ("Box_in_scene.png", Imread_grayscale); Ptr<surf> SURF;
The creation method is not the same as in 2 surf = surf::create (800);
Bfmatcher Matcher;
Mat C, D;
Vector<keypoint>key1, Key2;
Vector<dmatch> matches;
Surf->detectandcompute (A, Mat (), Key1, C);
Surf->detectandcompute (b, Mat (), Key2, D); Matcher.match (c, D, matches); Match sort (matches.begin (), Matches.end ());
Filter matching points vector< dmatch > good_matches;
int ptspairs = std::min ((int) (Matches.size () * 0.15));
cout << ptspairs << Endl;
for (int i = 0; i < ptspairs; i++) {good_matches.push_back (matches[i]);
} Mat outimg; Drawmatches (A, key1, B, Key2, Good_matches, Outimg, Scalar::all ( -1), Scalar::all ( -1),vector<char> (), drawmatchesflags::not_draw_single_points);
Draw the matching point std::vector<point2f> obj;
Std::vector<point2f> scene;
for (size_t i = 0; i < good_matches.size (); i++) {obj.push_back (key1[good_matches[i].queryidx].pt);
Scene.push_back (key2[good_matches[i].trainidx].pt);
} std::vector<point2f> obj_corners (4);
Obj_corners[0] = point (0, 0);
OBJ_CORNERS[1] = point (a.cols, 0);
OBJ_CORNERS[2] = Point (A.cols, a.rows);
OBJ_CORNERS[3] = Point (0, a.rows);
Std::vector<point2f> scene_corners (4); Mat H = findhomography (obj, ScenE, RANSAC);
Look for matching images perspectivetransform (obj_corners, Scene_corners, H); Line (Outimg,scene_corners[0] + point2f (float) a.cols, 0), scene_corners[1] + point2f ((float) a.cols, 0), Scalar (0, 255, 0 ), 2, LINE_AA); Draw Line (Outimg,scene_corners[1] + point2f (float) a.cols, 0), scene_corners[2] + point2f ((float) a.cols, 0), Scalar (0,
255, 0), 2, LINE_AA); Line (Outimg,scene_corners[2] + point2f (float) a.cols, 0), scene_corners[3] + point2f ((float) a.cols, 0), Scalar (0, 255, 0
), 2, LINE_AA); Line (Outimg,scene_corners[3] + point2f (float) a.cols, 0), Scene_corners[0] + point2f ((float) a.cols, 0), Scalar (0, 255, 0
), 2, LINE_AA);
Imshow ("AAAA", outimg);
Cvwaitkey (0); }
Run diagram: