bgfg_segm.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <iostream>
  2. #include <string>
  3. #include "opencv2/core.hpp"
  4. #include "opencv2/core/ocl.hpp"
  5. #include "opencv2/core/utility.hpp"
  6. #include "opencv2/videoio.hpp"
  7. #include "opencv2/highgui.hpp"
  8. #include "opencv2/video.hpp"
  9. using namespace std;
  10. using namespace cv;
  11. #define M_MOG2 2
  12. #define M_KNN 3
  13. int main(int argc, const char** argv)
  14. {
  15. CommandLineParser cmd(argc, argv,
  16. "{ c camera | | use camera }"
  17. "{ f file | ../data/vtest.avi | input video file }"
  18. "{ t type | mog2 | method's type (knn, mog2) }"
  19. "{ h help | | print help message }"
  20. "{ m cpu_mode | false | press 'm' to switch OpenCL<->CPU}");
  21. if (cmd.has("help"))
  22. {
  23. cout << "Usage : bgfg_segm [options]" << endl;
  24. cout << "Available options:" << endl;
  25. cmd.printMessage();
  26. return EXIT_SUCCESS;
  27. }
  28. bool useCamera = cmd.has("camera");
  29. string file = cmd.get<string>("file");
  30. string method = cmd.get<string>("type");
  31. if (method != "mog" && method != "mog2")
  32. {
  33. cerr << "Incorrect method" << endl;
  34. return EXIT_FAILURE;
  35. }
  36. int m = method == "mog2" ? M_MOG2 : M_KNN;
  37. VideoCapture cap;
  38. if (useCamera)
  39. cap.open(0);
  40. else
  41. cap.open(file);
  42. if (!cap.isOpened())
  43. {
  44. cout << "can not open camera or video file" << endl;
  45. return EXIT_FAILURE;
  46. }
  47. UMat frame, fgmask, fgimg;
  48. cap >> frame;
  49. fgimg.create(frame.size(), frame.type());
  50. Ptr<BackgroundSubtractorKNN> knn = createBackgroundSubtractorKNN();
  51. Ptr<BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2();
  52. switch (m)
  53. {
  54. case M_KNN:
  55. knn->apply(frame, fgmask);
  56. break;
  57. case M_MOG2:
  58. mog2->apply(frame, fgmask);
  59. break;
  60. }
  61. bool running=true;
  62. for (;;)
  63. {
  64. if(!running)
  65. break;
  66. cap >> frame;
  67. if (frame.empty())
  68. break;
  69. int64 start = getTickCount();
  70. //update the model
  71. switch (m)
  72. {
  73. case M_KNN:
  74. knn->apply(frame, fgmask);
  75. break;
  76. case M_MOG2:
  77. mog2->apply(frame, fgmask);
  78. break;
  79. }
  80. double fps = getTickFrequency() / (getTickCount() - start);
  81. std::cout << "FPS : " << fps << std::endl;
  82. std::cout << fgimg.size() << std::endl;
  83. fgimg.setTo(Scalar::all(0));
  84. frame.copyTo(fgimg, fgmask);
  85. imshow("image", frame);
  86. imshow("foreground mask", fgmask);
  87. imshow("foreground image", fgimg);
  88. char key = (char)waitKey(30);
  89. switch (key)
  90. {
  91. case 27:
  92. running = false;
  93. break;
  94. case 'm':
  95. case 'M':
  96. ocl::setUseOpenCL(!ocl::useOpenCL());
  97. cout << "Switched to " << (ocl::useOpenCL() ? "OpenCL enabled" : "CPU") << " mode\n";
  98. break;
  99. }
  100. }
  101. return EXIT_SUCCESS;
  102. }