Advertisement
Guest User

new

a guest
Nov 27th, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include "opencv2/core/core.hpp"
  4. #include "opencv2/features2d/features2d.hpp"
  5. #include "opencv2/highgui/highgui.hpp"
  6. #include "opencv2/calib3d/calib3d.hpp"
  7. #include "opencv2/imgcodecs/imgcodecs.hpp"
  8. #include <opencv2/videoio/videoio.hpp>
  9. #include <opencv2/video/video.hpp>
  10. #include "opencv2/xfeatures2d/nonfree.hpp"
  11. #include "opencv2/xfeatures2d.hpp"
  12. #include "opencv2/imgproc/imgproc.hpp"
  13. #include "opencv2/objdetect.hpp"
  14. #include "opencv2/calib3d.hpp"
  15. #include <stdlib.h>
  16.  
  17. using namespace cv;
  18. using namespace cv::xfeatures2d;
  19. FlannBasedMatcher matcher;
  20. using namespace std;
  21.  
  22. Mat img_scene,img_object;
  23.  
  24. int main(int argc, char** argv )
  25. {
  26.  
  27.     String imageName( "/home/zubair/Desktop/EXAMPLE/new12.jpg" );
  28.  
  29.        if( argc > 1)
  30.     {
  31.        imageName = argv[1];
  32.     }
  33.  
  34.     img_object = imread(imageName,CV_LOAD_IMAGE_GRAYSCALE );
  35.     //img_scene = imread(argv[2],CV_LOAD_IMAGE_GRAYSCALE );
  36.  
  37.     VideoCapture cap(0);
  38.     if (!cap.isOpened())
  39.      {
  40.           cout << "Cannot access camera" << endl;
  41.           return -1;
  42.      }
  43.  
  44.     if ( !img_object.data )
  45.     {
  46.         printf("No object image \n");
  47.         return -1;
  48.     }
  49.  
  50. while(1)
  51.     {
  52.  
  53.  
  54.     //namedWindow("obj", WINDOW_AUTOSIZE );
  55.     //namedWindow("scene",WINDOW_AUTOSIZE );
  56.     //imshow("obj", img_object);
  57.     //imshow("scene", img_scene);
  58.  
  59.     cap >> img_scene;
  60.  
  61.     if(img_scene.empty())
  62.     {
  63.         cout<<"no Frame in Image scene"<<endl;
  64.         return -1;
  65.     }
  66.  
  67.     imshow("image scene",img_scene);
  68.     imshow("object image",img_object);
  69.  
  70.     if( waitKey(10) == 27 ) break;
  71.     int minHessian = 400;
  72.  
  73.   Ptr<SURF> detector = SURF::create(minHessian);
  74.  
  75.   std::vector<KeyPoint> keypoints_object, keypoints_scene;
  76.  
  77.   detector->detect( img_object, keypoints_object );
  78.   detector->detect( img_scene, keypoints_scene );
  79.  
  80.   //-- Step 2: Calculate descriptors (feature vectors)
  81.   Ptr<SURF> extractor = SURF::create();
  82.  
  83.   Mat descriptors_object, descriptors_scene;
  84.  
  85.   extractor->compute( img_object, keypoints_object, descriptors_object );
  86.   extractor->compute( img_scene, keypoints_scene, descriptors_scene );
  87.  
  88.   //-- Step 3: Matching descriptor vectors using FLANN matcher
  89.   FlannBasedMatcher matcher;
  90.   std::vector< DMatch > matches;
  91.   matcher.match( descriptors_object, descriptors_scene, matches );
  92.  
  93.   double max_dist = 0; double min_dist = 100;
  94.  
  95.   //-- Quick calculation of max and min distances between keypoints
  96.   for( int i = 0; i < descriptors_object.rows; i++ )
  97.   { double dist = matches[i].distance;
  98.     if( dist < min_dist ) min_dist = dist;
  99.     if( dist > max_dist ) max_dist = dist;
  100.   }
  101.  
  102.   printf("-- Max dist : %f \n", max_dist );
  103.   printf("-- Min dist : %f \n", min_dist );
  104.  
  105.   //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
  106.   std::vector< DMatch > good_matches;
  107.  
  108.   for( int i = 0; i < descriptors_object.rows; i++ )
  109.   { if( matches[i].distance < 3*min_dist )
  110.      { good_matches.push_back( matches[i]); }
  111.   }
  112.  
  113.   Mat img_matches;
  114.   drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
  115.                good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
  116.                vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
  117.  
  118.   //-- Localize the object
  119.   std::vector<Point2f> obj;
  120.   std::vector<Point2f> scene;
  121.  
  122.   for( unsigned int i = 0; i < good_matches.size(); i++ )
  123.   {
  124.     //-- Get the keypoints from the good matches
  125.     obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
  126.     scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
  127.   }
  128.  
  129.   Mat H = findHomography( obj, scene, CV_RANSAC );
  130.  
  131.   //-- Get the corners from the image_1 ( the object to be "detected" )
  132.   std::vector<Point2f> obj_corners(4);
  133.   obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
  134.   obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
  135.   std::vector<Point2f> scene_corners(4);
  136.  
  137.   perspectiveTransform( obj_corners, scene_corners, H);
  138.  
  139.   //-- Draw lines between the corners (the mapped object in the scene - image_2 )
  140.   line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
  141.   line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  142.   line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  143.   line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  144.  
  145.   //-- Show detected matches
  146.   imshow( "Good Matches & Object detection", img_matches );
  147.  
  148. }
  149.     waitKey(10);
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement