bgfg.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "opencv2/bgsegm.hpp"
  2. #include "opencv2/videoio.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include <opencv2/core/utility.hpp>
  5. #include <iostream>
  6. using namespace cv;
  7. using namespace cv::bgsegm;
  8. const String about =
  9. "\nA program demonstrating the use and capabilities of different background subtraction algorithms\n"
  10. "Using OpenCV version " + String(CV_VERSION) +
  11. "\n\nPress 'c' to change the algorithm"
  12. "\nPress 'm' to toggle showing only foreground mask or ghost effect"
  13. "\nPress 'n' to change number of threads"
  14. "\nPress SPACE to toggle wait delay of imshow"
  15. "\nPress 'q' or ESC to exit\n";
  16. const String algos[7] = { "GMG", "CNT", "KNN", "MOG", "MOG2", "GSOC", "LSBP" };
  17. static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName)
  18. {
  19. Ptr<BackgroundSubtractor> algo;
  20. if(algoName == String("GMG"))
  21. algo = createBackgroundSubtractorGMG(20, 0.7);
  22. else if(algoName == String("CNT"))
  23. algo = createBackgroundSubtractorCNT();
  24. else if(algoName == String("KNN"))
  25. algo = createBackgroundSubtractorKNN();
  26. else if(algoName == String("MOG"))
  27. algo = createBackgroundSubtractorMOG();
  28. else if(algoName == String("MOG2"))
  29. algo = createBackgroundSubtractorMOG2();
  30. else if(algoName == String("GSOC"))
  31. algo = createBackgroundSubtractorGSOC();
  32. else if(algoName == String("LSBP"))
  33. algo = createBackgroundSubtractorLSBP();
  34. return algo;
  35. }
  36. int main(int argc, char** argv)
  37. {
  38. CommandLineParser parser(argc, argv, "{@video | vtest.avi | path to a video file}");
  39. parser.about(about);
  40. parser.printMessage();
  41. String videoPath = samples::findFile(parser.get<String>(0),false);
  42. Ptr<BackgroundSubtractor> bgfs = createBGSubtractorByName(algos[0]);
  43. VideoCapture cap;
  44. cap.open(videoPath);
  45. if (!cap.isOpened())
  46. {
  47. std::cerr << "Cannot read video. Try moving video file to sample directory." << std::endl;
  48. return -1;
  49. }
  50. Mat frame, fgmask, segm;
  51. int delay = 30;
  52. int algo_index = 0;
  53. int nthreads = getNumberOfCPUs();
  54. bool show_fgmask = false;
  55. for (;;)
  56. {
  57. cap >> frame;
  58. if (frame.empty())
  59. {
  60. cap.set(CAP_PROP_POS_FRAMES, 0);
  61. cap >> frame;
  62. }
  63. bgfs->apply(frame, fgmask);
  64. if (show_fgmask)
  65. segm = fgmask;
  66. else
  67. {
  68. frame.convertTo(segm, CV_8U, 0.5);
  69. add(frame, Scalar(100, 100, 0), segm, fgmask);
  70. }
  71. putText(segm, algos[algo_index], Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
  72. putText(segm, format("%d threads", nthreads), Point(10, 60), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
  73. imshow("FG Segmentation", segm);
  74. int c = waitKey(delay);
  75. if (c == ' ')
  76. delay = delay == 30 ? 1 : 30;
  77. if (c == 'c' || c == 'C')
  78. {
  79. algo_index++;
  80. if ( algo_index > 6 )
  81. algo_index = 0;
  82. bgfs = createBGSubtractorByName(algos[algo_index]);
  83. }
  84. if (c == 'n' || c == 'N')
  85. {
  86. nthreads++;
  87. if ( nthreads > 8 )
  88. nthreads = 1;
  89. setNumThreads(nthreads);
  90. }
  91. if (c == 'm' || c == 'M')
  92. show_fgmask = !show_fgmask;
  93. if (c == 'q' || c == 'Q' || c == 27)
  94. break;
  95. }
  96. return 0;
  97. }