Ask Your Question

how to draw the curve line?

asked 2015-11-05 09:31:17 -0500

hans gravatar image

updated 2016-05-26 14:31:09 -0500

I'm trying to draw a curve. But I do not know how to draw the point y, we ask for help.

image description

        int matWidth = 500;
        int matHeight = 500;

        int fromPointX = 50;
        int fromPointY = 400;

        int toPointX = 300;
        int toPointY = 200;

        ArrayList<Point> pointList = new ArrayList<Point>();

        for(int i = fromPointX; i < toPointX; i++) {
            Point point = new Point();
            point.x = i;

            // I do not know how to draw a curve.
            point.y = fromPointY++;

        for(int i = 1; i < pointList.size(); i++) {
            Core.line(mat, pointList.get(i-1), pointList.get(i),new Scalar(255,255,0), 5);
edit retag flag offensive close merge delete


It would be interesting also in c++ if Java has not the possibility :p

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-05 10:17:20 -0500 )edit

I think your problem isn't that you don't know how to draw a curve but that you don't know how to define a curve. From your so called point x and point y in the figure, infinite curves can be defined. But once you have defined it, and you have a list of all its points (or a good number of them; the more the smoother the curve), then drawing is simple: either you use line iteratively with every pair of consecutive points (as shown in your code) or you use a probably better function such as polylines (you should check if it's available on the java wrapper)

LorenaGdL gravatar imageLorenaGdL ( 2015-11-05 10:24:25 -0500 )edit

2 answers

Sort by ┬╗ oldest newest most voted

answered 2015-11-05 12:34:06 -0500

pklab gravatar image

updated 2015-11-05 12:44:52 -0500

To draw a point use a small circle or simply set the value at the given coordinates. In C++:

// draws the curve using polylines and line width (RED)
cv::polylines(mat, pointList, false, Scalar(0, 0, 255), lineWidht);

// draws the curve using dots
int lineWidht = 3;
for (int i = 0; i < pointList.size(); i++) {
    pt = pointList[i];
    // draw the dots using filled circle (GREEN)
    circle(mat, pt, cvRound((double)lineWidht / 2), Scalar(0, 255, 0), -1);
    // OR draw 1px point using direct assigment (MAGENTA)
    if ((pt.x<mat.cols) && (pt.y<mat.rows) && (pt.x*pt.y >= 0))
  <Vec3b>(pt.y, pt.x) = Vec3b(255, 0, 255);

To draw a curve from a list of points use polyline as @LorenaGdL. image description

edit flag offensive delete link more

answered 2015-11-05 11:43:30 -0500

LorenaGdL gravatar image

updated 2015-11-05 11:44:10 -0500

Small example following my previous comment. Obviously it is C++, but porting to Java should be straightforward. Once again, you already had the drawing part in your code, what it's missing is the curve definition (and that, of course, is a pure mathematical thing and has nothing to do with OpenCV).

Mat img(350, 300, CV_8UC1, Scalar(0));
Mat img2 = img.clone();

float start_point_x = 20;
float end_point_x = 120;
vector<Point2f> curvePoints;

//Define the curve through equation. In this example, a simple parabola
for (float x = start_point_x; x <= end_point_x; x+=1){
    float y = 0.0425*x*x - 6.25*x + 258;
    Point2f new_point = Point2f(2*x, 2*y);                  //resized to better visualize
    curvePoints.push_back(new_point);                       //add point to vector/list

//Option 1: use polylines
Mat curve(curvePoints, true);
curve.convertTo(curve, CV_32S); //adapt type for polylines
polylines(img, curve, false, Scalar(255), 2, CV_AA);

//Option 2: use line with each pair of consecutives points
for (int i = 0; i < curvePoints.size() - 1; i++){
    line(img2, curvePoints[i], curvePoints[i + 1], Scalar(255), 2, CV_AA);


imshow("Curve 1 - polylines", img);
imshow("Curve 2 - line", img2);

And that results in the following images (img on the left, img2 on the right - yep, they're identical) image descriptionimage description

edit flag offensive delete link more


berak gravatar imageberak ( 2015-11-05 12:04:35 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-11-05 09:31:17 -0500

Seen: 7,830 times

Last updated: Nov 05 '15