简体   繁体   中英

How to draw a curved line from multiple straight lines?

I am trying to connect some straight lines to form a curved line.

for example if I had three lines like these

在此处输入图片说明 :

and I wanted to draw a curved line like this:

在此处输入图片说明

I have tried to use OpenCV line and polylines functions.

    for (size_t i = 0;i < lines.size();i++)
    {
        for (size_t j = 0;j < lines.size();j++)
        {
            //first line
            Vec4i l1 = lines[i];
            Point p1 = Point(l1[0], l1[1]);
            Point p2 = Point(l1[2], l1[3]);

            //second line
            Vec4i l2 = lines[j];
            Point p3 = Point(l2[0], l2[1]);
            Point p4 = Point(l2[2], l2[3]);

            if ((cv::norm(p1 - p3) < 20) || (cv::norm(p1 - p4) < 20) || (cv::norm(p2 - p3) < 20) || (cv::norm(p2 - p4) < 20))
            {
                vector<Point> pointList;
                pointList.push_back(p1);
                pointList.push_back(p2);
                pointList.push_back(p3);
                pointList.push_back(p4);

                const Point *pts = (const cv::Point*) Mat(pointList).data;
                int npts = Mat(pointList).rows;

                polylines(img, &pts, &npts, 1, true, Scalar(255, 0, 0));

            }
        }
    }

but it doesn't work, because it connects lines that are far from each other. Also, is there a faster version of this I could try?

贝塞尔曲线可能会有所帮助( https://en.wikipedia.org/wiki/B%C3%A9zier_curve )。

@Pete suggest really good idea. You need to calculate points for Bezier:

vector<Point> bezierPoints;
bezierPoints.push_back(lines[0][0]);
for (size_t i = 1; i < lines.size(); i++) {
    Point mid = (lines[i-1][1] + lines[i][0]) / 2.0;
    bezierPoints.push_back(mid);
}
bezierPoints.push_back(lines.back()[1]);

After this you can build full Bezier path with this points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM