Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #include "opencv2/opencv.hpp"
  2. #include <iostream>
  3. #include <vector>
  4.  
  5.  
  6. using namespace cv;
  7. using namespace std;
  8. int main(int ac, char ** av)
  9. {
  10. cv::Mat frame;
  11. Mat back;
  12. Mat frame_bg;
  13. cv::VideoCapture capture(0);
  14. cv::VideoWriter outputVideo;
  15.  
  16. if (ac < 1)
  17. return 1;
  18.  
  19. if (ac == 3){
  20. back = imread(av[2], 1);
  21. //namedWindow("lala");
  22. //imshow("lala", back);
  23.  
  24. }
  25. capture.open(av[1]);
  26. if (!capture.isOpened())
  27. {
  28. std::cout << "capture device or file " << av[1] << " failed to open!" << std::endl;
  29. return 1;
  30. }
  31.  
  32.  
  33. cv::Ptr<cv::BackgroundSubtractor> bg1 = cv::createBackgroundSubtractorMOG2(100,20,false);
  34. cv::Ptr<cv::BackgroundSubtractor> bg2 = cv::createBackgroundSubtractorMOG2(200,20,true);
  35. cv::Ptr<cv::BackgroundSubtractor> bg3 = cv::createBackgroundSubtractorMOG2(500,20,false);
  36. cv::Ptr<cv::BackgroundSubtractor> bg4 = cv::createBackgroundSubtractorMOG2(250,16,true);
  37.  
  38. vector<vector<Point>> contours1;
  39. vector<vector<Point>> contours2;
  40. vector<vector<Point>> contours3;
  41. vector<vector<Point>> contours4;
  42.  
  43. cv::Size S = cv::Size((int) capture.get(CV_CAP_PROP_FRAME_WIDTH),
  44. (int) capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  45. //int ex = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC));
  46. //outputVideo.open("output.mpg", ex, capture.get(CV_CAP_PROP_FPS), S, true);
  47. outputVideo.open("output.mpg", CV_FOURCC('F','M','P','4'), capture.get(CV_CAP_PROP_FPS), S, true);
  48. namedWindow("Frame_damian");
  49.  
  50.  
  51. namedWindow("bq1");
  52. namedWindow("bq2");
  53. namedWindow("bq3");
  54. namedWindow("bq4");
  55. float prog = 0.0001;
  56. int time = 0;
  57.  
  58. bg1->getBackgroundImage(back);
  59. bg2->getBackgroundImage(back);
  60. bg3->getBackgroundImage(back);
  61. bg4->getBackgroundImage(back);
  62. for (;;) {
  63. capture >> frame;
  64. imshow("Frame_damian", frame);
  65.  
  66. outputVideo.write(frame);
  67.  
  68. int erosion_size = 6;
  69. Mat element = getStructuringElement(cv::MORPH_CROSS,
  70. cv::Size(4 * erosion_size + 1, 2 * erosion_size + 1),
  71. cv::Point(erosion_size, erosion_size) );
  72. Mat erod;
  73.  
  74.  
  75. Mat fore;
  76. Mat dummy;
  77. //if((time % 2 == 0) && time)
  78. {
  79. // bg7->apply(frame, fore, -0.5);
  80. // bg7->getBackgroundImage(back); //erode(fore,fore,Mat::ones(4,4,CV_8U));
  81. //dilate(fore,fore,Mat::ones(5,5,CV_8U));
  82. // imshow("bq7,250,20,false",fore);
  83. // time = 1;
  84.  
  85. //}else{
  86. // bg7->apply(back, dummy, 1.0);
  87. // bg7->getBackgroundImage(back);
  88. // time++;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. // erode(frame_bg,fore,Mat::ones(6,6,CV_8U));
  95. //dilate(fore,fore,Mat::ones(9,9,CV_8U));
  96.  
  97. // erode(frame_bg,fore,Mat::ones(5,5,CV_8U));
  98. // dilate(fore,fore,Mat::ones(4,4,CV_8U));
  99.  
  100.  
  101. float m = 3;
  102. float n = 3;
  103. int T = 1;
  104.  
  105.  
  106. bg1->apply(frame, frame_bg, prog);
  107. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  108. dilate(fore, frame_bg,Mat::ones(n,n,CV_8U));
  109. findContours(frame_bg, contours1, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
  110. cout<<contours1.size()<<" A"<<endl;
  111.  
  112. for(int i = 1; i < (int)contours1.size();){
  113. if((int)contours1[i].size()<T){
  114. contours1.erase(contours1.begin()+1);
  115. }else{
  116. i++;
  117. }
  118. }
  119. Mat obraz;
  120. for(int i = 0; i< contours1.size(); i++){
  121. Scalar color(255,255,255);
  122. drawContours(obraz,contours1, i, color, FILLED,8);
  123. }
  124. drawContours(obraz, contours1, -1, Scalar(0,0,255),4);
  125. //imshow("bq1", obraz);
  126. //imshow("bq1", frame_bg);
  127.  
  128. bg2->apply(frame, frame_bg, prog);
  129. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  130. dilate(fore,frame_bg,Mat::ones(n,n,CV_8U));
  131. findContours(frame_bg, contours2, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
  132. cout<<contours2.size()<<" B"<<endl;
  133. imshow("bq2", frame_bg);
  134.  
  135. bg3->apply(frame, frame_bg, prog);
  136. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  137. dilate(fore,frame_bg,Mat::ones(n,n,CV_8U));
  138. findContours(frame_bg, contours3, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
  139. cout<<contours3.size()<<" C"<<endl;
  140.  
  141. imshow("bq3", frame_bg);
  142.  
  143. bg4->apply(frame, frame_bg, prog);
  144. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  145. dilate(fore,frame_bg,Mat::ones(n,n,CV_8U));
  146. findContours(frame_bg, contours4, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
  147. cout<<contours4.size()<<" D"<<endl;
  148. imshow("bq4", frame_bg);
  149.  
  150.  
  151. drawContours(frame, contours1, -1, Scalar(0,0,255),2);
  152. char key = (char)cv::waitKey(20);
  153. switch (key) {
  154. case 'w':
  155. imwrite("background.jpg", frame);
  156. break;
  157. case 27:
  158. case 'q':
  159. return 0;
  160. break;
  161. }
  162.  
  163. }
  164.  
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement