Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <vector>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. void dostuff();
  9.  
  10. int main()
  11. {
  12. dostuff();
  13. return 0;
  14. }
  15.  
  16. typedef std::vector<cv::Point> vectorOfCvPoints;
  17.  
  18. void dostuff()
  19. {
  20.  
  21. const double ellipseCenterX = 250;
  22. const double ellipseCenterY = 250;
  23. const double ellipseRadiusX = 150;
  24. const double ellipseRadiusY = 100;
  25.  
  26. vectorOfCvPoints datapoints;
  27.  
  28. for (int i = 0; i < 360; i+=5)
  29. {
  30. double angle = i / 180.0 * CV_PI;
  31. double x = ellipseRadiusX * cos(angle);
  32. double y = ellipseRadiusY * sin(angle);
  33. x *= 1.4;
  34. y *= 1.4;
  35. x += ellipseCenterX;
  36. y += ellipseCenterY;
  37. datapoints.push_back(cv::Point(x,y));
  38. }
  39.  
  40. cv::Mat drawing = cv::Mat::zeros( 500, 500, CV_8UC1 );
  41.  
  42. for (int i = 0; i < datapoints.size(); i++)
  43. {
  44. const cv::Point & curPoint = datapoints[i];
  45. const double curPointX = curPoint.x;
  46. const double curPointY = curPoint.y * -1; //transform from image coordinates to geometric coordinates
  47.  
  48. double angleToEllipseCenter = atan2(curPointY - ellipseCenterY * -1, curPointX - ellipseCenterX); //ellipseCenterY * -1 for transformation to geometric coords (from image coords)
  49.  
  50. double nearestEllipseX = ellipseCenterX + ellipseRadiusX * cos(angleToEllipseCenter);
  51. double nearestEllipseY = ellipseCenterY * -1 + ellipseRadiusY * sin(angleToEllipseCenter); //ellipseCenterY * -1 for transformation to geometric coords (from image coords)
  52.  
  53.  
  54. cv::Point center(ellipseCenterX, ellipseCenterY);
  55. cv::Size axes(ellipseRadiusX, ellipseRadiusY);
  56. cv::ellipse(drawing, center, axes, 0, 0, 360, cv::Scalar(255));
  57. cv::line(drawing, curPoint, cv::Point(nearestEllipseX,nearestEllipseY*-1), cv::Scalar(180));
  58.  
  59. }
  60. cv::namedWindow( "ellipse", CV_WINDOW_AUTOSIZE );
  61. cv::imshow( "ellipse", drawing );
  62. cv::waitKey(0);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement