Draw a Spline with OpenCV

Hello,

How to draw a spline with OpenCV ? With 3 given control points ?

Thank you,
Christophe Jacquelin,

There is no direct function, but you can do it by hand:

C = t²*P1 + 2*t*(1-t)*P2 + (1-t)²*P3 where 0<=t<=1

So, in practice it should be something like (untested code):

for t in range(0,1,0.01):
    x = t*t*p1x + 2*t*(1-t)p2x + (1-t)*(1-t)p3x
    y = t*t*p1y + 2*t*(1-t)p2y + (1-t)*(1-t)p3y
    image[int(y),int(x)]=255

You can find the formulae for higher order curves on the wikipedia page.

There is a small difference between spline and Bezier curves and Catmull-Rom curves. If Bezier curves are what you’re after, there is a code that lets you calculate the point along an arbitrary number of control points:

// https://stackoverflow.com/questions/785097/how-do-i-implement-a-bézier-curve-in-c
vector_4 getBezierPoint(vector<vector_4> points, float t)
{
	int i = points.size() - 1;

	while (i > 0)
	{
		for (int k = 0; k < i; k++)
		{
			points[k].x += t * (points[k + 1].x - points[k].x);
			points[k].y += t * (points[k + 1].y - points[k].y);
			points[k].z += t * (points[k + 1].z - points[k].z);
			points[k].w += t * (points[k + 1].w - points[k].w);
		}

		i--;
	}

	return points[0];
}

Does this help? If not, and you’re super desperate to have a spline, then check out the following header-only library. Works great for me!

Especially see this test file for how to use the library:

See also
GitHub - ufoym/WarpMan: Interactive image warping