edgeboxes_demo.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. */
  31. #include "opencv2/ximgproc.hpp"
  32. #include "opencv2/highgui.hpp"
  33. #include "opencv2/imgproc.hpp"
  34. #include <iostream>
  35. using namespace std;
  36. using namespace cv;
  37. using namespace cv::ximgproc;
  38. static void help()
  39. {
  40. std::cout << std::endl <<
  41. "This sample demonstrates structured edge detection and edgeboxes." << std::endl <<
  42. "Usage:" << std::endl <<
  43. "./edgeboxes_demo [<model>] [<input_image>]" << std::endl;
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. if (argc < 3)
  48. {
  49. help();
  50. return -1;
  51. }
  52. Ptr<StructuredEdgeDetection> pDollar = createStructuredEdgeDetection(argv[1]);
  53. Mat im;
  54. im = imread(argv[2]);
  55. Mat rgb_im;
  56. cvtColor(im, rgb_im, COLOR_BGR2RGB);
  57. rgb_im.convertTo(rgb_im, CV_32F, 1.0 / 255.0f);
  58. Mat edge_im;
  59. pDollar->detectEdges(rgb_im, edge_im);
  60. // computes orientation from edge map
  61. Mat O;
  62. pDollar->computeOrientation(edge_im, O);
  63. // apply edge nms
  64. Mat edge_nms;
  65. pDollar->edgesNms(edge_im, O, edge_nms, 2, 0, 1, true);
  66. std::vector<Rect> boxes;
  67. Ptr<EdgeBoxes> edgeboxes = createEdgeBoxes();
  68. edgeboxes->setMaxBoxes(30);
  69. edgeboxes->getBoundingBoxes(edge_nms, O, boxes);
  70. for(int i = 0; i < (int)boxes.size(); i++)
  71. {
  72. Point p1(boxes[i].x, boxes[i].y), p2(boxes[i].x + boxes[i].width, boxes[i].y + boxes[i].height);
  73. Scalar color(0, 255, 0);
  74. rectangle(im, p1, p2, color, 1);
  75. }
  76. imshow("Edge", edge_im);
  77. imshow("Nms", edge_nms);
  78. imshow("Image & boxes", im);
  79. waitKey(0);
  80. return 0;
  81. }