Ask Your Question
0

Get smoothing point using B-spline curve C++

asked 2014-06-07 07:35:05 -0600

Jenny gravatar image

updated 2014-06-07 07:36:50 -0600

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 ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-06-07 14:04:06 -0600

kovand11 gravatar image

The p1 = 11; determines the number of evaluated points. But if only fixed number of points needed, its a waste using a generic B-spline. The exact behaviour can be achived with only weighting the four points with precalculated B-spline basis function values. And the k = 2; means that the segments are simple lines which are just connect the control points, so it should be 3 to be continuous in tangent, and 4 to be continuous in curvature.

So quick fix is p1 = 4; and k = 3;

But this is not the best solution to filter out points.

An easy: Moving average A hard one: Kalman filter

edit flag offensive delete link more

Comments

@kovand11: Thank you so much. I did it. About using B-spline curve to filter out points, that's a way I want to test it.

Jenny gravatar imageJenny ( 2014-06-08 08:31:36 -0600 )edit

Question Tools

Stats

Asked: 2014-06-07 07:35:05 -0600

Seen: 8,400 times

Last updated: Jun 07 '14