lucasamparo

Untitled

Oct 14th, 2019
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "opencv2/core.hpp"
  2. #include <opencv2/opencv.hpp>
  3. #include "opencv2/highgui.hpp"
  4. #include "opencv2/calib3d/calib3d.hpp"
  5. #include <opencv2/features2d.hpp>
  6. #include <iostream>
  7.  
  8. using namespace cv;
  9. using namespace std;
  10.  
  11.  
  12. int main(int argc, char *argv[]) {
  13.     Mat img1, img2;
  14.     img1 = imread(argv[1]);
  15.     img2 = imread(argv[2]);
  16.     Ptr<ORB> orb = ORB::create(50);
  17.     vector<KeyPoint> kp1, kp2;
  18.     Mat desc1, desc2;
  19.     cout<<"orb\n";
  20.     orb->detectAndCompute(img1, noArray(), kp1, desc1);
  21.     orb->detectAndCompute(img2, noArray(), kp2, desc2);
  22.     vector< vector<DMatch> > matches, matches1, matches2;
  23.     /* Options
  24.     BruteForce (it uses L2 )
  25.     BruteForce-L1
  26.     BruteForce-Hamming
  27.     BruteForce-Hamming(2)
  28.     FlannBased
  29.     */
  30.     Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
  31.     cout<<"matcher\n";
  32.     /* Options
  33.     match (espera só um vector<DMatch>)
  34.     knnMatch (precisa de um int)
  35.     radiusMatch (precisa de um float)
  36.     */
  37.     matcher->knnMatch(desc1, desc2, matches, 2);
  38.     Mat out_img;
  39.     drawMatches(img1, kp1, img2, kp2, matches, out_img, Scalar(255,255,255));
  40.     imshow("Result",out_img);
  41.     waitKey(0);
  42.     vector< vector<DMatch> > good_matches;
  43.     vector<Point2f> good_pt1, good_pt2;
  44.     std::vector<Point2f> obj;
  45.     for (int i = 0; i < matches.size(); i++)
  46.         if (matches[i][0].distance < 0.7*matches[i][1].distance){
  47.             good_matches.push_back(matches[i]);
  48.             good_pt1.push_back(kp1[matches[i][0].queryIdx].pt) ;
  49.             good_pt2.push_back(kp2[matches[i][1].trainIdx].pt) ;
  50.         }
  51.     drawMatches(img1, kp1, img2, kp2, good_matches, out_img, Scalar(255,255,255));
  52.     imshow("Result",out_img);
  53.     waitKey(0);
  54.  
  55.     Mat H = findHomography( good_pt1, good_pt2, RANSAC );
  56.     std::vector<Point2f> temp_corners(4), obj_corners1(4), obj_corners2(4);
  57.     obj_corners1[0] = cvPoint(0,0);
  58.     obj_corners1[1] = cvPoint( img1.cols, 0 );
  59.     obj_corners1[2] = cvPoint( img1.cols, img1.rows );
  60.     obj_corners1[3] = cvPoint( 0, img1.rows );
  61.     temp_corners[0] = cvPoint(0,0);
  62.     temp_corners[1] = cvPoint( img2.cols, 0 );
  63.     temp_corners[2] = cvPoint( img2.cols, img2.rows );
  64.     temp_corners[3] = cvPoint( 0, img2.rows );
  65.  
  66.     perspectiveTransform( temp_corners, obj_corners2, H);
  67.     obj_corners1.insert( obj_corners1.end(), obj_corners2.begin(), obj_corners2.end() );
  68.     float x_max = 0, y_max = 0, x_min = 10000, y_min= 10000;
  69.     for (int i = 0; i < obj_corners1.size(); i++) {
  70.         if (obj_corners1[i].x > x_max) {
  71.             x_max = obj_corners1[i].x + 0.5;
  72.             y_max = obj_corners1[i].y + 0.5;
  73.         }
  74.         if (obj_corners1[i].x < x_min) {
  75.             x_min = obj_corners1[i].x - 0.5;
  76.             y_min = obj_corners1[i].y - 0.5;
  77.         }
  78.     }
  79.     float data[9] = { 1, 0, -x_min, 0, 1, -y_min, 0, 0, 1};
  80.     Mat transf_matrix(H.rows, H.cols, H.type(), data) ;
  81.     std::cout<<transf_matrix<<" WP\n";
  82.     warpPerspective(img2, out_img, transf_matrix.dot(H), out_img.size()*2);
  83.     std::cout<<transf_matrix<<" GG\n";
  84.     imshow("Result",out_img);
  85.     waitKey(0);
  86. }
RAW Paste Data