OpenCL:使用双线性插值进行下采样

时间:2014-07-30 16:32:19

标签: c++ opencl

我遇到了使用双线性插值对图像进行下采样的问题。我已经阅读了关于stackoverflow的几乎所有相关文章,并在谷歌中搜索过,试图解决或者至少在我的OpenCL内核中找到问题。这是理论的main source。我在OpenCL中实现了这段代码后:

__kernel void downsample(__global uchar* image, __global uchar* outputImage, __global int* width, __global int* height, __global float* factor){

//image vector containing original RGB values
//outputImage vector containing "downsampled" RGB mean values
//factor - downsampling factor, downscaling the image by factor: 1024*1024 -> 1024/factor * 1024/factor

int r = get_global_id(0);
int c = get_global_id(1); //current coordinates
int oWidth = get_global_size(0);     
int olc, ohc, olr, ohr; //coordinates of the original image used for bilinear interpolation
int index; //linearized index of the point  
uchar q11, q12, q21, q22;
float accurate_c, accurate_r; //the exact scaled point
int k;

accurate_c = convert_float(c*factor[0]);
olc=convert_int(accurate_c);
ohc=olc+1;

if(!(ohc<width[0]))
ohc=olc;

accurate_r = convert_float(r*factor[0]);
olr=convert_int(accurate_r);
ohr=olr+1;

if(!(ohr<height[0]))
ohr=olr;

index= (c + r*oWidth)*3; //3 bytes per pixel
//Compute RGB values: take a central mean RGB values among four points 
for(k=0; k<3; k++){
    q11=image[(olc + olr*width[0])*3+k];
    q12=image[(olc + ohr*width[0])*3+k];
    q21=image[(ohc + olr*width[0])*3+k];
    q22=image[(ohc + ohr*width[0])*3+k];
    outputImage[index+k] = convert_uchar((q11*(ohc - accurate_c)*(ohr - accurate_r) + 
                                          q21*(accurate_c - olc)*(ohr - accurate_r) + 
                                          q12*(ohc - accurate_c)*(accurate_r - olr) +
                                          q22*(accurate_c - olc)*(accurate_r - olr)));
  }
}

内核使用因子= 2,4,5,6而不是因子= 3,7(我得到丢失的像素,图像看起来有点偏斜)而用c ++编写的“相同”代码可以正常工作所有因子值。我无法向自己解释为什么在opencl中发生这种情况。我附上了我的完整代码项目here

0 个答案:

没有答案