background_subtr_bgslib.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <vector>
  5. #include <opencv2/videoio.hpp>
  6. #include "algorithms/algorithms.h"
  7. using namespace cv;
  8. using namespace std;
  9. const char* input_params = "{ input | space_traffic.mp4 | Define the full input video path }";
  10. void get_bgslib_result(String video_to_process)
  11. {
  12. // create VideoCapture object for further video processing
  13. VideoCapture capture(samples::findFile(video_to_process));
  14. if (!capture.isOpened()) {
  15. //error in opening the video input
  16. cerr << "Unable to open: " << video_to_process << endl;
  17. return;
  18. }
  19. // instantiate background subtraction model
  20. SuBSENSE background_subtr_method = SuBSENSE();
  21. Mat frame, fgMask, background;
  22. while (true) {
  23. capture >> frame;
  24. // check whether the frames have been grabbed
  25. if (frame.empty())
  26. break;
  27. // resize video frames
  28. resize(frame, frame, Size(640, 360));
  29. try
  30. {
  31. // pass the frame to the model processor
  32. background_subtr_method.process(frame, fgMask, background);
  33. }
  34. catch (exception& e)
  35. {
  36. cout << "Exception occurred" << endl;
  37. cout << e.what() << endl;
  38. }
  39. // show the current frame, foreground mask, subtracted result
  40. imshow("Initial Frames", frame);
  41. imshow("Foreground Masks", fgMask);
  42. imshow("Subtraction Result", background);
  43. int keyboard = waitKey(10);
  44. if (keyboard == 27)
  45. break;
  46. }
  47. }
  48. int main(int argc, char* argv[])
  49. {
  50. CommandLineParser parser(argc, argv, input_params);
  51. // start BS-pipeline
  52. get_bgslib_result(parser.get<String>("input"));
  53. return 0;
  54. }