使用双线性插值在图像重新调整大小时的结果不匹配

时间:2013-03-09 15:48:34

标签: image-processing cuda image-resizing

我使用双线性插值实现了用于图像大小调整的CUDA函数。假设该函数给出了正确的结果(视觉上),直到我在一个小矩阵上测试以检查输出图像的确切值。我得到的结果与OpenCV和MATLAB的结果不同。我在算法中找不到任何明显的缺陷。有人可以帮我吗?

双线性插值设备功能:

texture<float, cudaTextureType2D> tex32f;

//Device function 
__device__ float blinterp(const float xIndex, const float yIndex)
{
    //floor the coordinates to get to the nearest valid pixel
    const int intX = static_cast<int>(xIndex);
    const int intY = static_cast<int>(yIndex);

    //Set weights of pixels according to distance from actual location
    const float a = xIndex - intX;  
    const float b = yIndex - intY;

    /* _____________________
     *|          |          |
     *|(1-a)(1-b)| (a)(1-b) |
     *|__________|__________|
     *|          |          |
     *| (1-a)(b) |  (a)(b)  |
     *|__________|__________|
     */

    //Compute the weighted average of 4 nearest pixels
    float out   =   (1 - a) * (1 - b)   *   tex2D(tex32f, intX,intY)
                +   (a) * (1 - b)       *   tex2D(tex32f,intX + 1,intY)
                +   (1 - a) * (b)       *   tex2D(tex32f, intX,intY + 1)
                +   (a * b)             *   tex2D(tex32f,intX + 1,intY + 1);

    return out;
}

调整内核大小:

__global__ void kernel_resize(float* dst, int dstWidth, int dstHeight, int dstPitch, float xScale, float yScale)
{
    const int xIndex = blockIdx.x * blockDim.x + threadIdx.x;
    const int yIndex = blockIdx.y * blockDim.y + threadIdx.y;

    if(xIndex>=dstWidth || yIndex>=dstHeight)   return;

    const unsigned int tid = yIndex * dstPitch + xIndex;

    const float inXindex = xIndex * xScale;
    const float inYindex = yIndex * yScale;

    dst[tid] = blinterp(inXindex,inYindex);
}

包装函数:

int resize_32f_c1(float* src,float* dst,int srcWidth,int srcHeight, int srcPitch, int dstWidth,int dstHeight,int dstPitch)
{
    if((srcWidth == dstWidth) && (srcHeight == dstHeight))
    {
        cudaMemcpy2D(dst,dstPitch,src,srcPitch,srcWidth * sizeof(float),srcHeight,cudaMemcpyDeviceToDevice);
        return 0;
    }

    cudaBindTexture2D(NULL,tex32f,src,srcWidth,srcHeight,srcPitch);

    dim3 Block(16,16);
    dim3 Grid((dstWidth + Block.x - 1)/Block.x, (dstHeight + Block.y - 1)/Block.y);

    float x = (float)(srcWidth)/(float)dstWidth;
    float y = (float)(srcHeight)/(float)dstHeight;

    kernel_resize<<<Grid,Block>>>(dst,dstWidth,dstHeight,dstPitch/sizeof(float),x,y);

    cudaUnbindTexture(tex32f);

    return 0;
}

结果(缩小2):

输入(10 x 10):

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

MATLAB和OpenCV输出:

0   0     0    0    0
0   0.25  0.5  0.25 0
0   0.5   1    0.5  0
0   0.25  0.5  0.25 0
0   0     0    0    0

我的输出:

0 0 0 0 0
0 0 0 0 0
0 0 1 1 0
0 0 1 1 0
0 0 0 0 0

1 个答案:

答案 0 :(得分:4)

根据@talonmies提供的建议,我最终找到了问题的原因。

当计算输入图像中像素的位置时,像素的坐标应该是以像素为中心的,即应该将偏移量0.5加到计算出的像素坐标上。不仅如此,还应使用以像素为中心的坐标来计算像素的权重。输入图像中像素的坐标应在内核中按如下方式计算:

const float inXindex = xIndex * xScale + 0.5f;
const float inYindex = yIndex * yScale + 0.5f;

或者,blinterp函数可以修改如下:

__device__ float blinterp(const float xIndex, const float yIndex)
{
    //round the coordinates to get to the nearest valid pixel
    const int intX = static_cast<int>(xIndex + 0.5f);
    const int intY = static_cast<int>(yIndex + 0.5f);

    //Set weights of pixels according to distance from actual location
    const float a = xIndex - intX + 0.5f;  
    const float b = yIndex - intY + 0.5f;
    .
    .
    .