OpenCl模糊图像算法

时间:2019-02-27 20:09:33

标签: opencl blur

我需要帮助来了解此算法。这是用于模糊图像的OpenCl算法。我认为输出图像中的每个像素等于输入图像中相邻像素的平均值。解压缩输入的像素以获取RGB分量,然后对每个分量应用滤镜,然后将其打包回到像素中。

 __kernel void Blur(__read_only image2d_t in, __write_only image2d_t out, int size)
{
    int Width = get_image_width(in);
    int Height = get_image_height(in);

    int tx = get_global_id(0);
    int ty = get_global_id(1);

    float kernelValue = 1.0 / (size*size);
    float halfVal = size / 2;

    float4 output;

    float R = 0, G = 0, B = 0;

    for(int x = -(int)halfVal; x <= (int)halfVal; x++)
    {
        for(int y = -(int)halfVal; y <= (int)halfVal; y++)
        {

            float realX = min(max(tx+x,0),Width-1);
            float realY = min(max(ty+y,0),Height-1);
            int2 coordsRead = (int2)(realX, realY);
            float4 c = read_imagef(in,coordsRead);

            R += c.x * kernelValue;
            G += c.y * kernelValue;
            B += c.z * kernelValue;
        }
    }
    output = (float4)(R,G,B,1);
    int2 coordsWrite = (int2)(tx,ty);
    write_imagef(out,coordsWrite,output);
}

0 个答案:

没有答案