Full width home advertisement

OpenCV

HTML

Post Page Advertisement [Top]

 In this post I am explaining how a line can be drawn using a start point, angle and length of line. Basically we have to use the equation

x2 = x1 + length * cos(θ)
y2 = y1 + length * sin(θ) 
And θ should be in radians, so convert it using below equation.

θ = angle * 3.14 / 180.0
Below is the source code how using OpenCV it can achieve.
OpenCV Draw Line using initial point angle and length

C++ Code


    int angle = 45;
    int length = 300;
    Point P1(450,300);
    Point P2;

    while (1) {
      Mat src(700,900,CV_8UC3,Scalar(91,91,0));

      P2.x =  (int)round(P1.x + length * cos(angle * CV_PI / 180.0));
      P2.y =  (int)round(P1.y + length * sin(angle * CV_PI / 180.0));
      line(src,P1,P2,Scalar(0,0,255),5);
      String txt = format("Angle =  %d", angle);
      putText(src, txt, Point(50,50), FONT_HERSHEY_SIMPLEX, 1, CV_RGB(255, 255, 255), 2);

      txt = format("P2 =  (%d , %d)", P2.x,P2.y);
      putText(src, txt, Point(50,100), FONT_HERSHEY_SIMPLEX, 1, CV_RGB(255, 255, 255), 2);

      putText(src, " '+' to increase angle", Point(50,500), FONT_HERSHEY_SIMPLEX, 1, CV_RGB(255, 255, 255), 2);
      putText(src, " '-' to decrease angle", Point(50,550), FONT_HERSHEY_SIMPLEX, 1, CV_RGB(255, 255, 255), 2);

      imshow("src", src);
      imwrite("angle.jpg", src);

      char c = waitKey();
      if (c == 27)
        break;
      if(c == '+')
        angle++;
      if(c == '-')
        angle--;
    }

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib