二维矩阵旋转与双线性插值

2D Matrix Rotation with Bilinear Interpolation

我在执行旋转图像的双线性插值时遇到困难。图像在二维矩阵中表示为双打。下面是我一直在使用的代码。图像正确旋转了所需的角度,但是插值似乎没有按需要消除输出图像的 'jaggedness'。

任何人都可以找出我的代码的问题吗?

// DimX & DimY are dimensions of input Image (Which has padded space for rotation)
// radian = angle as rad (2*PI*{angle in deg})/360)
// COGPosX & COGPosY are the Centre of Gravity pos for the Input Matrix
// SliceMatrix is the un-rotated input Matrix

// double cosine,sine,f1,f2,fval,p1,p2,p3,p4,rotX,rotY,xfloor,yfloor;
// double *sliceMatrix, *rotationMatrixInter;
// int x,y,dimX,dimY,COGPosX,COGPosY,rotatedX,rotatedY;   

cosine = (double)cos(radian);
sine = (double)sin(radian);

for(y=0;y<dimY;y++)
{
    for(x=0;x<dimX;x++)
    {
        // Calculate rotated Matrix positions
        rotatedX=(double)((x-COGPosX)*cosine)-((y-COGPosY)*sine)+COGPosX;
        rotatedY=(double)((x-COGPosX)*sine)+((y-COGPosY)*cosine)+COGPosY;

        rotX = (int)floor(rotatedX);
        rotY = (int)floor(rotatedY);

        xfloor = floor(rotatedX);
        yfloor = floor(rotatedY);

        if(rotX >=0 && rotY < dimX-1 && rotY >=0 && rotY < dimY-1 )
        {
            // BLI Calculation
            p1 = sliceMatrix[rotX+(dimX*rotY)];         // 0,0
            p2 = sliceMatrix[rotX+(dimX*(rotY+1))];     // 0,1
            p3 = sliceMatrix[(rotX+1)+(dimX*rotY)];     // 1,0
            p4 = sliceMatrix[(rotX+1)+(dimX*(rotY+1))]; // 1,1

            f1 = p1 + (p3-p1)*(rotatedX-xfloor);
            f2 = p2 + (p4-p2)*(rotatedX-xfloor);
            fval = f1 + (f2-f1)*(rotatedY-yfloor);

            rotationMatrixInter[x+(dimX*y)]= fval; 
        }    
    }
}

显然您并没有真正按预期进行插值。 rotatedXrotatedYint 类型;如果你把它们加倍,它们仍然是整数值,而分母

((double)(rotatedX+1.0)-(double)rotatedX))
((double)(rotatedX+1.0)-(double)rotatedX))

抵消为1.0,这意味着没有发生所需的真正插值,但f1f2实际上被分配给p3p4 分别。变量 rotatedXrotatedY 必须是 double 类型。旋转后,必须将它们向下和向上舍入(或向下舍入并加一个)以获得四个位置以从中对图像数据进行采样,并且舍入值的差异将控制插值。这可以按如下方式完成,其中 floor 应该向下舍入。

double xfloor = floor(rotatedX);
double yfloor = floor(rotatedY);

f1 = p1 + (p3-p1)*(rotatedX-xfloor);
f2 = p2 + (p4-p2)*(rotatedX-xfloor);
fval = f1 + (f2-f1)*(rotatedY-yfloor);