ppf_load_match.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  3. //
  4. // By downloading, copying, installing or using the software you agree to this license.
  5. // If you do not agree to this license, do not download, install,
  6. // copy or use the software.
  7. //
  8. //
  9. // License Agreement
  10. // For Open Source Computer Vision Library
  11. //
  12. // Copyright (C) 2014, OpenCV Foundation, all rights reserved.
  13. // Third party copyrights are property of their respective owners.
  14. //
  15. // Redistribution and use in source and binary forms, with or without modification,
  16. // are permitted provided that the following conditions are met:
  17. //
  18. // * Redistribution's of source code must retain the above copyright notice,
  19. // this list of conditions and the following disclaimer.
  20. //
  21. // * Redistribution's in binary form must reproduce the above copyright notice,
  22. // this list of conditions and the following disclaimer in the documentation
  23. // and/or other materials provided with the distribution.
  24. //
  25. // * The name of the copyright holders may not be used to endorse or promote products
  26. // derived from this software without specific prior written permission.
  27. //
  28. // This software is provided by the copyright holders and contributors "as is" and
  29. // any express or implied warranties, including, but not limited to, the implied
  30. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  31. // In no event shall the Intel Corporation or contributors be liable for any direct,
  32. // indirect, incidental, special, exemplary, or consequential damages
  33. // (including, but not limited to, procurement of substitute goods or services;
  34. // loss of use, data, or profits; or business interruption) however caused
  35. // and on any theory of liability, whether in contract, strict liability,
  36. // or tort (including negligence or otherwise) arising in any way out of
  37. // the use of this software, even if advised of the possibility of such damage.
  38. //
  39. // Author: Tolga Birdal <tbirdal AT gmail.com>
  40. #include "opencv2/surface_matching.hpp"
  41. #include <iostream>
  42. #include "opencv2/surface_matching/ppf_helpers.hpp"
  43. #include "opencv2/core/utility.hpp"
  44. using namespace std;
  45. using namespace cv;
  46. using namespace ppf_match_3d;
  47. static void help(const string& errorMessage)
  48. {
  49. cout << "Program init error : "<< errorMessage << endl;
  50. cout << "\nUsage : ppf_matching [input model file] [input scene file]"<< endl;
  51. cout << "\nPlease start again with new parameters"<< endl;
  52. }
  53. int main(int argc, char** argv)
  54. {
  55. // welcome message
  56. cout << "****************************************************" << endl;
  57. cout << "* Surface Matching demonstration : demonstrates the use of surface matching"
  58. " using point pair features." << endl;
  59. cout << "* The sample loads a model and a scene, where the model lies in a different"
  60. " pose than the training.\n* It then trains the model and searches for it in the"
  61. " input scene. The detected poses are further refined by ICP\n* and printed to the "
  62. " standard output." << endl;
  63. cout << "****************************************************" << endl;
  64. if (argc < 3)
  65. {
  66. help("Not enough input arguments");
  67. exit(1);
  68. }
  69. #if (defined __x86_64__ || defined _M_X64)
  70. cout << "Running on 64 bits" << endl;
  71. #else
  72. cout << "Running on 32 bits" << endl;
  73. #endif
  74. #ifdef _OPENMP
  75. cout << "Running with OpenMP" << endl;
  76. #else
  77. cout << "Running without OpenMP and without TBB" << endl;
  78. #endif
  79. string modelFileName = (string)argv[1];
  80. string sceneFileName = (string)argv[2];
  81. Mat pc = loadPLYSimple(modelFileName.c_str(), 1);
  82. // Now train the model
  83. cout << "Training..." << endl;
  84. int64 tick1 = cv::getTickCount();
  85. ppf_match_3d::PPF3DDetector detector(0.025, 0.05);
  86. detector.trainModel(pc);
  87. int64 tick2 = cv::getTickCount();
  88. cout << endl << "Training complete in "
  89. << (double)(tick2-tick1)/ cv::getTickFrequency()
  90. << " sec" << endl << "Loading model..." << endl;
  91. // Read the scene
  92. Mat pcTest = loadPLYSimple(sceneFileName.c_str(), 1);
  93. // Match the model to the scene and get the pose
  94. cout << endl << "Starting matching..." << endl;
  95. vector<Pose3DPtr> results;
  96. tick1 = cv::getTickCount();
  97. detector.match(pcTest, results, 1.0/40.0, 0.05);
  98. tick2 = cv::getTickCount();
  99. cout << endl << "PPF Elapsed Time " <<
  100. (tick2-tick1)/cv::getTickFrequency() << " sec" << endl;
  101. //check results size from match call above
  102. size_t results_size = results.size();
  103. cout << "Number of matching poses: " << results_size;
  104. if (results_size == 0) {
  105. cout << endl << "No matching poses found. Exiting." << endl;
  106. exit(0);
  107. }
  108. // Get only first N results - but adjust to results size if num of results are less than that specified by N
  109. size_t N = 2;
  110. if (results_size < N) {
  111. cout << endl << "Reducing matching poses to be reported (as specified in code): "
  112. << N << " to the number of matches found: " << results_size << endl;
  113. N = results_size;
  114. }
  115. vector<Pose3DPtr> resultsSub(results.begin(),results.begin()+N);
  116. // Create an instance of ICP
  117. ICP icp(100, 0.005f, 2.5f, 8);
  118. int64 t1 = cv::getTickCount();
  119. // Register for all selected poses
  120. cout << endl << "Performing ICP on " << N << " poses..." << endl;
  121. icp.registerModelToScene(pc, pcTest, resultsSub);
  122. int64 t2 = cv::getTickCount();
  123. cout << endl << "ICP Elapsed Time " <<
  124. (t2-t1)/cv::getTickFrequency() << " sec" << endl;
  125. cout << "Poses: " << endl;
  126. // debug first five poses
  127. for (size_t i=0; i<resultsSub.size(); i++)
  128. {
  129. Pose3DPtr result = resultsSub[i];
  130. cout << "Pose Result " << i << endl;
  131. result->printPose();
  132. if (i==0)
  133. {
  134. Mat pct = transformPCPose(pc, result->pose);
  135. writePLY(pct, "para6700PCTrans.ply");
  136. }
  137. }
  138. return 0;
  139. }