OPENCV Screen Tracking

Source: Internet
Author: User

#include <stdio.h>#include<iostream>#include"opencv2/imgproc/imgproc.hpp"#include"opencv2/core/core.hpp"#include"opencv2/features2d/features2d.hpp"#include"opencv2/highgui/highgui.hpp"#include"opencv2/calib3d/calib3d.hpp"#include"opencv2/nonfree/nonfree.hpp"using namespaceCV;intMainintargcChar**argv) {Cvcapture* Capture = Cvcreatefilecapture ("Sign3.mp4" ); Mat Img_object= Imread ("pic3.jpg", Cv_load_image_grayscale); Mat Frame=Cvqueryframe (Capture);    Mat Img_scene;    Cvtcolor (frame, img_scene, Cv_bgr2gray); intMinhessian = -;    Surffeaturedetector detector (Minhessian); Std::vector<KeyPoint>Keypoints_object, Keypoints_scene;    Detector.detect (Img_object, keypoints_object);    Surfdescriptorextractor extractor;    Mat Descriptors_object, Descriptors_scene;    Extractor.compute (Img_object, Keypoints_object, Descriptors_object);    Flannbasedmatcher Matcher; Std::vector< Dmatch >matches; Std::vector<Point2f>obj; Std::vector<Point2f>scene;  while(1) {Frame=Cvqueryframe (Capture);        Cvtcolor (frame, img_scene, Cv_bgr2gray); if(!img_object.data | |!img_scene.data) {Std::cout<<" --(!) Error reading Images"<< Std::endl;return-1; } //--Step 1:detect the keypoints using SURF DetectorDetector.detect (Img_scene, Keypoints_scene); //--Step 2:calculate descriptors (feature vectors)Extractor.compute (Img_scene, Keypoints_scene, Descriptors_scene); //--Step 3:matching descriptor vectors using FLANN matcherMatcher.match (Descriptors_object, descriptors_scene, matches); DoubleMax_dist =0;DoubleMin_dist = -; //--Quick calculation of Max and min distances between keypoints         for(inti =0; i < descriptors_object.rows; i++ )        {             DoubleDist =matches[i].distance; if(Dist <min_dist) Min_dist=Dist; if(Dist >max_dist) Max_dist=Dist; }//printf ("--Max dist:%f \ n", max_dist);//printf ("-Min dist:%f \ n", min_dist); //--Draw only "good" matches (i.e whose distance are less than 3*min_dist)std::vector< Dmatch >good_matches;  for(inti =0; i < descriptors_object.rows; i++ )        {             if(Matches[i].distance <2*min_dist)                 {Good_matches.push_back (matches[i]);        }} Mat img_matches; Drawmatches (Img_object, Keypoints_object, Img_scene, Keypoints_scene, Good_matches, Img_matches, Scalar::all ( /c3>-1), Scalar::all (-1), Vector<Char>(), drawmatchesflags::not_draw_single_points); //--Localize the object                 for(inti =0; I < good_matches.size (); i++ )        {            //--Get The keypoints from the good matchesobj.push_back (keypoints_object[good_matches[i].queryidx].pt);        Scene.push_back (keypoints_scene[good_matches[i].trainidx].pt); } Mat H= Findhomography (obj, Scene, Cv_ransac,5.0 ); //--Get the corners from the Image_1 (the object is "detected")Std::vector<point2f> Obj_corners (4); obj_corners[0] = Cvpoint (0,0); obj_corners[1] = Cvpoint (Img_object.cols,0 ); obj_corners[2] = Cvpoint (Img_object.cols, img_object.rows); obj_corners[3] = Cvpoint (0, img_object.rows); Std::vector<Point2f> Scene_corners (4);        Perspectivetransform (Obj_corners, Scene_corners, H); //--Draw lines between the corners (the mapped object in the scene-image_2)Line (Img_matches, scene_corners[0] + point2f (img_object.cols,0), scene_corners[1] + point2f (img_object.cols,0), Scalar (0,255,0),4 ); Line (img_matches, scene_corners[1] + point2f (img_object.cols,0), scene_corners[2] + point2f (img_object.cols,0), Scalar (0,255,0),4 ); Line (img_matches, scene_corners[2] + point2f (img_object.cols,0), scene_corners[3] + point2f (img_object.cols,0), Scalar (0,255,0),4 ); Line (img_matches, scene_corners[3] + point2f (img_object.cols,0), scene_corners[0] + point2f (img_object.cols,0), Scalar (0,255,0),4 ); //--Show detected matchesNamedwindow ("Good Matches & Object detection", Window_normal); Imshow ("Good Matches & Object detection", img_matches); Charc = Cvwaitkey (1); if(c = = -) Break; }    return 0;}

OPENCV Screen Tracking

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.