lines_extraction.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. using namespace cv;
  50. using namespace cv::line_descriptor;
  51. using namespace std;
  52. static const char* keys =
  53. { "{@image_path | | Image path }" };
  54. static void help()
  55. {
  56. cout << "\nThis example shows the functionalities of lines extraction " << "furnished by BinaryDescriptor class\n"
  57. << "Please, run this sample using a command in the form\n" << "./example_line_descriptor_lines_extraction <path_to_input_image>" << endl;
  58. }
  59. int main( int argc, char** argv )
  60. {
  61. /* get parameters from comand line */
  62. CommandLineParser parser( argc, argv, keys );
  63. String image_path = parser.get<String>( 0 );
  64. if( image_path.empty() )
  65. {
  66. help();
  67. return -1;
  68. }
  69. /* load image */
  70. cv::Mat imageMat = imread( image_path, 1 );
  71. if( imageMat.data == NULL )
  72. {
  73. std::cout << "Error, image could not be loaded. Please, check its path" << std::endl;
  74. return -1;
  75. }
  76. /* create a random binary mask */
  77. cv::Mat mask = Mat::ones( imageMat.size(), CV_8UC1 );
  78. /* create a pointer to a BinaryDescriptor object with deafult parameters */
  79. Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
  80. /* create a structure to store extracted lines */
  81. vector<KeyLine> lines;
  82. /* extract lines */
  83. cv::Mat output = imageMat.clone();
  84. bd->detect( imageMat, lines, mask );
  85. /* draw lines extracted from octave 0 */
  86. if( output.channels() == 1 )
  87. cvtColor( output, output, COLOR_GRAY2BGR );
  88. for ( size_t i = 0; i < lines.size(); i++ )
  89. {
  90. KeyLine kl = lines[i];
  91. if( kl.octave == 0)
  92. {
  93. /* get a random color */
  94. int R = ( rand() % (int) ( 255 + 1 ) );
  95. int G = ( rand() % (int) ( 255 + 1 ) );
  96. int B = ( rand() % (int) ( 255 + 1 ) );
  97. /* get extremes of line */
  98. Point pt1 = Point2f( kl.startPointX, kl.startPointY );
  99. Point pt2 = Point2f( kl.endPointX, kl.endPointY );
  100. /* draw line */
  101. line( output, pt1, pt2, Scalar( B, G, R ), 3 );
  102. }
  103. }
  104. /* show lines on image */
  105. imshow( "Lines", output );
  106. waitKey();
  107. }
  108. #else
  109. int main()
  110. {
  111. std::cerr << "OpenCV was built without features2d module" << std::endl;
  112. return 0;
  113. }
  114. #endif // HAVE_OPENCV_FEATURES2D