Ask Your Question
1

Drawing an arc

asked 2017-08-30 16:36:44 -0600

rudolfninja gravatar image

updated 2017-08-31 06:51:07 -0600

Hi guys, I have arc parameters from Autocad file and I need to draw it using opencv. However, using corresponding parameters in cv::ellipse function the arc is absolutely not the same as in Autocad. Here is my code:

cv::ellipse(image, center, cv::Size(radius, radius), angle, start_angle, end_angle, cv::Scalar(255, 255, 255));

The picture should be like image description

but in fact I've got image description

Here is parameters for every arc:

1) center.x = 495.419403
    center.y = 527.065674
    start angle = 0.0000
    end abgle = 56.88228
    full angle = 56.88228
    start.x = 501.41940
    start.y = 527.06567
    end.x = 498.69757
    end.y = 532.090979
    arc_center.x = 500.695245
    arc_center.y = 529.92321
    radius = 6.00000

2) center.x = 495.4194
    center.y = 486.31543
    start angle = 270.0000
    end abgle = 0.0000
    full angle = 90.0000
    start.x = 495.41940
    start.y = 480.31543
    end.x = 501.4194
    end.y = 486.31543
    arc_center.x = 499.6620
    arc_center.y = 482.07279
    radius = 6.00000

3) center.x = 454.6691
    center.y = 486.31543
    start angle = 213.11771
    end abgle = 270.000
    full angle = 56.88228
    start.x = 449.64386
    start.y = 483.03727
    end.x = 454.66916
    end.y = 480.31543
    arc_center.x = 451.8116
    arc_center.y = 481.03959
    radius = 6.00000

4) center.x = 419.4194
    center.y = 562.31543
    start angle = 56.88228
    end abgle = 213.1177
    full angle = 156.2354
    start.x = 429.2539
    start.y = 577.3913
    end.x = 404.3435
    end.y = 552.4809415
    arc_center.x = 406.69148
    arc_center.y = 575.04336
    radius = 18.00000

How can I draw correct arcs? I've got start and end points of the arc, radius, and center points (arc center and center from where radius goes)

edit retag flag offensive close merge delete

Comments

could you post whole code you tried. or values you get from autocad

sturkmen gravatar imagesturkmen ( 2017-08-30 17:12:34 -0600 )edit

Seems like you're not passing the right start and end angles. Can you calculate those angles in AutoCAD ?

Ziri gravatar imageZiri ( 2017-08-30 22:47:51 -0600 )edit

I updated my post with required info. Looks like the problem might be somewhere in that, that autocad coordinates start from left bottom corner while in openCV from left top corner. I changed Y value in every point to (H - autocad_point.y). Now the image has correct rotation, but still problems with arcs. Here is what I got now: picture

rudolfninja gravatar imagerudolfninja ( 2017-08-31 00:47:32 -0600 )edit

As @sturkmen asked, provide code please so that we can debug it locally. Could it be a radians/degrees issue?

StevenPuttemans gravatar imageStevenPuttemans ( 2017-08-31 03:45:09 -0600 )edit

Degrees. I can't provide full code because it uses 3rd partylibraries to work with autocad file. So you won't be able to compile this code. I just can provide coordinatess of all the primitives for you to try to draw them. I can provide autocad file as well.

rudolfninja gravatar imagerudolfninja ( 2017-08-31 03:50:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-08-31 04:19:36 -0600

updated 2017-08-31 06:40:11 -0600

i think we can use the code below to find the answer. lets try change something

code draws something like this

image description

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat img(600, 600, CV_8UC3, Scalar(0, 0, 0));

    Point2d center, start, end, arc_center;
    center.x = 495.419403;
    center.y = 527.065674;
    double start_angle = 0.0;
    double end_angle = 56.88228;
    double full_angle = 56.88228;

    start.x = 501.41940;
    start.y = 527.06567;
    end.x = 498.69757;
    end.y = 532.090979;
    arc_center.x = 500.695245;
    arc_center.y = 529.92321;
    double radius = 6.0;
    double angle = 0.0;

    Point2d lastline_end = end;
    Point2d line_start = start;
    end_angle = end_angle == 0.0 ? 360.0 : end_angle;
    cv::ellipse(img, center, cv::Size(radius, radius), angle, start_angle, end_angle, cv::Scalar(255, 0, 0));

    center.x = 495.4194;
    center.y = 486.31543;
    start_angle = 270.0000;
    end_angle = 0.0000;
    full_angle = 90.0000;
    start.x = 495.41940;
    start.y = 480.31543;
    end.x = 501.4194;
    end.y = 486.31543;
    arc_center.x = 499.6620;
    arc_center.y = 482.07279;
    radius = 6.00000;

    line(img, line_start, end, Scalar(255, 255, 255));
    line_start = start;
    end_angle = end_angle == 0.0 ? 360.0 : end_angle;
    cv::ellipse(img, center, cv::Size(radius, radius), angle, start_angle, end_angle, cv::Scalar(0, 0, 255));


    center.x = 454.6691;
    center.y = 486.31543;
    start_angle = 213.11771;
    end_angle = 270.000;
    full_angle = 56.88228;
    start.x = 449.64386;
    start.y = 483.03727;
    end.x = 454.66916;
    end.y = 480.31543;
    arc_center.x = 451.8116;
    arc_center.y = 481.03959;
    radius = 6.00000;

    line(img, line_start, end, Scalar(255, 255, 255));
    line_start = start;
    end_angle = end_angle == 0.0 ? 360.0 : end_angle;
    cv::ellipse(img, center, cv::Size(radius, radius), angle, start_angle, end_angle, cv::Scalar(0, 255, 0));

    center.x = 419.4194;
    center.y = 562.31543;
    start_angle = 56.88228;
    end_angle = 213.1177;
    full_angle = 156.2354;
    start.x = 429.2539;
    start.y = 577.3913;
    end.x = 404.3435;
    end.y = 552.4809415;
    arc_center.x = 406.69148;
    arc_center.y = 575.04336;
    radius = 18.00000;

    line(img, line_start, end, Scalar(255, 255, 255));
    line(img, lastline_end, start, Scalar(255, 255, 255));
    end_angle = end_angle == 0.0 ? 360.0 : end_angle;
    cv::ellipse(img, center, cv::Size(radius, radius), angle, start_angle, end_angle, cv::Scalar(255, 0, 255));
    flip(img, img, 0);
    imshow("Image", img);
    cv::waitKey(0);

    return 0;
}
edit flag offensive delete link more

Comments

Why do you use angle with 0.0 value but not full angle (end angle - start angle)? This is the difference in my code

rudolfninja gravatar imagerudolfninja ( 2017-08-31 04:40:12 -0600 )edit

because i have no info about angle and first set it to 0 to test. and changed it to 180 for red angle

sturkmen gravatar imagesturkmen ( 2017-08-31 04:45:34 -0600 )edit

You've got end_angle and start angle. So you can calc full_angle. That waht I do. But looks like when use 0, it looks almost correct.

rudolfninja gravatar imagerudolfninja ( 2017-08-31 04:47:11 -0600 )edit

I've added additional check: end_angle = end_angle == 0.0 ? 360.0 : end_angle; and it solved the problem with red arc and now everything looks correct. Now the following question: autocad coordinates start from left bottom corner while openCV from left top. So, to transofrm autocad coordinates to openCV I use following formula: opencv.x = autocad.x; opencv.y = H - autocad.y; where H is matrix height. However in this case arcs has incorrect angles. How should I transform angles in this case?

rudolfninja gravatar imagerudolfninja ( 2017-08-31 05:03:06 -0600 )edit

maybe flipping image will help. see updated answer.

sturkmen gravatar imagesturkmen ( 2017-08-31 06:41:54 -0600 )edit

flipping change the position of the other points. But actually, it is not so importatnt for me.I still don't understand why we use zero angle instead of end_angle - start_angle. I've got another cad file with a lot of arcs where they doesn't shown correct using this way of drawing. Should I create new answer to discuss the problem with this cad file? I'd like to get common algorithm of drawing that works for every arc.

rudolfninja gravatar imagerudolfninja ( 2017-08-31 06:54:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-08-30 16:36:44 -0600

Seen: 5,213 times

Last updated: Aug 31 '17