/opencv/samples/c/one_way_sample.cpp

https://github.com/malcolmreynolds/OpenCV · C++ · 119 lines · 84 code · 22 blank · 13 comment · 5 complexity · fe74511eed250f1e130726e2d7caa8a6 MD5 · raw file

  1. /*
  2. * one_way_sample.cpp
  3. * outlet_detection
  4. *
  5. * Created by Victor Eruhimov on 8/5/09.
  6. * Copyright 2009 Argus Corp. All rights reserved.
  7. *
  8. */
  9. #include "opencv2/imgproc/imgproc.hpp"
  10. #include "opencv2/features2d/features2d.hpp"
  11. #include "opencv2/highgui/highgui.hpp"
  12. #include "opencv2/imgproc/imgproc_c.h"
  13. #include <string>
  14. #include <stdio.h>
  15. void help()
  16. {
  17. printf("\nThis program demonstrates the one way interest point descriptor found in features2d.hpp\n"
  18. "Correspondences are drawn\n");
  19. printf("Format: \n./one_way_sample <path_to_samples> <image1> <image2>\n");
  20. printf("For example: ./one_way_sample . ../c/scene_l.bmp ../c/scene_r.bmp\n");
  21. }
  22. using namespace cv;
  23. IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
  24. const vector<KeyPoint>& features2, const vector<int>& desc_idx);
  25. int main(int argc, char** argv)
  26. {
  27. const char images_list[] = "one_way_train_images.txt";
  28. const CvSize patch_size = cvSize(24, 24);
  29. const int pose_count = 50;
  30. if (argc != 4)
  31. {
  32. help();
  33. return 0;
  34. }
  35. std::string path_name = argv[1];
  36. std::string img1_name = path_name + "/" + std::string(argv[2]);
  37. std::string img2_name = path_name + "/" + std::string(argv[3]);
  38. printf("Reading the images...\n");
  39. IplImage* img1 = cvLoadImage(img1_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
  40. IplImage* img2 = cvLoadImage(img2_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
  41. // extract keypoints from the first image
  42. SURF surf_extractor(5.0e3);
  43. vector<KeyPoint> keypoints1;
  44. // printf("Extracting keypoints\n");
  45. surf_extractor(img1, Mat(), keypoints1);
  46. printf("Extracted %d keypoints...\n", (int)keypoints1.size());
  47. printf("Training one way descriptors... \n");
  48. // create descriptors
  49. OneWayDescriptorBase descriptors(patch_size, pose_count, OneWayDescriptorBase::GetPCAFilename(), path_name,
  50. images_list);
  51. descriptors.CreateDescriptorsFromImage(img1, keypoints1);
  52. printf("done\n");
  53. // extract keypoints from the second image
  54. vector<KeyPoint> keypoints2;
  55. surf_extractor(img2, Mat(), keypoints2);
  56. printf("Extracted %d keypoints from the second image...\n", (int)keypoints2.size());
  57. printf("Finding nearest neighbors...");
  58. // find NN for each of keypoints2 in keypoints1
  59. vector<int> desc_idx;
  60. desc_idx.resize(keypoints2.size());
  61. for (size_t i = 0; i < keypoints2.size(); i++)
  62. {
  63. int pose_idx = 0;
  64. float distance = 0;
  65. descriptors.FindDescriptor(img2, keypoints2[i].pt, desc_idx[i], pose_idx, distance);
  66. }
  67. printf("done\n");
  68. IplImage* img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, desc_idx);
  69. cvNamedWindow("correspondences", 1);
  70. cvShowImage("correspondences", img_corr);
  71. cvWaitKey(0);
  72. cvReleaseImage(&img1);
  73. cvReleaseImage(&img2);
  74. cvReleaseImage(&img_corr);
  75. }
  76. IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
  77. const vector<KeyPoint>& features2, const vector<int>& desc_idx)
  78. {
  79. IplImage* img_corr = cvCreateImage(cvSize(img1->width + img2->width, MAX(img1->height, img2->height)),
  80. IPL_DEPTH_8U, 3);
  81. cvSetImageROI(img_corr, cvRect(0, 0, img1->width, img1->height));
  82. cvCvtColor(img1, img_corr, CV_GRAY2RGB);
  83. cvSetImageROI(img_corr, cvRect(img1->width, 0, img2->width, img2->height));
  84. cvCvtColor(img2, img_corr, CV_GRAY2RGB);
  85. cvResetImageROI(img_corr);
  86. for (size_t i = 0; i < features1.size(); i++)
  87. {
  88. cvCircle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
  89. }
  90. for (size_t i = 0; i < features2.size(); i++)
  91. {
  92. CvPoint pt = cvPoint((int)features2[i].pt.x + img1->width, (int)features2[i].pt.y);
  93. cvCircle(img_corr, pt, 3, CV_RGB(255, 0, 0));
  94. cvLine(img_corr, features1[desc_idx[i]].pt, pt, CV_RGB(0, 255, 0));
  95. }
  96. return img_corr;
  97. }