seeds.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <opencv2/imgproc.hpp>
  2. #include <opencv2/highgui.hpp>
  3. #include <opencv2/imgcodecs.hpp>
  4. #include <opencv2/core/utility.hpp>
  5. #include <opencv2/ximgproc.hpp>
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <iostream>
  9. using namespace cv;
  10. using namespace cv::ximgproc;
  11. using namespace std;
  12. void trackbarChanged(int pos, void* data);
  13. static void help()
  14. {
  15. cout << "\nThis program demonstrates SEEDS superpixels using OpenCV class SuperpixelSEEDS\n"
  16. "Use [space] to toggle output mode\n"
  17. "\n"
  18. "It captures either from the camera of your choice: 0, 1, ... default 0\n"
  19. "Or from an input image\n"
  20. "Call:\n"
  21. "./seeds [camera #, default 0]\n"
  22. "./seeds [input image file]\n" << endl;
  23. }
  24. static const char* window_name = "SEEDS Superpixels";
  25. static bool init = false;
  26. void trackbarChanged(int, void*)
  27. {
  28. init = false;
  29. }
  30. int main(int argc, char** argv)
  31. {
  32. VideoCapture cap;
  33. Mat input_image;
  34. bool use_video_capture = false;
  35. help();
  36. if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])) )
  37. {
  38. cap.open(argc == 2 ? argv[1][0] - '0' : 0);
  39. use_video_capture = true;
  40. }
  41. else if( argc >= 2 )
  42. {
  43. input_image = imread(argv[1]);
  44. }
  45. if( use_video_capture )
  46. {
  47. if( !cap.isOpened() )
  48. {
  49. cout << "Could not initialize capturing...\n";
  50. return -1;
  51. }
  52. }
  53. else if( input_image.empty() )
  54. {
  55. cout << "Could not open image...\n";
  56. return -1;
  57. }
  58. namedWindow(window_name, 0);
  59. int num_iterations = 4;
  60. int prior = 2;
  61. bool double_step = false;
  62. int num_superpixels = 400;
  63. int num_levels = 4;
  64. int num_histogram_bins = 5;
  65. createTrackbar("Number of Superpixels", window_name, &num_superpixels, 1000, trackbarChanged);
  66. createTrackbar("Smoothing Prior", window_name, &prior, 5, trackbarChanged);
  67. createTrackbar("Number of Levels", window_name, &num_levels, 10, trackbarChanged);
  68. createTrackbar("Iterations", window_name, &num_iterations, 12, 0);
  69. Mat result, mask;
  70. Ptr<SuperpixelSEEDS> seeds;
  71. int width, height;
  72. int display_mode = 0;
  73. for (;;)
  74. {
  75. Mat frame;
  76. if( use_video_capture )
  77. cap >> frame;
  78. else
  79. input_image.copyTo(frame);
  80. if( frame.empty() )
  81. break;
  82. if( !init )
  83. {
  84. width = frame.size().width;
  85. height = frame.size().height;
  86. seeds = createSuperpixelSEEDS(width, height, frame.channels(), num_superpixels,
  87. num_levels, prior, num_histogram_bins, double_step);
  88. init = true;
  89. }
  90. Mat converted;
  91. cvtColor(frame, converted, COLOR_BGR2HSV);
  92. double t = (double) getTickCount();
  93. seeds->iterate(converted, num_iterations);
  94. result = frame;
  95. t = ((double) getTickCount() - t) / getTickFrequency();
  96. printf("SEEDS segmentation took %i ms with %3i superpixels\n",
  97. (int) (t * 1000), seeds->getNumberOfSuperpixels());
  98. /* retrieve the segmentation result */
  99. Mat labels;
  100. seeds->getLabels(labels);
  101. /* get the contours for displaying */
  102. seeds->getLabelContourMask(mask, false);
  103. result.setTo(Scalar(0, 0, 255), mask);
  104. /* display output */
  105. switch (display_mode)
  106. {
  107. case 0: //superpixel contours
  108. imshow(window_name, result);
  109. break;
  110. case 1: //mask
  111. imshow(window_name, mask);
  112. break;
  113. case 2: //labels array
  114. {
  115. // use the last x bit to determine the color. Note that this does not
  116. // guarantee that 2 neighboring superpixels have different colors.
  117. const int num_label_bits = 2;
  118. labels &= (1 << num_label_bits) - 1;
  119. labels *= 1 << (16 - num_label_bits);
  120. imshow(window_name, labels);
  121. }
  122. break;
  123. }
  124. int c = waitKey(1);
  125. if( (c & 255) == 'q' || c == 'Q' || (c & 255) == 27 )
  126. break;
  127. else if( (c & 255) == ' ' )
  128. display_mode = (display_mode + 1) % 3;
  129. }
  130. return 0;
  131. }