Advertisement
Guest User

opencv_test.cpp

a guest
Aug 12th, 2014
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. // opencv_test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "opencv2/imgproc/imgproc.hpp"
  6. #include "opencv2/highgui/highgui.hpp"
  7. #include "opencv2/contrib/contrib.hpp"
  8.  
  9. #include <iostream>
  10.  
  11. using namespace cv;
  12. using namespace std;
  13.  
  14. static void help()
  15. {
  16.  
  17.    cout << "\nThis program demonstrates Chamfer matching -- computing a distance between an \n"
  18.             "edge template and a query edge image.\n"
  19.             "Usage: \n"
  20.             "./chamfer <image edge map> <template edge map>,"
  21.             " By default the inputs are logo_in_clutter.png logo.png\n";
  22. }
  23.  
  24. const char* keys =
  25. {
  26.     "{1| |logo_in_clutter.png|image edge map    }"
  27.     "{2| |logo.png               |template edge map}"
  28. };
  29.  
  30. int main( int argc, const char** argv )
  31. {
  32.  
  33.     help();
  34.     CommandLineParser parser(argc, argv, keys);
  35.     string image = parser.get<string>("1");
  36.     string templ = parser.get<string>("2");
  37.     Mat img = imread(image.c_str(), 0);
  38.     Mat tpl = imread(templ.c_str(), 0);
  39.    
  40.     if (img.empty() || tpl.empty())
  41.     {
  42.         cout << "Could not read image file " << image << " or " << templ << "." << endl;
  43.         return -1;
  44.     }
  45.     Mat cimg;
  46.     cvtColor(img, cimg, COLOR_GRAY2BGR);
  47.    
  48.     // if the image and the template are not edge maps but normal grayscale images,
  49.     // you might want to uncomment the lines below to produce the maps. You can also
  50.     // run Sobel instead of Canny.
  51.  
  52.     Canny(img, img, 5, 50, 3);
  53.     Canny(tpl, tpl, 5, 50, 3);
  54.    
  55.     vector<vector<Point> > results;
  56.     vector<float> costs;
  57.  
  58.     int best = chamferMatching2( img, tpl, results, costs );
  59.    
  60.     if( best < 0 )
  61.     {
  62.         cout << "matching not found" << endl;
  63.         return -1;
  64.     }
  65.    
  66.     size_t i, n = results[best].size();
  67.     for( i = 0; i < n; i++ )
  68.     {
  69.         Point pt = results[best][i];
  70.         if( pt.inside(Rect(0, 0, cimg.cols, cimg.rows)) )
  71.            cimg.at<Vec3b>(pt.y, pt.x) = Vec3b(0, 255, 0);
  72.     }
  73.  
  74.     imshow("result", cimg);
  75.  
  76.     waitKey();
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement