structured_edge_detection.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**************************************************************************************
  2. The structured forests for fast edge detection demo requires you to provide a model.
  3. This model can be found at the opencv_extra repository on Github on the following link:
  4. https://github.com/opencv/opencv_extra/blob/master/testdata/cv/ximgproc/model.yml.gz
  5. ***************************************************************************************/
  6. #include <opencv2/ximgproc.hpp>
  7. #include "opencv2/highgui.hpp"
  8. #include <iostream>
  9. using namespace cv;
  10. using namespace cv::ximgproc;
  11. const char* keys =
  12. {
  13. "{i || input image file name}"
  14. "{m || model file name}"
  15. "{o || output image file name}"
  16. };
  17. int main( int argc, const char** argv )
  18. {
  19. CommandLineParser parser(argc, argv, keys);
  20. parser.about("This sample demonstrates usage of structured forests for fast edge detection");
  21. parser.printMessage();
  22. if ( !parser.check() )
  23. {
  24. parser.printErrors();
  25. return -1;
  26. }
  27. String modelFilename = parser.get<String>("m");
  28. String inFilename = parser.get<String>("i");
  29. String outFilename = parser.get<String>("o");
  30. //! [imread]
  31. Mat image = imread(inFilename, IMREAD_COLOR);
  32. if ( image.empty() )
  33. CV_Error(Error::StsError, String("Cannot read image file: ") + inFilename);
  34. //! [imread]
  35. if ( modelFilename.size() == 0)
  36. CV_Error(Error::StsError, String("Empty model name"));
  37. //! [convert]
  38. image.convertTo(image, DataType<float>::type, 1/255.0);
  39. //! [convert]
  40. TickMeter tm;
  41. tm.start();
  42. //! [create]
  43. Ptr<StructuredEdgeDetection> pDollar =
  44. createStructuredEdgeDetection(modelFilename);
  45. //! [create]
  46. tm.stop();
  47. std::cout << "createStructuredEdgeDetection() time : " << tm << std::endl;
  48. tm.reset();
  49. tm.start();
  50. //! [detect]
  51. Mat edges;
  52. pDollar->detectEdges(image, edges);
  53. //! [detect]
  54. tm.stop();
  55. std::cout << "detectEdges() time : " << tm << std::endl;
  56. tm.reset();
  57. tm.start();
  58. //! [nms]
  59. // computes orientation from edge map
  60. Mat orientation_map;
  61. pDollar->computeOrientation(edges, orientation_map);
  62. // suppress edges
  63. Mat edge_nms;
  64. pDollar->edgesNms(edges, orientation_map, edge_nms, 2, 0, 1, true);
  65. //! [nms]
  66. tm.stop();
  67. std::cout << "nms time : " << tm << std::endl;
  68. //! [imshow]
  69. if ( outFilename.size() == 0 )
  70. {
  71. imshow("edges", edges);
  72. imshow("edges nms", edge_nms);
  73. waitKey(0);
  74. }
  75. else
  76. imwrite(outFilename, 255*edges);
  77. //! [imshow]
  78. return 0;
  79. }