双立方插值?

时间:2014-01-04 17:04:27

标签: c# bicubic

我浏览了互联网,就双立方插值而言,我找不到一个简单的等式。维基百科关于这个主题的页面不是很有用,那么有没有简单的方法来学习Bicubic Interpolation如何工作以及如何实现它?我用它来生成Perlin Noise,但是使用双线性插值可以满足我的需求(我已经尝试过了)。

如果有人能通过一个好的网站或只是一个答案指出我正确的方向,我将不胜感激。 (我顺便使用C#)

4 个答案:

答案 0 :(得分:7)

使用this(感谢发现这个的AhmetKakıcı),我想出了如何添加双立方插值。对于那些也在寻找答案的人来说,这就是我所使用的:

private float CubicPolate( float v0, float v1, float v2, float v3, float fracy ) {
    float A = (v3-v2)-(v0-v1);
    float B = (v0-v1)-A;
    float C = v2-v0;
    float D = v1;

    return A*Mathf.Pow(fracy,3)+B*Mathf.Pow(fracy,2)+C*fracy+D;
}

为了获得2D插值,我先得到x,然后插入y。例如

float x1 = CubicPolate( ndata[0,0], ndata[1,0], ndata[2,0], ndata[3,0], fracx );
float x2 = CubicPolate( ndata[0,1], ndata[1,1], ndata[2,1], ndata[3,1], fracx );
float x3 = CubicPolate( ndata[0,2], ndata[1,2], ndata[2,2], ndata[3,2], fracx );
float x4 = CubicPolate( ndata[0,3], ndata[1,3], ndata[2,3], ndata[3,3], fracx );

float y1 = CubicPolate( x1, x2, x3, x4, fracy );

其中ndata定义为:

float[,] ndata = new float[4,4];
for( int X = 0; X < 4; X++ )
    for( int Y = 0; Y < 4; Y++ )
        //Smoothing done by averaging the general area around the coords.
        ndata[X,Y] = SmoothedNoise( intx+(X-1), inty+(Y-1) );

(intx和inty是请求坐标的浮动值.fracx和fracy是输入坐标的小数部分,分别为x-intxy-inty

答案 1 :(得分:1)

注意:上面尼古拉斯给出的Bicubic公式(作为答案)中有一个错误。通过对正弦曲线进行插值,我发现它是不正确的。

正确的公式是:

float A = 0.5f * (v3 - v0) + 1.5f * (v1 - v2);
float B = 0.5f * (v0 + v2) - v1 - A;
float C = 0.5f * (v2 - v0);
float D = v1;

有关派生,请参见https://www.paulinternet.nl/?page=bicubic

答案 2 :(得分:0)

我对使用的三次多项式有点困惑。

是的,它在0和1中给出了正确的值,但是据我所知,相邻单元的派生不适合。如果网格数据是线性的,它甚至不会返回一行....

在x = 0.5

中它不是点对称的

适合0和1的多项式对于相邻单元也具有相同的导数,因此平滑(几乎)易于计算。

(如果适合数据,它会缩小为线性形式)

    //Bicubic convolution algorithm, cubic Hermite spline
    static double CubicPolateConv
            (double vm1, double v0, double vp1, double vp2, double frac) {
        //The polynomial of degree 3 where P(x)=f(x) for x in {0,1}
        //and P'(1) in one cell matches P'(0) in the next, gives a continous smooth curve.
        //And we also wants the it to reduce nicely to a line, if that matches the data
        //P(x)=Ax^3+Bx^2+Cx-D=((Ax+B)x+C)X+D
        //P(0)=D       =v0
        //P(1)=A+B+C+D =Vp1
        //P'(0)=C      =(vp1-vm1)/2
        //p'(1)=3A+2B+C=(vp2-v0 )/2
        //Subtracting first and third from the second  
        //A+B =vp1-C-D = (vp1+vm1)/2 - v0
        //Subtracting that twice and a C from the last
        //A=(vp2-v0)/2 - 2(A+B) -C =(vp2-v0)/2 - (Vp1+vm1-2v0) - (vp1-vm1)/2
        // = 3(v0-vp1)/2 + (vp2-vm1)/2
        //B=(A+B)-A = (vp1+vm1)/2 - v0 - (3(v0-vp1)/2 + (vp2-vm1)/2)
        // = vm1 + 2vp1 - (5v0+vp2)/2;
        double C = (vp1 - vm1) / 2;
        double ApB =vp1 -C -v0;
        double A = (vp2 - v0) / 2 - 2 * ApB - C;
        double B = ApB - A;
        //double B = vm1 + 2 * vp1 - (5 * v0 + vp2) / 2;
        //double A = (3*(v0 - vp1) + (vp2 - vm1)) / 2;

        return ((A * frac + B) * frac + C) * frac + v0;
    }

答案 3 :(得分:0)

接Eske Rahn的答案并打了个电话(请注意,下面的代码使用(j,i)的矩阵尺寸惯例,而不是(x,y)的图像,但这对于插值而言无关紧要):

/// <summary>
/// Holds extension methods.
/// </summary>
public static class Extension
{
    /// <summary>
    /// Performs a bicubic interpolation over the given matrix to produce a
    /// [<paramref name="outHeight"/>, <paramref name="outWidth"/>] matrix.
    /// </summary>
    /// <param name="data">
    /// The matrix to interpolate over.
    /// </param>
    /// <param name="outWidth">
    /// The width of the output matrix.
    /// </param>
    /// <param name="outHeight">
    /// The height of the output matrix.
    /// </param>
    /// <returns>
    /// The interpolated matrix.
    /// </returns>
    /// <remarks>
    /// Note, dimensions of the input and output matrices are in
    /// conventional matrix order, like [matrix_height, matrix_width],
    /// not typical image order, like [image_width, image_height]. This
    /// shouldn't effect the interpolation but you must be aware of it
    /// if you are working with imagery.
    /// </remarks>
    public static float[,] BicubicInterpolation(
        this float[,] data, 
        int outWidth, 
        int outHeight)
    {
        if (outWidth < 1 || outHeight < 1)
        {
            throw new ArgumentException(
                "BicubicInterpolation: Expected output size to be " +
                $"[1, 1] or greater, got [{outHeight}, {outWidth}].");
        }

        // props to https://stackoverflow.com/a/20924576/240845 for getting me started
        float InterpolateCubic(float v0, float v1, float v2, float v3, float fraction)
        {
            float p = (v3 - v2) - (v0 - v1);
            float q = (v0 - v1) - p;
            float r = v2 - v0;

            return (fraction * ((fraction * ((fraction * p) + q)) + r)) + v1;
        }

        // around 6000 gives fastest results on my computer.
        int rowsPerChunk = 6000 / outWidth; 
        if (rowsPerChunk == 0)
        {
            rowsPerChunk = 1;
        }

        int chunkCount = (outHeight / rowsPerChunk) 
                         + (outHeight % rowsPerChunk != 0 ? 1 : 0);

        var width = data.GetLength(1);
        var height = data.GetLength(0);
        var ret = new float[outHeight, outWidth];

        Parallel.For(0, chunkCount, (chunkNumber) =>
        {
            int jStart = chunkNumber * rowsPerChunk;
            int jStop = jStart + rowsPerChunk;
            if (jStop > outHeight)
            {
                jStop = outHeight;
            }

            for (int j = jStart; j < jStop; ++j)
            {
                float jLocationFraction = j / (float)outHeight;
                var jFloatPosition = height * jLocationFraction;
                var j2 = (int)jFloatPosition;
                var jFraction = jFloatPosition - j2;
                var j1 = j2 > 0 ? j2 - 1 : j2;
                var j3 = j2 < height - 1 ? j2 + 1 : j2;
                var j4 = j3 < height - 1 ? j3 + 1 : j3;
                for (int i = 0; i < outWidth; ++i)
                {
                    float iLocationFraction = i / (float)outWidth;
                    var iFloatPosition = width * iLocationFraction;
                    var i2 = (int)iFloatPosition;
                    var iFraction = iFloatPosition - i2;
                    var i1 = i2 > 0 ? i2 - 1 : i2;
                    var i3 = i2 < width - 1 ? i2 + 1 : i2;
                    var i4 = i3 < width - 1 ? i3 + 1 : i3;
                    float jValue1 = InterpolateCubic(
                        data[j1, i1], data[j1, i2], data[j1, i3], data[j1, i4], iFraction);
                    float jValue2 = InterpolateCubic(
                        data[j2, i1], data[j2, i2], data[j2, i3], data[j2, i4], iFraction);
                    float jValue3 = InterpolateCubic(
                        data[j3, i1], data[j3, i2], data[j3, i3], data[j3, i4], iFraction);
                    float jValue4 = InterpolateCubic(
                        data[j4, i1], data[j4, i2], data[j4, i3], data[j4, i4], iFraction);
                    ret[j, i] = InterpolateCubic(
                        jValue1, jValue2, jValue3, jValue4, jFraction);
                }
            }
        });

        return ret;
    }
}