slic.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. static const char* window_name = "SLIC Superpixels";
  13. static const char* keys =
  14. "{h help | | help menu}"
  15. "{c camera |0| camera id}"
  16. "{i image | | image file}"
  17. "{a algorithm |1| SLIC(0),SLICO(1),MSLIC(2)}"
  18. ;
  19. int main(int argc, char** argv)
  20. {
  21. CommandLineParser cmd(argc,argv,keys);
  22. if (cmd.has("help")) {
  23. cmd.about("This program demonstrates SLIC superpixels using OpenCV class SuperpixelSLIC.\n"
  24. "If no image file is supplied, try to open a webcam.\n"
  25. "Use [space] to toggle output mode, ['q' or 'Q' or 'esc'] to exit.\n");
  26. cmd.printMessage();
  27. return 0;
  28. }
  29. int capture = cmd.get<int>("camera");
  30. String img_file = cmd.get<String>("image");
  31. int algorithm = cmd.get<int>("algorithm");
  32. int region_size = 50;
  33. int ruler = 30;
  34. int min_element_size = 50;
  35. int num_iterations = 3;
  36. bool use_video_capture = img_file.empty();
  37. VideoCapture cap;
  38. Mat input_image;
  39. if( use_video_capture )
  40. {
  41. if( !cap.open(capture) )
  42. {
  43. cout << "Could not initialize capturing..."<<capture<<"\n";
  44. return -1;
  45. }
  46. }
  47. else
  48. {
  49. input_image = imread(img_file);
  50. if( input_image.empty() )
  51. {
  52. cout << "Could not open image..."<<img_file<<"\n";
  53. return -1;
  54. }
  55. }
  56. namedWindow(window_name, 0);
  57. createTrackbar("Algorithm", window_name, &algorithm, 2, 0);
  58. createTrackbar("Region size", window_name, &region_size, 200, 0);
  59. createTrackbar("Ruler", window_name, &ruler, 100, 0);
  60. createTrackbar("Connectivity", window_name, &min_element_size, 100, 0);
  61. createTrackbar("Iterations", window_name, &num_iterations, 12, 0);
  62. Mat result, mask;
  63. int display_mode = 0;
  64. for (;;)
  65. {
  66. Mat frame;
  67. if( use_video_capture )
  68. cap >> frame;
  69. else
  70. input_image.copyTo(frame);
  71. if( frame.empty() )
  72. break;
  73. result = frame;
  74. Mat converted;
  75. cvtColor(frame, converted, COLOR_BGR2HSV);
  76. double t = (double) getTickCount();
  77. Ptr<SuperpixelSLIC> slic = createSuperpixelSLIC(converted,algorithm+SLIC,region_size,float(ruler));
  78. slic->iterate(num_iterations);
  79. if (min_element_size>0)
  80. slic->enforceLabelConnectivity(min_element_size);
  81. t = ((double) getTickCount() - t) / getTickFrequency();
  82. cout << "SLIC" << (algorithm?'O':' ')
  83. << " segmentation took " << (int) (t * 1000)
  84. << " ms with " << slic->getNumberOfSuperpixels() << " superpixels" << endl;
  85. // get the contours for displaying
  86. slic->getLabelContourMask(mask, true);
  87. result.setTo(Scalar(0, 0, 255), mask);
  88. // display output
  89. switch (display_mode)
  90. {
  91. case 0: //superpixel contours
  92. imshow(window_name, result);
  93. break;
  94. case 1: //mask
  95. imshow(window_name, mask);
  96. break;
  97. case 2: //labels array
  98. {
  99. // use the last x bit to determine the color. Note that this does not
  100. // guarantee that 2 neighboring superpixels have different colors.
  101. // retrieve the segmentation result
  102. Mat labels;
  103. slic->getLabels(labels);
  104. const int num_label_bits = 2;
  105. labels &= (1 << num_label_bits) - 1;
  106. labels *= 1 << (16 - num_label_bits);
  107. imshow(window_name, labels);
  108. break;
  109. }
  110. }
  111. int c = waitKey(1) & 0xff;
  112. if( c == 'q' || c == 'Q' || c == 27 )
  113. break;
  114. else if( c == ' ' )
  115. display_mode = (display_mode + 1) % 3;
  116. }
  117. return 0;
  118. }