Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 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. namedWindow("bq1-1");
  56. //namedWindow("bq2-1");
  57. namedWindow("bq3-1");
  58. //namedWindow("bq4-1");
  59. float prog = 0.0001;
  60. int time = 0;
  61.  
  62. bg1->getBackgroundImage(back);
  63. // bg2->getBackgroundImage(back);
  64. bg3->getBackgroundImage(back);
  65. // bg4->getBackgroundImage(back);
  66. for (;;) {
  67. capture >> frame;
  68. imshow("Frame_damian", frame);
  69.  
  70. outputVideo.write(frame);
  71.  
  72. int erosion_size = 6;
  73. Mat element = getStructuringElement(cv::MORPH_CROSS,
  74. cv::Size(4 * erosion_size + 1, 2 * erosion_size + 1),
  75. cv::Point(erosion_size, erosion_size) );
  76. Mat erod;
  77.  
  78.  
  79. Mat fore;
  80. Mat dummy;
  81. //if((time % 2 == 0) && time)
  82. {
  83. // bg7->apply(frame, fore, -0.5);
  84. // bg7->getBackgroundImage(back); //erode(fore,fore,Mat::ones(4,4,CV_8U));
  85. //dilate(fore,fore,Mat::ones(5,5,CV_8U));
  86. // imshow("bq7,250,20,false",fore);
  87. // time = 1;
  88.  
  89. //}else{
  90. // bg7->apply(back, dummy, 1.0);
  91. // bg7->getBackgroundImage(back);
  92. // time++;
  93. }
  94.  
  95.  
  96.  
  97.  
  98. // erode(frame_bg,fore,Mat::ones(6,6,CV_8U));
  99. //dilate(fore,fore,Mat::ones(9,9,CV_8U));
  100.  
  101. // erode(frame_bg,fore,Mat::ones(5,5,CV_8U));
  102. // dilate(fore,fore,Mat::ones(4,4,CV_8U));
  103.  
  104.  
  105. float m =4 ;
  106. float n = 5;
  107. int T = 1;
  108. vector<Vec4i> hierarchy;
  109. Mat drawing = Mat::zeros( frame.size(), CV_8UC3 );
  110. Mat copy;
  111. bg1->apply(frame, frame_bg, prog);
  112. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  113. dilate(fore, frame_bg,Mat::ones(n,n,CV_8U));
  114. copy = frame_bg;
  115. imshow("bq1-1", copy);
  116. // findContours(frame_bg, contours1, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
  117. findContours( frame_bg, contours1, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
  118. for(int i = 1; i < (int)contours1.size();){
  119. cout<<contours1[i].size()<<endl;
  120. if((int)contours1[i].size()<20){
  121. contours1.erase(contours1.begin() + i);
  122. }else{
  123. i++;
  124. }
  125. }
  126. for(int i = 1; i < (int)contours1.size();i++){
  127. cout<<contours1[i].size()<<" Po"<<endl;
  128. }
  129. for( size_t i = 0; i< contours1.size(); i++ )
  130. {
  131. Scalar color = Scalar( 255, 255, 255);
  132. drawContours( drawing, contours1, (int)i, color, 2, 8, hierarchy, 0, Point() );
  133. }
  134. imshow("draw", drawing);
  135. //cout<<contours1.size()<<" A"<<endl;
  136.  
  137. Mat canny_output;
  138. vector<vector<Point> > contours;
  139. // vector<Vec4i> hierarchy;
  140. int thresh = 100;
  141. int max_thresh = 255;
  142. RNG rng(12345);
  143.  
  144.  
  145. Mat obraz;
  146. // for(int i = 0; i< contours1.size(); i++){
  147. // Scalar color(255,255,255);
  148. // drawContours(obraz,contours1, i, color, FILLED,8);
  149. // }
  150. //drawContours(obraz, contours1, -1, Scalar(0,0,255),4);
  151. //imshow("bq1", obraz);
  152. imshow("bq1", frame_bg);
  153.  
  154.  
  155. /*bg2->apply(frame, frame_bg, prog);
  156. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  157. dilate(fore,frame_bg,Mat::ones(n,n,CV_8U));
  158. copy = frame_bg;
  159. imshow("bq2-2", copy);
  160. findContours(frame_bg, contours2, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
  161. cout<<contours2.size()<<" B"<<endl;
  162.  
  163. imshow("bq2", frame_bg);*/
  164.  
  165. bg3->apply(frame, frame_bg, prog);
  166. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  167. dilate(fore,frame_bg,Mat::ones(n,n,CV_8U));
  168. copy = frame_bg;
  169. imshow("bq3-2", copy);
  170. findContours(frame_bg, contours3, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
  171. // cout<<contours3.size()<<" C"<<endl;
  172. imshow("bq3", frame_bg);
  173.  
  174. /*
  175. bg4->apply(frame, frame_bg, prog);
  176. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  177. dilate(fore,frame_bg,Mat::ones(n,n,CV_8U));
  178. copy = frame_bg;
  179. imshow("bq4-2", copy);
  180. findContours(frame_bg, contours4, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
  181. cout<<contours4.size()<<" D"<<endl;
  182. imshow("bq4", frame_bg);
  183. */
  184.  
  185. drawContours(frame, contours1, -1, Scalar(0,0,255),2);
  186. char key = (char)cv::waitKey(20);
  187. switch (key) {
  188. case 'w':
  189. imwrite("background.jpg", frame);
  190. break;
  191. case 27:
  192. case 'q':
  193. return 0;
  194. break;
  195. }
  196.  
  197. }
  198.  
  199. return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement