Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 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> bg3 = cv::createBackgroundSubtractorMOG2(500,20,false);
  35.  
  36. vector<vector<Point> > contours1;
  37. vector<vector<Point> > contours3;
  38.  
  39. cv::Size S = cv::Size((int) capture.get(CV_CAP_PROP_FRAME_WIDTH),
  40. (int) capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  41. outputVideo.open("output.mpg", CV_FOURCC('F','M','P','4'), capture.get(CV_CAP_PROP_FPS), S, true);
  42. namedWindow("Frame_damian");
  43.  
  44.  
  45. namedWindow("bq1");
  46. namedWindow("bq3");
  47. namedWindow("bq1-1");
  48. namedWindow("bq3-1");
  49. float prog = 0.0001;
  50. int time = 0;
  51.  
  52. bg1->getBackgroundImage(back);
  53. bg3->getBackgroundImage(back);
  54. for (;;) {
  55. capture >> frame;
  56. imshow("Frame_damian", frame);
  57. Mat frame2 = frame;
  58. outputVideo.write(frame);
  59.  
  60. int erosion_size = 6;
  61. Mat element = getStructuringElement(cv::MORPH_CROSS,
  62. cv::Size(4 * erosion_size + 1, 2 * erosion_size + 1),
  63. cv::Point(erosion_size, erosion_size) );
  64. Mat erod;
  65.  
  66.  
  67. Mat fore;
  68. Mat dummy;
  69.  
  70. float m =4 ;
  71. float n = 7;
  72. int T = 1;
  73. vector<Vec4i> hierarchy;
  74. Mat drawing = Mat::zeros( frame.size(), CV_8UC3 );
  75. Mat copy;
  76. bg1->apply(frame, frame_bg, prog);
  77. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  78. dilate(fore, frame_bg,Mat::ones(n,n,CV_8U));
  79. copy = frame_bg;
  80. imshow("bq1-1", copy);
  81. // findContours(frame_bg, contours1, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
  82. findContours( frame_bg, contours1, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0) );
  83. for(int i = 1; i < (int)contours1.size();){
  84. cout<<contours1[i].size()<<endl;
  85. if((int)contours1[i].size()<60){
  86. contours1.erase(contours1.begin() + i);
  87. }else{
  88. i++;
  89. }
  90. }
  91. // for(int i = 1; i < (int)contours1.size();i++){
  92. // cout<<contours1[i].size()<<" Po"<<endl;
  93. //}
  94. for( size_t i = 0; i< contours1.size(); i++ )
  95. {
  96. Scalar color = Scalar( 255, 255, 255);
  97. drawContours( drawing, contours1, (int)i, color, 2, 8, hierarchy, 0, Point() );
  98. }
  99. imshow("draw", drawing);
  100. vector<Point> > wektorŚrodka =
  101. //cout<<contours1.size()<<" A"<<endl;
  102. for( size_t i = 0; i< contours1.size(); i++ ){
  103. int minx= 100000, miny= 100000, maxx= 0, maxy = 0;
  104. for( size_t j = 0; j< contours1[i].size(); j++ ){
  105.  
  106. if(contours1[i][j].x > maxx){
  107. // cout<<"ello";
  108. maxx = contours1[i][j].x;
  109. }
  110. if(contours1[i][j].x < minx){
  111. // cout<<"ello";
  112. minx = contours1[i][j].x;
  113. }
  114. if(contours1[i][j].y > maxy){
  115. // cout<<"ello";
  116. maxy = contours1[i][j].y;
  117. }
  118. if(contours1[i][j].y < miny){
  119. // cout<<"ello";
  120. miny = contours1[i][j].y;
  121. }
  122. }
  123. Scalar red = Scalar( 0, 0,255);
  124. Point pointTopLeft = Point(minx, maxy);
  125. Point pointBottonRight = Point(maxx, miny);
  126. cout << "Punkt " << i << "(" << minx << ","<< miny<<endl;
  127. rectangle(frame2, pointTopLeft, pointBottonRight, red, 4);
  128. imshow("frame2", frame2);
  129. }
  130.  
  131.  
  132.  
  133. Mat canny_output;
  134. vector<vector<Point> > contours;
  135. // vector<Vec4i> hierarchy;
  136. int thresh = 100;
  137. int max_thresh = 255;
  138. RNG rng(12345);
  139.  
  140.  
  141. Mat obraz;
  142. // for(int i = 0; i< contours1.size(); i++){
  143. // Scalar color(255,255,255);
  144. // drawContours(obraz,contours1, i, color, FILLED,8);
  145. // }
  146. //drawContours(obraz, contours1, -1, Scalar(0,0,255),4);
  147. //imshow("bq1", obraz);
  148. imshow("bq1", frame_bg);
  149.  
  150.  
  151.  
  152. bg3->apply(frame, frame_bg, prog);
  153. erode(frame_bg,fore,Mat::ones(m,m,CV_8U));
  154. dilate(fore,frame_bg,Mat::ones(n,n,CV_8U));
  155. copy = frame_bg;
  156. imshow("bq3-2", copy);
  157. findContours(frame_bg, contours3, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
  158. // cout<<contours3.size()<<" C"<<endl;
  159. imshow("bq3", frame_bg);
  160.  
  161.  
  162.  
  163. drawContours(frame, contours1, -1, Scalar(0,0,255),2);
  164. char key = (char)cv::waitKey(20);
  165. switch (key) {
  166. case 'w':
  167. imwrite("background.jpg", frame);
  168. break;
  169. case 27:
  170. case 'q':
  171. return 0;
  172. break;
  173. }
  174.  
  175. }
  176.  
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement