ssearch.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "opencv2/ximgproc/segmentation.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include "opencv2/core.hpp"
  4. #include "opencv2/imgproc.hpp"
  5. #include <iostream>
  6. #include <ctime>
  7. using namespace cv;
  8. using namespace cv::ximgproc::segmentation;
  9. static void help() {
  10. std::cout << std::endl <<
  11. "Usage:" << std::endl <<
  12. "./ssearch input_image (f|q)" << std::endl <<
  13. "f=fast, q=quality" << std::endl <<
  14. "Use l to display less rects, m to display more rects, q to quit" << std::endl;
  15. }
  16. int main(int argc, char** argv) {
  17. // If image path and f/q is not passed as command
  18. // line arguments, quit and display help message
  19. if (argc < 3) {
  20. help();
  21. return -1;
  22. }
  23. // speed-up using multithreads
  24. setUseOptimized(true);
  25. setNumThreads(4);
  26. // read image
  27. Mat im = imread(argv[1]);
  28. // resize image
  29. int newHeight = 200;
  30. int newWidth = im.cols*newHeight/im.rows;
  31. resize(im, im, Size(newWidth, newHeight));
  32. // create Selective Search Segmentation Object using default parameters
  33. Ptr<SelectiveSearchSegmentation> ss = createSelectiveSearchSegmentation();
  34. // set input image on which we will run segmentation
  35. ss->setBaseImage(im);
  36. // Switch to fast but low recall Selective Search method
  37. if (argv[2][0] == 'f') {
  38. ss->switchToSelectiveSearchFast();
  39. }
  40. // Switch to high recall but slow Selective Search method
  41. else if (argv[2][0] == 'q') {
  42. ss->switchToSelectiveSearchQuality();
  43. }
  44. // if argument is neither f nor q print help message
  45. else {
  46. help();
  47. return -2;
  48. }
  49. // run selective search segmentation on input image
  50. std::vector<Rect> rects;
  51. ss->process(rects);
  52. std::cout << "Total Number of Region Proposals: " << rects.size() << std::endl;
  53. // number of region proposals to show
  54. int numShowRects = 100;
  55. // increment to increase/decrease total number
  56. // of reason proposals to be shown
  57. int increment = 50;
  58. while(1) {
  59. // create a copy of original image
  60. Mat imOut = im.clone();
  61. // itereate over all the region proposals
  62. for(int i = 0; i < rects.size(); i++) {
  63. if (i < numShowRects) {
  64. rectangle(imOut, rects[i], Scalar(0, 255, 0));
  65. }
  66. else {
  67. break;
  68. }
  69. }
  70. // show output
  71. imshow("Output", imOut);
  72. // record key press
  73. int k = waitKey();
  74. // m is pressed
  75. if (k == 109) {
  76. // increase total number of rectangles to show by increment
  77. numShowRects += increment;
  78. }
  79. // l is pressed
  80. else if (k == 108 && numShowRects > increment) {
  81. // decrease total number of rectangles to show by increment
  82. numShowRects -= increment;
  83. }
  84. // q is pressed
  85. else if (k == 113) {
  86. break;
  87. }
  88. }
  89. return 0;
  90. }