本文介绍了OpenCL创建错误的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到OpenCL图像过滤器的问题我一直在努力工作。
之前我写了很多这些(Sobel边缘检测,自动分割等),所以我我知道怎么做,但下面的代码给了我一些真的奇怪的输出:

I'm having an issue with an OpenCL image filter I've been trying to get working.I've written many of these before (Sobel Edge Detection, Auto Segmentation, and such), so I thought I knew how to do this, but the following code is giving me some really weird output:

//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格式,用于创建纯蓝色(我之前没有问题就这样做过)。

So for testing, all I want to do is replace red pixels with blue ones, but this code will replace all matching pixels with WHITE ones.As far as I know, my formatting for blue is proper RGBA formatting for creating pure blue (I've done this before without issue).

我'使用PyOpenCL作为我的框架,我确保将源图像和目标图像的图像通道顺序设置为RGBA。此外,我还确保将源图像转换为RGBA格式(使用Python Imaging Library),如果它在运行内核之前尚未采用该格式。

I'm using PyOpenCL as my framework, and I've made sure to set the image channel order for both the source and destination images as RGBA. In addition, I've also made sure to convert the source image to RGBA format (using Python Imaging Library) if it was not already in that format before running the kernel on it.

我回过头来看看我写的其他内核,格式是一样的。我在这里错过了什么会导致它写出白色像素而不是蓝色像素?

I've gone back and looked at other kernels I've written, and the formatting is identical. What am I missing here that would cause it to write white pixels out instead of blue ones?

推荐答案

好的,所以我想我我已经明白了。出于某种原因,OpenCL不太热衷于让你按照我想要的方式编辑频道。我最后通过简单地添加或减去等效的float4向量来获得我想要的结果向量来解决它。

Okay, so I think I've figured it out. For some reason, OpenCL's not so keen on letting you edit the channels the way I wanted to. I ended up solving it by simply adding or subtracting equivalent float4 vectors to obtain the resultant vector I wanted.

__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 pix = read_imagef(srcImg, sampler, (int2)(imageCoordinate.x, imageCoordinate.y));

        //Full red channel, subtract this from original to remove red!
        float4 red = (float4)(1.0f, 0.0f, 0.0f, 0.0f);
        float4 blue = (float4)(0.0f, 0.0f, 1.0f, 0.0f);

    if (pix.x >= 0.9f && pix.y <= 0.1f && pix.z <= 0.1f) //If red, then replace with blue.
    {
        const float4 outColor = pix - red + blue;
        write_imagef(dstImg, imageCoordinate, outColor);
    }
    else
        write_imagef(dstImg, imageCoordinate, pix);

    }
}

所以在这种情况下,通过创建向量表示蓝色和红色(没有透明度)减去红色,然后添加蓝色,我获得了我想要的结果向量。就个人而言,我不确定为什么我必须这样做,但我很高兴我知道OpenCL希望我做什么,现在。希望如果其他人遇到这个问题,他们会在这里找到。

So in this case, by creating vectors to represent blue and red (without transparency) subtracting red, then adding blue, I obtain the resulting vector I wanted. Personally, I'm not sure why I have to do it this way, but I'm just glad I know what OpenCL expects me to do, now. Hopefully if someone else is having this problem, they'll find this here.

这篇关于OpenCL创建错误的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:05