Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <string>
  5.  
  6. #include "opencv2/highgui/highgui.hpp"
  7. #include "opencv2/video/background_segm.hpp"
  8. #include "opencv2\opencv.hpp"
  9. #include "opencv2/core/core.hpp"
  10.  
  11. using namespace cv;
  12. using namespace std;
  13.  
  14. int main(int argc, char** argv)
  15. {
  16. Mat frame;
  17. Mat gauss_frame;
  18. Mat fgMaskMOG2;
  19. Mat binary_frame;
  20. Mat fg_blur;
  21. Mat contour_frame;
  22. bool fine = false;
  23. bool pausa = false;
  24.  
  25.  
  26. float proporzioni = 80;
  27. int MIN_WIDTH = 25*proporzioni/100;
  28. int MIN_HEIGHT = 25*proporzioni/100;
  29. float MIN_DIST = 70*proporzioni/100;
  30.  
  31. int dim_gauss = (proporzioni/100)*5;
  32. if (dim_gauss%2==0)
  33. dim_gauss = dim_gauss-1;
  34.  
  35. int dim_blur = (proporzioni/100)*40;
  36.  
  37. int ingressi = 0;
  38. int uscite = 0;
  39.  
  40. bool prima_frame_con_persone = 1;
  41.  
  42. Scalar colore_ing, colore_usc;
  43.  
  44. vector <Point> pt_frame_prec;
  45. vector <Point> pt_frame_now;
  46.  
  47. Ptr<BackgroundSubtractorMOG2> pMOG2;
  48. pMOG2 = new BackgroundSubtractorMOG2(300, 0, true); //history=300frame, soglia:0, shadow_detection = true
  49.  
  50. VideoCapture capture(argv[1]);
  51.  
  52.  
  53. while(!fine)
  54. {
  55. if (!capture.read(frame))
  56. {
  57. cout<<"Fine Video"<<endl;
  58. fine = true;
  59. }
  60. else
  61. {
  62.  
  63. resize(frame, frame, Size(frame.size().width*proporzioni/100, frame.size().height*proporzioni/100) );
  64.  
  65. GaussianBlur(frame, gauss_frame, Size(dim_gauss, dim_gauss), 1, 1);
  66.  
  67. pMOG2->operator()(gauss_frame, fgMaskMOG2, 0.01);
  68.  
  69. blur(fgMaskMOG2, fg_blur, Size(dim_blur, dim_blur));
  70.  
  71. threshold(fg_blur, binary_frame, 150, 255, CV_THRESH_BINARY);
  72.  
  73. contour_frame = binary_frame.clone();
  74.  
  75. vector< vector< Point> > contours;
  76.  
  77. findContours(contour_frame, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  78. //CV_RETR_EXTERNAL restituisce i contorni esterni
  79. //CV_CHAIN_APPROX_NONE: tutti i pixel del contorno
  80.  
  81. vector< vector< Point> >::iterator itc= contours.begin();
  82.  
  83. vector< Point > coordinate;
  84. Point centro;
  85.  
  86. while (itc!=contours.end())
  87. {
  88. Rect temp1 = boundingRect(Mat(*itc));
  89.  
  90. if(temp1.height>MIN_HEIGHT && temp1.width>MIN_WIDTH)
  91. {
  92. centro.x = temp1.x + temp1.width/2;
  93. centro.y = temp1.y + temp1.height/2;
  94.  
  95. coordinate.push_back(centro);
  96.  
  97. rectangle(frame, temp1, Scalar(0,0,255));
  98. circle(frame, centro, 2, Scalar( 0, 0, 255 ), 5, 8, 0);
  99. }
  100.  
  101. ++itc;
  102. }
  103.  
  104.  
  105.  
  106. int confine = frame.size().height/2;
  107.  
  108. colore_usc = colore_ing = Scalar(0,0,255);
  109.  
  110. if(prima_frame_con_persone)
  111. {
  112. pt_frame_prec = coordinate;
  113. prima_frame_con_persone = 0;
  114. }
  115. else
  116. {
  117. pt_frame_now = coordinate;
  118.  
  119. vector< Point >::iterator itp1, itp2;
  120. itp1 = pt_frame_now.begin();
  121.  
  122.  
  123. while(itp1!=pt_frame_now.end())
  124. {
  125. itp2 = pt_frame_prec.begin();
  126. Point a = *itp1;
  127. cout<<"("<<a.x<<", "<<a.y<<")\t";
  128. while(itp2!=pt_frame_prec.end())
  129. {
  130. Point b = *itp2;
  131. float distanza = sqrt( pow((float)(a.x-b.x), 2) + pow((float)(a.y - b.y),2) );
  132. cout<<distanza<<"\t";
  133. if(distanza < MIN_DIST )
  134. {
  135. line(frame, a, b, Scalar( 0, 255, 0 ), 2, 8, 0);
  136.  
  137. if(a.y > confine && b.y <= confine)
  138. {
  139. uscite++;
  140. line(frame, Point(0, confine), Point (frame.size().width, confine-5), Scalar( 255, 255, 0 ), 2, 8, 0);
  141. colore_usc=Scalar(255,255,0);
  142.  
  143. }
  144.  
  145. if(a.y <= confine && b.y > confine)
  146. {
  147. ingressi++;
  148. line(frame, Point(0, confine), Point (frame.size().width, confine-5), Scalar( 255, 255, 0 ), 2, 8, 0);
  149. colore_ing=Scalar(255,255,0);
  150. }
  151. }
  152.  
  153. ++itp2;
  154. }
  155. cout<<endl;
  156.  
  157. ++itp1;
  158. }
  159. pt_frame_prec = pt_frame_now;
  160. }
  161.  
  162. stringstream stringaingressi;
  163. stringstream stringauscite;
  164. stringaingressi << "Ingressi "<<ingressi;
  165. stringauscite << "Uscite "<<uscite;
  166.  
  167. if(!pt_frame_now.empty())
  168. cout<<endl<<stringaingressi.str()<<"\t"<<stringauscite.str()<<endl<<endl;
  169.  
  170. putText(frame, stringauscite.str(), Point(2, confine-8), FONT_HERSHEY_SIMPLEX, 0.55, colore_usc, 2, 8, 0);
  171. putText(frame, stringaingressi.str(), Point(2, confine+18), FONT_HERSHEY_SIMPLEX, 0.55, colore_ing, 2, 8, 0);
  172.  
  173. line(frame, Point(0, confine), Point (frame.size().width, confine), Scalar( 0, 255, 0 ), 2, 8, 0);
  174.  
  175.  
  176. imshow ("gaussian", gauss_frame);
  177. imshow("mog", fgMaskMOG2);
  178. imshow("fg_blur", fg_blur);
  179. imshow("binaria", binary_frame);
  180. imshow("contorni", contour_frame);
  181. imshow ("prova", frame);
  182.  
  183.  
  184. switch(waitKey(10))
  185. {
  186. case 27: //'esc'
  187. fine = true;
  188. break;
  189. case 112: //'p' pausa/riprendi
  190. pausa = !pausa;
  191. if(pausa == true)
  192. cout<<"Code paused, press 'p' again to resume"<<endl;
  193. while (pausa == true)
  194. {
  195. if(waitKey()==112)
  196. {
  197. pausa = false;
  198. cout<<"Code Resumed"<<endl;
  199. }
  200. }
  201. break;
  202. }
  203. }
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement