bgfg_segm.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <iostream>
  2. #include <string>
  3. #include "opencv2/core.hpp"
  4. #include "opencv2/core/utility.hpp"
  5. #include "opencv2/cudabgsegm.hpp"
  6. #include "opencv2/video.hpp"
  7. #include "opencv2/highgui.hpp"
  8. using namespace std;
  9. using namespace cv;
  10. using namespace cv::cuda;
  11. enum Method
  12. {
  13. MOG,
  14. MOG2,
  15. };
  16. int main(int argc, const char** argv)
  17. {
  18. cv::CommandLineParser cmd(argc, argv,
  19. "{ c camera | | use camera }"
  20. "{ f file | ../data/vtest.avi | input video file }"
  21. "{ m method | mog | method (mog, mog2) }"
  22. "{ h help | | print help message }");
  23. if (cmd.has("help") || !cmd.check())
  24. {
  25. cmd.printMessage();
  26. cmd.printErrors();
  27. return 0;
  28. }
  29. bool useCamera = cmd.has("camera");
  30. string file = cmd.get<string>("file");
  31. string method = cmd.get<string>("method");
  32. if (method != "mog"
  33. && method != "mog2")
  34. {
  35. cerr << "Incorrect method" << endl;
  36. return -1;
  37. }
  38. Method m = method == "mog" ? MOG :
  39. method == "mog2" ? MOG2 :
  40. (Method)-1;
  41. CV_Assert(m != (Method)-1);
  42. VideoCapture cap;
  43. if (useCamera)
  44. cap.open(0);
  45. else
  46. cap.open(file);
  47. if (!cap.isOpened())
  48. {
  49. cerr << "can not open camera or video file" << endl;
  50. return -1;
  51. }
  52. Mat frame;
  53. cap >> frame;
  54. GpuMat d_frame(frame);
  55. Ptr<BackgroundSubtractor> mog = cuda::createBackgroundSubtractorMOG();
  56. Ptr<BackgroundSubtractor> mog2 = cuda::createBackgroundSubtractorMOG2();
  57. GpuMat d_fgmask;
  58. GpuMat d_fgimg;
  59. GpuMat d_bgimg;
  60. Mat fgmask;
  61. Mat fgimg;
  62. Mat bgimg;
  63. switch (m)
  64. {
  65. case MOG:
  66. mog->apply(d_frame, d_fgmask, 0.01);
  67. break;
  68. case MOG2:
  69. mog2->apply(d_frame, d_fgmask);
  70. break;
  71. }
  72. namedWindow("image", WINDOW_NORMAL);
  73. namedWindow("foreground mask", WINDOW_NORMAL);
  74. namedWindow("foreground image", WINDOW_NORMAL);
  75. namedWindow("mean background image", WINDOW_NORMAL);
  76. for(;;)
  77. {
  78. cap >> frame;
  79. if (frame.empty())
  80. break;
  81. d_frame.upload(frame);
  82. int64 start = cv::getTickCount();
  83. //update the model
  84. switch (m)
  85. {
  86. case MOG:
  87. mog->apply(d_frame, d_fgmask, 0.01);
  88. mog->getBackgroundImage(d_bgimg);
  89. break;
  90. case MOG2:
  91. mog2->apply(d_frame, d_fgmask);
  92. mog2->getBackgroundImage(d_bgimg);
  93. break;
  94. }
  95. double fps = cv::getTickFrequency() / (cv::getTickCount() - start);
  96. std::cout << "FPS : " << fps << std::endl;
  97. d_fgimg.create(d_frame.size(), d_frame.type());
  98. d_fgimg.setTo(Scalar::all(0));
  99. d_frame.copyTo(d_fgimg, d_fgmask);
  100. d_fgmask.download(fgmask);
  101. d_fgimg.download(fgimg);
  102. if (!d_bgimg.empty())
  103. d_bgimg.download(bgimg);
  104. imshow("image", frame);
  105. imshow("foreground mask", fgmask);
  106. imshow("foreground image", fgimg);
  107. if (!bgimg.empty())
  108. imshow("mean background image", bgimg);
  109. char key = (char)waitKey(30);
  110. if (key == 27)
  111. break;
  112. }
  113. return 0;
  114. }