Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2.  
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include "opencv2/imgproc/imgproc.hpp"
  5. #include <cmath>
  6. #include <ctime>
  7. #include <boost/date_time/posix_time/posix_time_types.hpp>
  8.  
  9.  
  10. using namespace cv;
  11. using namespace std;
  12.  
  13. Point PointOnCurve(Point p0, Point p1, Point p2, Point p3, float t)
  14. {
  15. Point ret;
  16.  
  17. float t2 = t * t;
  18. float t3 = t2 * t;
  19.  
  20. ret.x = 0.5f * ((2.0f * p1.x) +
  21. (-p0.x + p2.x) * t +
  22. (2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x) * t2 +
  23. (-p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x) * t3);
  24.  
  25. ret.y = 0.5f * ((2.0f * p1.y) +
  26. (-p0.y + p2.y) * t +
  27. (2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y) * t2 +
  28. (-p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y) * t3);
  29.  
  30. return ret;
  31. }
  32.  
  33. int main(int argc, char* argv[])
  34. {
  35. double real_radius = 0.06;
  36. VideoCapture cap(0);
  37. int x = 0;
  38. Point last_graph_point;
  39. boost::posix_time::ptime t1 = boost::posix_time::microsec_clock::local_time();
  40.  
  41. vector<Point> points;
  42. namedWindow("interpolate",CV_WINDOW_AUTOSIZE);
  43. Mat graph = Mat::zeros(500,500, CV_8U);;
  44.  
  45. while (1)
  46. {
  47. Mat frame;
  48. Mat result;
  49. Mat channels[3];
  50.  
  51. bool bSuccess = cap.read(frame);
  52. flip(frame,frame, 1);
  53.  
  54. Mat gray;
  55. cvtColor(frame, gray, COLOR_BGR2HSV);
  56. vector<Vec3f> circles;
  57.  
  58. inRange(gray, Scalar(90,100,50),Scalar(130,255,255),gray);
  59. GaussianBlur( gray, gray, Size(11, 11), 500);
  60.  
  61. HoughCircles( gray, circles, CV_HOUGH_GRADIENT, 1, 1000, 100, 20, 10);
  62. double radius;
  63. double time_passed;
  64. if(circles.size())
  65. {
  66. radius = circles[0][2];
  67. points.push_back(Point(circles[0][0], circles[0][1]));
  68. circle(frame, Point(circles[0][0], circles[0][1]), radius, Scalar(0,0,255),5);
  69. boost::posix_time::ptime t2 = boost::posix_time::microsec_clock::local_time();
  70. boost::posix_time::time_duration diff = t2 - t1;
  71. t1 = boost::posix_time::microsec_clock::local_time();
  72. time_passed = diff.total_milliseconds() * 0.001;
  73. }
  74.  
  75. if (points.size() > 3)
  76. {
  77. vector<Point> points_interpolated;
  78. for (int i = 0;i < points.size() - 3; i++)
  79. {
  80. for (int j = 0; j < 20; j++)
  81. {
  82. points_interpolated.push_back(PointOnCurve(points[i], points[i + 1], points[i + 2], points[i + 3], (1.0 / 50) * j));
  83. }
  84. }
  85.  
  86. for (int i = 0;i < points_interpolated.size() - 1; i++)
  87. {
  88. line(frame, points_interpolated[i], points_interpolated[i + 1], Scalar(255,0,0),2);
  89. }
  90.  
  91. double distance = 0;
  92. double xsq = pow(points[points.size() - 2].x - points[points.size() - 1].x, 2);
  93. double ysq = pow(points[points.size() - 2].y - points[points.size() - 1].y, 2);
  94. distance = sqrt(xsq + ysq);
  95.  
  96.  
  97. if (circles.size())
  98. {
  99. double ratio = real_radius / radius;
  100. double vel = distance * ratio / time_passed;
  101. String s =format("%.2f m/s",vel);
  102. line(graph, last_graph_point,Point(x, vel * 70), Scalar(255,0,0),1);
  103. last_graph_point = Point(x,vel * 70);
  104. putText(frame, s, points[points.size() - 1], FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255,255), 2);
  105. }
  106. }
  107.  
  108. x+=5;
  109. if (x > 500)
  110. {
  111. graph = Mat::zeros(500,500, CV_8U);;
  112. x = 0;
  113. last_graph_point = Point(0,0);
  114. }
  115.  
  116. imshow("graph",graph);
  117. imshow("interpolate",frame);
  118.  
  119. waitKey(1);
  120. }
  121. return 0;
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement