Guest User

Untitled

a guest
Dec 14th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cv.h>
  3. #include "opencv2/core/core.hpp"
  4. #include "opencv2/imgproc/imgproc.hpp"
  5. #include "opencv2/video/background_segm.hpp"
  6. #include "opencv2/highgui/highgui.hpp"
  7.  
  8.  
  9. using namespace cv;
  10. using namespace std;
  11.  
  12. int main (int argc, char** argv) {
  13. // Read in our stuff
  14. if (argc != 3) {
  15. cout << "Usage: ./Test input.avi output.avi" << endl;
  16. exit(4);
  17. }
  18.  
  19. // Attempt to read in the input file
  20. VideoCapture inVid;
  21. if (!inVid.open(argv[1])) {
  22. cout << "Failed to open the input video" << endl;
  23. exit(5);
  24. }
  25.  
  26. VideoWriter outVid;
  27. if (!outVid.open(argv[2], inVid.get(CV_CAP_PROP_FOURCC), inVid.get(CV_CAP_PROP_FPS), Size2f(inVid.get(CV_CAP_PROP_FRAME_WIDTH), inVid.get(CV_CAP_PROP_FRAME_HEIGHT)))) {
  28. cout << "Failed top open the output video" << endl;
  29. exit(6);
  30. }
  31.  
  32. // Lets create our basic "background". Unfortunatly, we have to assume our first image is "clean" in this regard (dunno how to fix... *shrug*)
  33. BackgroundSubtractorMOG2 mog;
  34. Mat img, fgmask;
  35. vector<vector<Point> > v;
  36. int idx, area = 0;
  37.  
  38. while (inVid.read(img)) {
  39. // We have a frame to process
  40. // Update our mog
  41. mog(img, fgmask);
  42.  
  43. // Find the contours of the fgmask
  44. findContours(fgmask, v, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
  45.  
  46. for (int i = 0; i < v.size(); i++) {
  47. if (area < v[i].size()) {
  48. idx = i;
  49. }
  50. }
  51.  
  52. // Calculate and draw our bonding box
  53. Rect rect = boundingRect(v[idx]);
  54. Point pt1, pt2;
  55. pt1.x = rect.x;
  56. pt1.y = rect.y;
  57. pt2.x = rect.x + rect.width;
  58. pt2.y = rect.y + rect.height;
  59.  
  60. rectangle(img, pt1, pt2, CV_RGB(255,0,0), 1);
  61.  
  62. outVid.write(img);
  63. }
  64. mog(img, fgmask, 0);
  65.  
  66. return 0;
  67. }
Add Comment
Please, Sign In to add comment