/opencv/samples/cpp/generic_descriptor_match.cpp

https://github.com/malcolmreynolds/OpenCV · C++ · 98 lines · 74 code · 20 blank · 4 comment · 6 complexity · d6305027e7d215b3865370a2e949f726 MD5 · raw file

  1. #include "opencv2/calib3d/calib3d.hpp"
  2. #include "opencv2/features2d/features2d.hpp"
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include "opencv2/imgproc/imgproc_c.h"
  5. #include <cstdio>
  6. using namespace cv;
  7. void help()
  8. {
  9. printf("Use the SURF descriptor for matching keypoints between 2 images\n");
  10. printf("Format: \n./generic_descriptor_match <image1> <image2> <algorithm> <XML params>\n");
  11. printf("For example: ./generic_descriptor_match ../c/scene_l.bmp ../c/scene_r.bmp FERN fern_params.xml\n");
  12. }
  13. IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
  14. const vector<KeyPoint>& features2, const vector<DMatch>& desc_idx);
  15. int main(int argc, char** argv)
  16. {
  17. if (argc != 5)
  18. {
  19. help();
  20. return 0;
  21. }
  22. std::string img1_name = std::string(argv[1]);
  23. std::string img2_name = std::string(argv[2]);
  24. std::string alg_name = std::string(argv[3]);
  25. std::string params_filename = std::string(argv[4]);
  26. Ptr<GenericDescriptorMatcher> descriptorMatcher = GenericDescriptorMatcher::create(alg_name, params_filename);
  27. if( descriptorMatcher == 0 )
  28. {
  29. printf ("Cannot create descriptor\n");
  30. return 0;
  31. }
  32. //printf("Reading the images...\n");
  33. IplImage* img1 = cvLoadImage(img1_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
  34. IplImage* img2 = cvLoadImage(img2_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
  35. // extract keypoints from the first image
  36. SURF surf_extractor(5.0e3);
  37. vector<KeyPoint> keypoints1;
  38. // printf("Extracting keypoints\n");
  39. surf_extractor(img1, Mat(), keypoints1);
  40. printf("Extracted %d keypoints from the first image\n", (int)keypoints1.size());
  41. vector<KeyPoint> keypoints2;
  42. surf_extractor(img2, Mat(), keypoints2);
  43. printf("Extracted %d keypoints from the second image\n", (int)keypoints2.size());
  44. printf("Finding nearest neighbors... \n");
  45. // find NN for each of keypoints2 in keypoints1
  46. vector<DMatch> matches2to1;
  47. descriptorMatcher->match( img2, keypoints2, img1, keypoints1, matches2to1 );
  48. printf("Done\n");
  49. IplImage* img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, matches2to1);
  50. cvNamedWindow("correspondences", 1);
  51. cvShowImage("correspondences", img_corr);
  52. cvWaitKey(0);
  53. cvReleaseImage(&img1);
  54. cvReleaseImage(&img2);
  55. cvReleaseImage(&img_corr);
  56. }
  57. IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
  58. const vector<KeyPoint>& features2, const vector<DMatch>& desc_idx)
  59. {
  60. IplImage* img_corr = cvCreateImage(cvSize(img1->width + img2->width, MAX(img1->height, img2->height)),
  61. IPL_DEPTH_8U, 3);
  62. cvSetImageROI(img_corr, cvRect(0, 0, img1->width, img1->height));
  63. cvCvtColor(img1, img_corr, CV_GRAY2RGB);
  64. cvSetImageROI(img_corr, cvRect(img1->width, 0, img2->width, img2->height));
  65. cvCvtColor(img2, img_corr, CV_GRAY2RGB);
  66. cvResetImageROI(img_corr);
  67. for (size_t i = 0; i < features1.size(); i++)
  68. {
  69. cvCircle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
  70. }
  71. for (size_t i = 0; i < features2.size(); i++)
  72. {
  73. CvPoint pt = cvPoint(cvRound(features2[i].pt.x + img1->width), cvRound(features2[i].pt.y));
  74. cvCircle(img_corr, pt, 3, CV_RGB(255, 0, 0));
  75. cvLine(img_corr, features1[desc_idx[i].trainIdx].pt, pt, CV_RGB(0, 255, 0));
  76. }
  77. return img_corr;
  78. }