radius_matching.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2014, Biagio Montesano, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include <iostream>
  42. #include <opencv2/opencv_modules.hpp>
  43. #ifdef HAVE_OPENCV_FEATURES2D
  44. #include <opencv2/line_descriptor.hpp>
  45. #include <opencv2/core/utility.hpp>
  46. #include <opencv2/imgproc.hpp>
  47. #include <opencv2/features2d.hpp>
  48. #include <opencv2/highgui.hpp>
  49. #include <vector>
  50. using namespace cv;
  51. using namespace cv::line_descriptor;
  52. static const std::string images[] =
  53. { "cameraman.jpg", "church.jpg", "church2.png", "einstein.jpg", "stuff.jpg" };
  54. static const char* keys =
  55. { "{@image_path | | Image path }" };
  56. static void help()
  57. {
  58. std::cout << "\nThis example shows the functionalities of radius matching " << "Please, run this sample using a command in the form\n"
  59. << "./example_line_descriptor_radius_matching <path_to_input_images>/" << std::endl;
  60. }
  61. int main( int argc, char** argv )
  62. {
  63. /* get parameters from comand line */
  64. CommandLineParser parser( argc, argv, keys );
  65. String pathToImages = parser.get < String > ( 0 );
  66. /* create structures for hosting KeyLines and descriptors */
  67. int num_elements = sizeof ( images ) / sizeof ( images[0] );
  68. std::vector < Mat > descriptorsMat;
  69. std::vector < std::vector<KeyLine> > linesMat;
  70. /*create a pointer to a BinaryDescriptor object */
  71. Ptr < BinaryDescriptor > bd = BinaryDescriptor::createBinaryDescriptor();
  72. /* compute lines and descriptors */
  73. for ( int i = 0; i < num_elements; i++ )
  74. {
  75. /* get path to image */
  76. std::stringstream image_path;
  77. image_path << pathToImages << images[i];
  78. std::cout << image_path.str().c_str() << std::endl;
  79. /* load image */
  80. Mat loadedImage = imread( image_path.str().c_str(), 1 );
  81. if( loadedImage.data == NULL )
  82. {
  83. std::cout << "Could not load images." << std::endl;
  84. help();
  85. exit( -1 );
  86. }
  87. /* compute lines and descriptors */
  88. std::vector < KeyLine > lines;
  89. Mat computedDescr;
  90. bd->detect( loadedImage, lines );
  91. bd->compute( loadedImage, lines, computedDescr );
  92. descriptorsMat.push_back( computedDescr );
  93. linesMat.push_back( lines );
  94. }
  95. /* compose a queries matrix */
  96. Mat queries;
  97. for ( size_t j = 0; j < descriptorsMat.size(); j++ )
  98. {
  99. if( descriptorsMat[j].rows >= 5 )
  100. queries.push_back( descriptorsMat[j].rowRange( 0, 5 ) );
  101. else if( descriptorsMat[j].rows > 0 && descriptorsMat[j].rows < 5 )
  102. queries.push_back( descriptorsMat[j] );
  103. }
  104. std::cout << "It has been generated a matrix of " << queries.rows << " descriptors" << std::endl;
  105. /* create a BinaryDescriptorMatcher object */
  106. Ptr < BinaryDescriptorMatcher > bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
  107. /* populate matcher */
  108. bdm->add( descriptorsMat );
  109. /* compute matches */
  110. std::vector < std::vector<DMatch> > matches;
  111. bdm->radiusMatch( queries, matches, 30 );
  112. std::cout << "size matches sample " << matches.size() << std::endl;
  113. for ( int i = 0; i < (int) matches.size(); i++ )
  114. {
  115. for ( int j = 0; j < (int) matches[i].size(); j++ )
  116. {
  117. std::cout << "match: " << matches[i][j].queryIdx << " " << matches[i][j].trainIdx << " " << matches[i][j].distance << std::endl;
  118. }
  119. }
  120. }
  121. #else
  122. int main()
  123. {
  124. std::cerr << "OpenCV was built without features2d module" << std::endl;
  125. return 0;
  126. }
  127. #endif // HAVE_OPENCV_FEATURES2D