具有双线性插值的二维矩阵旋转

时间:2015-03-06 15:49:03

标签: c arrays math matrix interpolation

我无法实现旋转图像的双线性插值。图像在2D矩阵中表示为双精度。以下是我一直在使用的代码。图像以所需的角度正确旋转,但插值似乎并没有消除“锯齿状”。根据需要输出图像。

任何人都可以通过我的代码识别问题吗?

// 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; 
        }    
    }
}

1 个答案:

答案 0 :(得分:1)

显然你并没有像预期的那样进行内插。 rotatedXrotatedY的类型为int;如果你把它们加倍,它们仍然是整数值和分母

((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);