Opencv实现的三次样条曲线(Cubic Spline)插值

1.样条曲线简介

样条曲线(Spline)本质是分段多项式实函数,在实数范围内有: S:[a,b]→R ,在区间 [a,b] 上包含 k 个子区间[ti−1,ti],且有:

a=t0

对应每一段区间 i 的存在多项式: Pi:[ti−1,ti]→R,且满足于:

S(t)=P1(t) , t0≤t

其中, Pi(t) 多项式中最高次项的幂,视为样条的阶数或次数(Order of spline),根据子区间 [ti−1,ti] 的区间长度是否一致分为均匀(Uniform)样条和非均匀(Non-uniform)样条。

满足了公式 (2) 的多项式有很多,为了保证曲线在 S 区间内具有据够的平滑度,一条n次样条,同时应具备处处连续且可微的性质:

P(j)i(ti)=P(j)i+1(ti);(3)

其中 i=1,…,k−1;j=0,…,n−1 。

2.三次样条曲线

2.1曲线条件

按照上述的定义,给定节点:

t:z:a=t0z0

三次样条曲线满足三个条件:

  1. 在每段分段区间 [ti,ti+1],i=0,1,…,k−1 上, S(t)=Si(t) 都是一个三次多项式;
  2. 满足 S(ti)=zi,i=1,…,k−1 ;
  3. S(t) 的一阶导函数 S′(t) 和二阶导函数 S′′(t) 在区间 [a,b] 上都是连续的,从而曲线具有光滑性。

则三次样条的方程可以写为:

Si(t)=ai+bi(t−ti)+ci(t−ti)2+di(t−ti)3,(5)

其中, ai,bi,ci,di 分别代表 n 个未知系数。

  • 曲线的连续性表示为:

Si(ti)=zi,(6)

Si(ti+1)=zi+1,(7)

其中 i=0,1,…,k−1 。

  • 曲线微分连续性:

S′i(ti+1)=S′i+1(ti+1),(8)

S′′i(ti+1)=S′′i+1(ti+1),(9)

其中 i=0,1,…,k−2 。

  • 曲线的导函数表达式:

S′i=bi+2ci(t−ti)+3di(t−ti)2,(10)

S′′i(x)=2ci+6di(t−ti),(11)

令区间长度 hi=ti+1−ti ,则有:

  1. 由公式 (6) ,可得: ai=zi ;

  2. 由公式 (7) ,可得: ai+bihi+cih2i+dih3i=zi+1 ;

  3. 由公式 (8) ,可得:
    S′i(ti+1)=bi+2cihi+3dih2i ;
    S′i+1(ti+1)=bi+1 ;
    ⇒bi+2cihi+3dih2i−bi+1=0 ;

  4. 由公式 (9) ,可得:
    S′′i(ti+1)=2ci+6dihi ;
    S′′i+1(ti+1)=2ci+1 ;
    ⇒2ci+6dihi=2ci+1 ;

    设 mi=S′′i(xi)=2ci ,则:

    A. mi+6dihi−mi+1=0⇒
    di=mi+1−mi6hi ;

    B.将 ci,di 代入 zi+bihi+cih2i+dih3i=zi+1⇒
    bi=zi+1−zihi−hi2mi−hi6(mi+1−mi) ;

    C.将 bi,ci,di 代入 bi+2cihi+3dih2i=bi+1⇒

    himi+2(hi+hi+1)mi+1+hi+1mi+2=6[zi+2−zi+1hi+1−zi+1−zihi].(12)

2.2端点条件

在上述分析中,曲线段的两个端点 t0 和 tk 是不适用的,有一些常用的端点限制条件,这里只讲解自然边界。
在自然边界下,首尾两端的二阶导函数满足 S′′=0 ,即 m0=0 和 mk=0 。

3.三次样条插值类的实现

头文件
/*
  *Cubic spline interpolation class.
  *
*/

#ifndef CUBICSPLINEINTERPOLATION_H
#pragma once
#define CUBICSPLINEINTERPOLATION_H

#include 
#include 
#include 

#include 

using namespace std;
using namespace cv;

/* Cubic spline interpolation coefficients */
class CubicSplineCoeffs
{
public:
    CubicSplineCoeffs( const int &count )
    {
        a = std::vector(count);
        b = std::vector(count);
        c = std::vector(count);
        d = std::vector(count);
    }
    ~CubicSplineCoeffs()
    {
        std::vector().swap(a);
        std::vector().swap(b);
        std::vector().swap(c);
        std::vector().swap(d);
    }

public:
    std::vector a, b, c, d;
};

enum CubicSplineMode
{
    CUBIC_NATURAL,    // Natural
    CUBIC_CLAMPED,    // TODO: Clamped
    CUBIC_NOT_A_KNOT  // TODO: Not a knot
};

enum SplineFilterMode
{
    CUBIC_WITHOUT_FILTER, // without filter
    CUBIC_MEDIAN_FILTER  // median filter
};

/* Cubic spline interpolation */
class CubicSplineInterpolation
{
public:
    CubicSplineInterpolation() {}
    ~CubicSplineInterpolation() {}

public:
    /*
        Calculate cubic spline coefficients.
          - node list x (input_x);
          - node list y (input_y);
          - output coefficients (cubicCoeffs);
          - ends mode (splineMode).
    */
    void calCubicSplineCoeffs( std::vector &input_x,
                std::vector &input_y, CubicSplineCoeffs *&cubicCoeffs,
                CubicSplineMode splineMode = CUBIC_NATURAL,
                SplineFilterMode filterMode = CUBIC_MEDIAN_FILTER );

    /*
        Cubic spline interpolation for a list.
          - input coefficients (cubicCoeffs);
          - input node list x (input_x);
          - output node list x (output_x);
          - output node list y (output_y);
          - interpolation step (interStep).
    */
    void cubicSplineInterpolation( CubicSplineCoeffs *&cubicCoeffs,
                std::vector &input_x, std::vector &output_x,
                std::vector &output_y, const double interStep = 0.5 );

    /*
        Cubic spline interpolation for a value.
          - input coefficients (cubicCoeffs);
          - input a value(x);
          - output interpolation value(y);
    */
    void cubicSplineInterpolation2( CubicSplineCoeffs *&cubicCoeffs,
            std::vector input_x, double x, double &y );

    /*
        calculate  tridiagonal matrices with Thomas Algorithm(TDMA) :

        example:
        | b1 c1 0  0  0  0  |  |x1 |   |d1 |
        | a2 b2 c2 0  0  0  |  |x2 |   |d2 |
        | 0  a3 b3 c3 0  0  |  |x3 | = |d3 |
        | ...         ...   |  |...|   |...|
        | 0  0  0  0  an bn |  |xn |   |dn |

        Ci = ci/bi , i=1; ci / (bi - Ci-1 * ai) , i = 2, 3, ... n-1;
        Di = di/bi , i=1; ( di - Di-1 * ai )/(bi - Ci-1 * ai) , i = 2, 3, ..., n-1

        xi = Di - Ci*xi+1 , i = n-1, n-2, 1;
    */
    bool caltridiagonalMatrices( cv::Mat_ &input_a,
        cv::Mat_ &input_b, cv::Mat_ &input_c,
        cv::Mat_ &input_d, cv::Mat_ &output_x );

    /* Calculate the curve index interpolation belongs to */
    int calInterpolationIndex( double &pt, std::vector &input_x );

    /* median filtering */
    void cubicMedianFilter( std::vector &input, const int filterSize = 5 );

    double cubicSort( std::vector &input );
    // double cubicNearestValue( std::vector );
};

#endif // CUBICSPLINEINTERPOLATION_H
实现文件(cpp)
/*
 * CubicSplineInterpolation.cpp
 */

#include "cubicsplineinterpolation.h"

void CubicSplineInterpolation::calCubicSplineCoeffs(
    std::vector &input_x,
    std::vector &input_y,
    CubicSplineCoeffs *&cubicCoeffs,
    CubicSplineMode splineMode /* = CUBIC_NATURAL */,
    SplineFilterMode filterMode /*= CUBIC_MEDIAN_FILTER*/ )
{
    int sizeOfx = input_x.size();
    int sizeOfy = input_y.size();

    if ( sizeOfx != sizeOfy )
    {
        std::cout << "Data input error!" << std::endl <<
            "Location: CubicSplineInterpolation.cpp" <<
            " -> calCubicSplineCoeffs()" << std::endl;

        return;
    }

    /*
        hi*mi + 2*(hi + hi+1)*mi+1 + hi+1*mi+2
        =  6{ (yi+2 - yi+1)/hi+1 - (yi+1 - yi)/hi }

        so, ignore the both ends:
        | -     -     -        0           ...             0     |  |m0 |
        | h0 2(h0+h1) h1       0           ...             0     |  |m1 |
        | 0     h1    2(h1+h2) h2 0        ...                   |  |m2 |
        |         ...                      ...             0     |  |...|
        | 0       ...           0 h(n-2) 2(h(n-2)+h(n-1)) h(n-1) |  |   |
        | 0       ...                      ...             -     |  |mn |

    */

    std::vector copy_y = input_y;

    if ( filterMode == CUBIC_MEDIAN_FILTER )
    {
        cubicMedianFilter(copy_y, 5);
    }

    const int count  = sizeOfx;
    const int count1 = sizeOfx - 1;
    const int count2 = sizeOfx - 2;
    const int count3 = sizeOfx - 3;

    cubicCoeffs = new CubicSplineCoeffs( count1 );

    std::vector step_h( count1, 0.0 );

    // for m matrix
    cv::Mat_ m_a(1, count2, 0.0);
    cv::Mat_ m_b(1, count2, 0.0);
    cv::Mat_ m_c(1, count2, 0.0);
    cv::Mat_ m_d(1, count2, 0.0);
    cv::Mat_ m_part(1, count2, 0.0);

    cv::Mat_ m_all(1, count, 0.0);

    // initial step hi
    for ( int idx=0; idx < count1; idx ++ )
    {
        step_h[idx] = input_x[idx+1] - input_x[idx];
    }
    // initial coefficients
    for ( int idx=0; idx < count3; idx ++ )
    {
        m_a(idx) = step_h[idx];
        m_b(idx) = 2 * (step_h[idx] + step_h[idx+1]);
        m_c(idx) = step_h[idx+1];
    }
    // initial d
    for ( int idx =0; idx < count3; idx ++ )
    {
        m_d(idx) = 6 * (
            (copy_y[idx+2] - copy_y[idx+1]) / step_h[idx+1] -
            (copy_y[idx+1] - copy_y[idx]) / step_h[idx] );
    }

     //cv::Mat_ matOfm( count2,  )
    bool isSucceed = caltridiagonalMatrices(m_a, m_b, m_c, m_d, m_part);
    if ( !isSucceed )
    {
        std::cout<<"Calculate tridiagonal matrices failed!"< " <<
            "caltridiagonalMatrices()"<a[i] = copy_y[i];
            cubicCoeffs->b[i] = ( copy_y[i+1] - copy_y[i] ) / step_h[i] -
                step_h[i]*( 2*m_all(i) + m_all(i+1) ) / 6;
            cubicCoeffs->c[i] = m_all(i) / 2.0;
            cubicCoeffs->d[i] = ( m_all(i+1) - m_all(i) ) / ( 6.0 * step_h[i] );
        }
    }
    else
    {
        std::cout<<"Not define the interpolation mode!"< &input_x,
    std::vector &output_x,
    std::vector &output_y,
    const double interStep )
{
    const int count = input_x.size();

    double low  = input_x[0];
    double high = input_x[count-1];

    double interBegin = low;
    for ( ; interBegin < high; interBegin += interStep )
    {
        int index = calInterpolationIndex(interBegin, input_x);
        if ( index >= 0 )
        {
            double dertx = interBegin - input_x[index];
            double y = cubicCoeffs->a[index] + cubicCoeffs->b[index] * dertx +
                cubicCoeffs->c[index] * dertx * dertx +
                cubicCoeffs->d[index] * dertx * dertx * dertx;
            output_x.push_back(interBegin);
            output_y.push_back(y);
        }
    }
}

void CubicSplineInterpolation::cubicSplineInterpolation2(
    CubicSplineCoeffs *&cubicCoeffs,
    std::vector input_x, double x, double &y)
{
    const int count = input_x.size();

    double low  = input_x[0];
    double high = input_x[count-1];

    if ( xhigh )
    {
        std::cout<<"The interpolation value is out of range!"<= 0 )
        {
            double dertx = x - input_x[index];
            y = cubicCoeffs->a[index] + cubicCoeffs->b[index] * dertx +
                cubicCoeffs->c[index] * dertx * dertx +
                cubicCoeffs->d[index] * dertx * dertx * dertx;
        }
        else
        {
            std::cout<<"Can't find the interpolation range!"< &input_a,
    cv::Mat_ &input_b,
    cv::Mat_ &input_c,
    cv::Mat_ &input_d,
    cv::Mat_ &output_x )
{
    int rows = input_a.rows;
    int cols = input_a.cols;

    if ( ( rows == 1 && cols > rows ) ||
        (cols == 1 && rows > cols ) )
    {
        const int count = ( rows > cols ? rows : cols ) - 1;

        output_x = cv::Mat_::zeros(rows, cols);

        cv::Mat_ cCopy, dCopy;
        input_c.copyTo(cCopy);
        input_d.copyTo(dCopy);

        if ( input_b(0) != 0 )
        {
            cCopy(0) /= input_b(0);
            dCopy(0) /= input_b(0);
        }
        else
        {
            return false;
        }

        for ( int i=1; i < count; i++ )
        {
            double temp = input_b(i) - input_a(i) * cCopy(i-1);
            if ( temp == 0.0 )
            {
                return false;
            }

            cCopy(i) /= temp;
            dCopy(i) = ( dCopy(i) - dCopy(i-1)*input_a(i) ) / temp;
        }

        output_x(count) = dCopy(count);
        for ( int i=count-2; i > 0; i-- )
        {
            output_x(i) = dCopy(i) - cCopy(i)*output_x(i+1);
        }
        return true;
    }
    else
    {
        return false;
    }
}

int CubicSplineInterpolation::calInterpolationIndex(
    double &pt, std::vector &input_x )
{
    const int count = input_x.size()-1;
    int index = -1;
    for ( int i=0; i input_x[i] && pt <= input_x[i+1] )
        {
            index = i;
            return index;
        }
    }
    return index;
}

void CubicSplineInterpolation::cubicMedianFilter(
    std::vector &input, const int filterSize /* = 5 */ )
{
    const int count = input.size();
    for ( int i=filterSize/2; i temp(filterSize, 0.0);
        for ( int j=0; j().swap(temp);
    }

    for ( int i=0; i temp(filterSize, 0.0);
        for ( int j=0; j().swap(temp);
    }

    for ( int i=count-filterSize/2; i temp(filterSize, 0.0);
        for ( int j=0; j().swap(temp);
    }
}

double CubicSplineInterpolation::cubicSort( std::vector &input )
{
    int iCount = input.size();
    for ( int j=0; jj; k-- )
        {
            if ( input[k-1] > input[k] )
            {
                double tp  = input[k];
                input[k]   = input[k-1];
                input[k-1] = tp;
            }
        }
    }
    return input[iCount/2];
}

你可能感兴趣的:(OpenCV,图像视频处理,opencv,人工智能,计算机视觉)