如何从多条直线画一条曲线?

How to draw a curved line from multiple straight lines?

我正在尝试将一些直线连接起来形成一条曲线。

例如,如果我有这样的三行

:

我想画一条这样的曲线:

我尝试使用 OpenCV linepolylines 函数。

    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));

            }
        }
    }

但它不起作用,因为它连接了彼此相距很远的线。 另外,有没有更快的版本我可以试试?

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

@Pete 建议真是个好主意。您需要计算贝塞尔曲线的点数:

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]);

在此之后,您可以 build 使用此点的完整贝塞尔曲线路径。