matching.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #define MATCHES_DIST_THRESHOLD 25
  50. using namespace cv;
  51. using namespace cv::line_descriptor;
  52. static const char* keys =
  53. { "{@image_path1 | | Image path 1 }"
  54. "{@image_path2 | | Image path 2 }" };
  55. static void help()
  56. {
  57. std::cout << "\nThis example shows the functionalities of lines extraction " << "and descriptors computation furnished by BinaryDescriptor class\n"
  58. << "Please, run this sample using a command in the form\n" << "./example_line_descriptor_compute_descriptors <path_to_input_image 1>"
  59. << "<path_to_input_image 2>" << std::endl;
  60. }
  61. int main( int argc, char** argv )
  62. {
  63. /* get parameters from command line */
  64. CommandLineParser parser( argc, argv, keys );
  65. String image_path1 = parser.get<String>( 0 );
  66. String image_path2 = parser.get<String>( 1 );
  67. if( image_path1.empty() || image_path2.empty() )
  68. {
  69. help();
  70. return -1;
  71. }
  72. /* load image */
  73. cv::Mat imageMat1 = imread( image_path1, 1 );
  74. cv::Mat imageMat2 = imread( image_path2, 1 );
  75. if( imageMat1.data == NULL || imageMat2.data == NULL )
  76. {
  77. std::cout << "Error, images could not be loaded. Please, check their path" << std::endl;
  78. }
  79. /* create binary masks */
  80. cv::Mat mask1 = Mat::ones( imageMat1.size(), CV_8UC1 );
  81. cv::Mat mask2 = Mat::ones( imageMat2.size(), CV_8UC1 );
  82. /* create a pointer to a BinaryDescriptor object with default parameters */
  83. Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor( );
  84. /* compute lines and descriptors */
  85. std::vector<KeyLine> keylines1, keylines2;
  86. cv::Mat descr1, descr2;
  87. ( *bd )( imageMat1, mask1, keylines1, descr1, false, false );
  88. ( *bd )( imageMat2, mask2, keylines2, descr2, false, false );
  89. /* select keylines from first octave and their descriptors */
  90. std::vector<KeyLine> lbd_octave1, lbd_octave2;
  91. Mat left_lbd, right_lbd;
  92. for ( int i = 0; i < (int) keylines1.size(); i++ )
  93. {
  94. if( keylines1[i].octave == 0 )
  95. {
  96. lbd_octave1.push_back( keylines1[i] );
  97. left_lbd.push_back( descr1.row( i ) );
  98. }
  99. }
  100. for ( int j = 0; j < (int) keylines2.size(); j++ )
  101. {
  102. if( keylines2[j].octave == 0 )
  103. {
  104. lbd_octave2.push_back( keylines2[j] );
  105. right_lbd.push_back( descr2.row( j ) );
  106. }
  107. }
  108. /* create a BinaryDescriptorMatcher object */
  109. Ptr<BinaryDescriptorMatcher> bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
  110. /* require match */
  111. std::vector<DMatch> matches;
  112. bdm->match( left_lbd, right_lbd, matches );
  113. /* select best matches */
  114. std::vector<DMatch> good_matches;
  115. for ( int i = 0; i < (int) matches.size(); i++ )
  116. {
  117. if( matches[i].distance < MATCHES_DIST_THRESHOLD )
  118. good_matches.push_back( matches[i] );
  119. }
  120. /* plot matches */
  121. cv::Mat outImg;
  122. cv::Mat scaled1, scaled2;
  123. std::vector<char> mask( matches.size(), 1 );
  124. drawLineMatches( imageMat1, lbd_octave1, imageMat2, lbd_octave2, good_matches, outImg, Scalar::all( -1 ), Scalar::all( -1 ), mask,
  125. DrawLinesMatchesFlags::DEFAULT );
  126. imshow( "Matches", outImg );
  127. waitKey();
  128. imwrite("/home/ubisum/Desktop/images/env_match/matches.jpg", outImg);
  129. /* create an LSD detector */
  130. Ptr<LSDDetector> lsd = LSDDetector::createLSDDetector();
  131. /* detect lines */
  132. std::vector<KeyLine> klsd1, klsd2;
  133. Mat lsd_descr1, lsd_descr2;
  134. lsd->detect( imageMat1, klsd1, 2, 2, mask1 );
  135. lsd->detect( imageMat2, klsd2, 2, 2, mask2 );
  136. /* compute descriptors for lines from first octave */
  137. bd->compute( imageMat1, klsd1, lsd_descr1 );
  138. bd->compute( imageMat2, klsd2, lsd_descr2 );
  139. /* select lines and descriptors from first octave */
  140. std::vector<KeyLine> octave0_1, octave0_2;
  141. Mat leftDEscr, rightDescr;
  142. for ( int i = 0; i < (int) klsd1.size(); i++ )
  143. {
  144. if( klsd1[i].octave == 1 )
  145. {
  146. octave0_1.push_back( klsd1[i] );
  147. leftDEscr.push_back( lsd_descr1.row( i ) );
  148. }
  149. }
  150. for ( int j = 0; j < (int) klsd2.size(); j++ )
  151. {
  152. if( klsd2[j].octave == 1 )
  153. {
  154. octave0_2.push_back( klsd2[j] );
  155. rightDescr.push_back( lsd_descr2.row( j ) );
  156. }
  157. }
  158. /* compute matches */
  159. std::vector<DMatch> lsd_matches;
  160. bdm->match( leftDEscr, rightDescr, lsd_matches );
  161. /* select best matches */
  162. good_matches.clear();
  163. for ( int i = 0; i < (int) lsd_matches.size(); i++ )
  164. {
  165. if( lsd_matches[i].distance < MATCHES_DIST_THRESHOLD )
  166. good_matches.push_back( lsd_matches[i] );
  167. }
  168. /* plot matches */
  169. cv::Mat lsd_outImg;
  170. resize( imageMat1, imageMat1, Size( imageMat1.cols / 2, imageMat1.rows / 2 ), 0, 0, INTER_LINEAR_EXACT );
  171. resize( imageMat2, imageMat2, Size( imageMat2.cols / 2, imageMat2.rows / 2 ), 0, 0, INTER_LINEAR_EXACT );
  172. std::vector<char> lsd_mask( matches.size(), 1 );
  173. drawLineMatches( imageMat1, octave0_1, imageMat2, octave0_2, good_matches, lsd_outImg, Scalar::all( -1 ), Scalar::all( -1 ), lsd_mask,
  174. DrawLinesMatchesFlags::DEFAULT );
  175. imshow( "LSD matches", lsd_outImg );
  176. waitKey();
  177. }
  178. #else
  179. int main()
  180. {
  181. std::cerr << "OpenCV was built without features2d module" << std::endl;
  182. return 0;
  183. }
  184. #endif // HAVE_OPENCV_FEATURES2D