Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. #include "opencv2\highgui\highgui.hpp"
  2. #include "opencv2\imgproc\imgproc.hpp"
  3. #include "opencv2\\video\\tracking.hpp"
  4. #include "opencv2\opencv.hpp"
  5. #include <iostream>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9. using namespace cv;
  10.  
  11. // Chú ý: Khi chạy video 1 thì sử dụng mức thres cho từng video. Nếu chạy video 1 thì đóng (//thres=197), nếu chạy video 2 thì đóng (//thres=90)
  12. //Thresh hold
  13. //int thres = 90; //Mức thresh cho video 1
  14. int thres = 180; //Mức thresh cho video 2
  15. int maxval = 255;
  16.  
  17. //Làm mờ
  18. int kernel_size = 9;
  19.  
  20. //Canny Edge
  21. int low_threshold = 20;
  22. int high_threshold = 40;
  23.  
  24. //Hough line
  25. int rho = 1;// Khoảng cách RHO
  26. double theta = CV_PI / 180;// Góc theta
  27. int houghVote = 30; //Ngưỡng
  28. int max_line_gap = 30; //Chiều dài lớn nhất
  29. int min_line_gap = 10;//Chiều dài nhỏ nhất của hough
  30.  
  31. //Vẽ line
  32. Scalar color = Scalar(0, 255, 0);//Màu
  33. int thickness = 3;//Độ dày
  34.  
  35. int prev_left_x;//Chứa toa do diem trươc đó ben trái
  36.  
  37. int prev_right_x;
  38. //X_center xét từ dưới lên
  39.  
  40. template <typename T>
  41. std::string NumberToString(T Number)
  42. {
  43. std::ostringstream ss;
  44. ss << Number;
  45. return ss.str();
  46. }
  47. Mat XuLy(Mat img)
  48. {
  49. Mat out;
  50. if (img.channels() == 3)
  51. cvtColor(img, out, CV_BGR2GRAY);
  52. GaussianBlur(out, out, Size(kernel_size, kernel_size), 15, 2);
  53. threshold(out, out, thres, maxval, CV_THRESH_BINARY);
  54. //medianBlur(out, out, 15);
  55.  
  56. Canny(out, out, low_threshold, high_threshold, 3, false);
  57.  
  58. return out;
  59. }
  60.  
  61. Mat Cat_Hinh_Anh(Mat &src)
  62. {
  63. return src(Rect(0, roundf(src.size().height * 3 / 4), src.size().width, roundf(src.size().height / 4)));
  64. }
  65. Point Tim_Tam(vector<Point> left, vector<Point> right)
  66. {
  67. if (left.size() == 0 || right.size() == 0)
  68. return NULL;
  69. int sum_left_x = 0;
  70. int sum_left_y = 0;
  71. int count_left = 0;
  72.  
  73. int sum_right_x = 0;
  74. int sum_right_y = 0;
  75. int count_right = 0;
  76.  
  77. for (int i = 0; i < left.size(); i++)
  78. {
  79. sum_left_x += left[i].x;
  80. sum_left_y += left[i].y;
  81. count_left++;
  82. }
  83.  
  84. for (int i = 0; i < right.size(); i++)
  85. {
  86. sum_right_x += right[i].x;
  87. sum_right_y += right[i].y;
  88. count_right++;
  89. }
  90.  
  91. double mid_left_x = sum_left_x / count_left;
  92. double mid_left_y = sum_left_y / count_left;
  93.  
  94. double mid_right_x = sum_right_x / count_right;
  95. double mid_right_y = sum_right_y / count_right;
  96.  
  97. return Point(roundf((mid_left_x + mid_right_x) / 2), roundf((mid_left_y + mid_right_y) / 2));
  98.  
  99. }
  100.  
  101. Point Process(Mat img, Mat src)
  102. {
  103. vector<Vec4i> lines;
  104.  
  105. }
  106. int main()
  107. {
  108. VideoCapture capture("C:/Users/ADMIN/Desktop/test.avi");
  109. int t = capture.get(CV_CAP_PROP_FRAME_COUNT);
  110. int i = 0;
  111.  
  112. int width = 0, height = 0, fps = 0;
  113. width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
  114. height = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  115. //fps = static_cast<int>(capture.get(CV_CAP_PROP_FPS));
  116.  
  117. //cout << capture.get(CV_CAP_PROP_FOURCC) << endl;
  118. //cout << "Input video: (" << width << "x" << height << ") at " << fps << " fps" << endl;
  119.  
  120. cout << t << endl;
  121. /*ofstream outfile;
  122. outfile.open("C:/Users/ADMIN/Desktop/Tam_OUT_LaiXuanThoi.txt");*/
  123. //outfile << t << endl;
  124. int repeat=0;
  125. //VideoWriter ghi("C:/Users/ADMIN/Desktop/Video_OUT_LaiXuanThoi.avi", capture.get(CV_CAP_PROP_FOURCC), capture.get(CV_CAP_PROP_FPS), Size(width, height), true);
  126. while (i<t )
  127. {
  128. i++;
  129. Mat src;
  130. if (i == t - 1)
  131. {
  132. if (repeat == 2) break;
  133. else repeat++;
  134. i = 0;
  135. capture.set(CV_CAP_PROP_POS_FRAMES, 0);
  136. }
  137. capture.read(src);
  138.  
  139. Mat cut = Cat_Hinh_Anh(src);
  140. cut = XuLy(cut);
  141. //cout << i << " ";
  142. Point tam = Process(cut, src);
  143.  
  144. cout << i << " ";
  145. cout << " " << tam.x << " " << tam.y << endl;
  146. //Ghi ra man hin
  147. putText(src, NumberToString(tam.x) + "," + NumberToString(tam.y), Point(150, 290), 1, 2, Scalar(255, 255, 0), 2);
  148. //outfile << i << " " << tam.x << " " << tam.y << endl;
  149. imshow("Source", src);
  150. imshow("Cut image", cut);
  151. //ghi.write(src);
  152. if (waitKey(30)==27)
  153. waitKey(0);
  154. }
  155. //ghi.release();
  156. //destroyAllWindows();
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement