# how to draw the curve line?

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

        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 close merge delete

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

( 2015-11-05 10:17:20 -0500 )edit
1

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)

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

Sort by » oldest newest most voted

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))
mat.at<Vec3b>(pt.y, pt.x) = Vec3b(255, 0, 255);
}


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

more

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
}

//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);
waitKey();


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

more