我一直在尝试使用 OpenCL 图像过滤器时遇到问题。我以前写过很多这些(Sobel Edge Detection、Auto Segmentation 等),所以我以为我知道该怎么做,但是下面的代码给了我一些非常奇怪的输出:
//NoRedPixels.cl
__kernel void NoRedPixels(
__read_only image2d_t srcImg,
__write_only image2d_t dstImg,
sampler_t sampler,
int width, int height,
int threshold,
int colour,
int fill)
{
int2 imageCoordinate = (int2)(get_global_id(0), get_global_id(1));
if (imageCoordinate.x < width && imageCoordinate.y < height)
{
float4 pixel = read_imagef(srcImg, sampler, imageCoordinate);
float4 blue = (float4)(0.0f,0.0f,1.0f,1.0f);
if (1.0f - pixel.x <= 0.1f)
write_imagef(dstImg, imageCoordinate, blue);
else
write_imagef(dstImg, imageCoordinate, pixel);
}
}
所以为了测试,我想做的就是用蓝色像素替换红色像素,但是这段代码会将所有匹配的像素替换为白色像素。据我所知,我的蓝色格式是正确的 RGBA 格式,用于创建纯蓝色(我之前做过这个没有问题)。
我使用 PyOpenCL 作为我的框架,并确保将源图像和目标图像的图像通道顺序设置为 RGBA。此外,我还确保将源图像转换为 RGBA 格式(使用 Python 图像库),如果它在运行内核之前还不是那种格式的话。
我回去看了看我写的其他内核,格式是一样的。我在这里缺少什么会导致它写出白色像素而不是蓝色像素?