OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Mon, 04 Jun 2018 05:29:03 -05002D spline algorithm in OpenCVhttp://answers.opencv.org/question/192956/2d-spline-algorithm-in-opencv/Does OpenCV have a method for generating a 2D 'spline' contour? IOW, choose gray-scale pixel values at selected points on a 2D map and have them connected by a smoothed 2-dimensional contour. Output would presumably be gray-scale values in Mat form.
The inputs to a regular spline are a set of point coordinates, and output is a continuous curve that connects the input points.
In this case, the input would be a set of point coordinates -each with a gray-scale value-. The output would be a smoothed gray-scale contour that covers the entire square Mat.
That was tougher to explain than I expected. Hope it was clear. Perhaps that would actually be a 3D spline in that gray-scale value would be the 3rd dimension. But this is for generating a 2D image.
MartianMon, 04 Jun 2018 05:29:03 -0500http://answers.opencv.org/question/192956/cubic spline interpolation on contour pointshttp://answers.opencv.org/question/61692/cubic-spline-interpolation-on-contour-points/ I have traced the boundary of an object using openCV as shown in the figure :
![image description](/upfiles/14313826035031836.png)
I have vectorX and vectorY holding the x-axis and y-axis coordinates of above curve. Th curve looks fine but when we zoom it in we find that the curve is not smooth i.e. jagged:
![image description](/upfiles/1431382625296654.png)
I require smooth curve before further processing. In MATLAB, we can use CSAPS (Cubic smoothing spline) to smooth such curves and it works pretty well. I am looking to do the same thing either using openCV or some free C++ library. I am looking to get an output like the following (where curve is smoothed by CSAPS function):
![image description](/upfiles/1431382645462488.png)
Any help will be really appreciated in this regard.
My code:
ret,thresh = cv2.threshold(imgray,50,255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
xSample = np.flipud(contours[0][:,0,0])
ySample = np.flipud(contours[0][:,0,1])
arraySample = np.vstack([xSample, ySample])
arraySample = arraySample.T
plt.figure(2)
plt.plot(arraySample[:,0], arraySample[:,1], 'ro-')
plt.show()NabeelKhanMon, 11 May 2015 17:18:33 -0500http://answers.opencv.org/question/61692/B-spline smoothinghttp://answers.opencv.org/question/39892/b-spline-smoothing/
I have been working on video stabilization for quite a few weeks now. The algorithm I'm following basically involves 3 steps :-
1. FAST feature detection and Matching
2. Calculating affine transformation (scale + rotation + translation x + translation y ) from matched keypoints
3. Smooth motion parameters using cubic spline or b-spline.
I have been able to calculate affine transform. But I am stuck at smoothing motion parameters. I have been unable to evaluate spline function to smooth the three parameters.
Here is a graph for smoothed data points
![image description](http://i.stack.imgur.com/daklR.png)
Any suggestion or help as to how can I code to get a desired result as shown in the graph?
shawshankWed, 20 Aug 2014 10:05:24 -0500http://answers.opencv.org/question/39892/Get smoothing point using B-spline curve C++http://answers.opencv.org/question/34707/get-smoothing-point-using-b-spline-curve-c/I'm working on video stabilization topic. At the smoothing part, I need smooth parameters (translation, rotation+scale) by using B-spline curve for warping in order to create stabilized video.
Now, I am testing on some points. For example, I have 4 points (control points) with degree = 2, after using b-spline I wanna obtain 4 smoothed points. But when I use B-spline curve sample code in below, it created more than 4 points. I can not understand exactly that those results points which are belong to original points (control points). If I only get 4 points, the results is like as 4 original points (4 control points).How to get 4 smoothed points by using B-Spline curve?
Hope you guys help me to show this problem.
Here is the code.
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
/*
Subroutine to generate a B-spline open knot vector with multiplicity
equal to the order at the ends.
c = order of the basis function
n = the number of defining polygon vertices
nplus2 = index of x() for the first occurence of the maximum knot vector value
nplusc = maximum value of the knot vector -- $n + c$
x() = array containing the knot vector
*/
void knot(int n, int c, int x[])
{
int nplusc,nplus2,i;
nplusc = n + c;
nplus2 = n + 2;
x[1] = 0;
for (i = 2; i <= nplusc; i++){
if ( (i > c) && (i < nplus2) )
x[i] = x[i-1] + 1;
else
x[i] = x[i-1];
}
}
/* Subroutine to generate B-spline basis functions for open knot vectors
C code for An Introduction to NURBS
by David F. Rogers. Copyright (C) 2000 David F. Rogers,
All rights reserved.
Name: basis.c
Language: C
Subroutines called: none
Book reference: p. 279
c = order of the B-spline basis function
d = first term of the basis function recursion relation
e = second term of the basis function recursion relation
npts = number of defining polygon vertices
n[] = array containing the basis functions
n[1] contains the basis function associated with B1 etc.
nplusc = constant -- npts + c -- maximum number of knot values
t = parameter value
temp[] = temporary array
x[] = knot vector
*/
void basis(int c,float t, int npts,int x[],float n[])
{
int nplusc;
int i,k;
float d,e;
float temp[36];
nplusc = npts + c;
/* calculate the first order basis functions n[i][1] */
for (i = 1; i<= nplusc-1; i++){
if (( t >= x[i]) && (t < x[i+1]))
temp[i] = 1;
else
temp[i] = 0;
}
/* calculate the higher order basis functions */
for (k = 2; k <= c; k++){
for (i = 1; i <= nplusc-k; i++){
if (temp[i] != 0)/* if the lower order basis function is zero skip the calculation */
d = ((t-x[i])*temp[i])/(x[i+k-1]-x[i]);
else
d = 0;
if (temp[i+1] != 0) /* if the lower order basis function is zero skip the calculation */
e = ((x[i+k]-t)*temp[i+1])/(x[i+k]-x[i+1]);
else
e = 0;
temp[i] = d + e;
}
}
if (t == (float)x[nplusc]){ /* pick up last point */
temp[npts] = 1;
}
/* put in n array */
for (i = 1; i <= npts; i++) {
n[i] = temp[i];
}
}
/*Subroutine to generate a B-spline curve using an uniform open knot vector
C code for An Introduction to NURBS
by David F. Rogers. Copyright (C) 2000 David F. Rogers,
All rights reserved.
Name: bspline.c
Language: C
Subroutines called: knot.c, basis.c, fmtmul.c
Book reference: Section 3.5, Ex. 3.4, Alg. p. 281
b[] = array containing the defining polygon vertices
b[1] contains the x-component of the vertex
b[2] contains the y-component of the vertex
b[3] contains the z-component of the vertex
k = order of the \bsp basis function
nbasis = array containing the basis functions for a single value of t
nplusc = number of knot values
npts = number of defining polygon vertices
p[,] = array containing the curve points
p[1] contains the x-component of the point
p[2] contains the y-component of the point
p[3] contains the z-component of the point
p1 = number of points to be calculated on the curve
t = parameter value 0 <= t <= 1
x[] = array containing the knot vector
*/
void bspline(int npts,int k,int p1,float b[],float p[])
{
int i,j,icount,jcount;
int i1;
int x[30]; /* allows for 20 data points with basis function of order 5 */
int nplusc;
float step;
float t;
float nbasis[20];
float temp;
nplusc = npts + k;
/* zero and re-dimension the knot vector and the basis array */
for(i = 0; i <= npts; i++){
nbasis[i] = 0.;
}
for(i = 0; i <= nplusc; i++){
x[i] = 0.;
}
/* generate the uniform open knot vector */
knot(npts,k,x);
icount = 0;
/*calculate the points on the bspline curve */
t = 0;
step = ((float)x[nplusc])/((float)(p1-1));
for (i1 = 1; i1<= p1; i1++){
if ((float)x[nplusc] - t < 5e-6){
t = (float)x[nplusc];
}
basis(k,t,npts,x,nbasis); /* generate the basis function for this value of t */
for (j = 1; j <= 3; j++){ /* generate a point on the curve */
jcount = j;
p[icount+j] = 0.;
for (i = 1; i <= npts; i++){ /* Do local matrix multiplication */
temp = nbasis[i]*b[jcount];
p[icount + j] = p[icount + j] + temp;
jcount = jcount + 3;
}
}
icount = icount + 3;
t = t + step;
}
}
void main()
{
int i;
int npts,k,p1;
float b[31]; /* allows for up to 10 control vertices */
float p[103]; /* allows for up to 100 points on curve */
npts = 4;
k = 2; /* second order, change to 4 to get fourth order */
p1 = 11; /* eleven points on curve */
for (i = 1; i <= 3*npts; i++){
b[i] = 0.;
}
for (i = 1; i <= 3*p1; i++){
p[i] = 0.;
}
/*
Define the control polygon, Ex. 3.4 in the z=1 plane because
this is three dimensional routine. x=b[1], y=b[2], z=b[3], etc.
*/
b[1]=1;
b[2]=0;
b[3]=0;
b[4]=2;
b[5]=0;
b[6]=0;
b[7]=4;
b[8]=0;
b[9]=0;
b[10]=3;
b[11]=0;
b[12]=0;
bspline(npts,k,p1,b,p);
printf("\nPolygon points\n\n");
for (i = 1; i <= 3*npts; i=i+3){
printf(" %f %f %f \n",b[i],b[i+1],b[i+2]);
}
printf("\nCurve points\n\n");
for (i = 1; i <= 3*p1; i=i+3){
printf(" %f %f %f \n",p[i],p[i+1],p[i+2]);
}
getch();
}
Here is the current result. It created more than 4 smoothed points.
![image description](/upfiles/14021446029183789.png)JennySat, 07 Jun 2014 07:35:05 -0500http://answers.opencv.org/question/34707/Smoothing data by using high degree B-spline Smoothinghttp://answers.opencv.org/question/33957/smoothing-data-by-using-high-degree-b-spline-smoothing/I am working on video stabilization. After obtaining affine transformation parameters (translation, scale+rotation) between two consecutive frames, I need to smooth out them for stabilize purpose. To do that, I want to use B-Spline to smooth it. Any suggestion or explanation how to use B-Spline to smooth those parameter (translation x, translation y, scale+rotation a) or some code c++ to smooth those points.
Thank you in advance.
Here is illustrated figure about smoothing some points.
![image description](/upfiles/14008571256820512.png)JennyFri, 23 May 2014 10:00:00 -0500http://answers.opencv.org/question/33957/