0

我有这个简单的OpenCL代码块,我得到了意想不到的结果。参数image是一个浮点数组,value是一个从 -255 到 +255 的数字。使用 java 我使用 JSlider 来更改value. 默认值为 0,问题是当我将滑块移动到 0 以上时,图像是黑色的,如果我将滑块移动到小于 0 时,图像是白色的,这不应该发生。这应该单独检查每个像素并调整该像素。似乎不是出于某种原因。

此代码块应更改图像的阈值。红色绿色和蓝色大于阈值的任何像素都应该是白色,否则应该是黑色。

kernel void threshold(global float* image, const float value, const int max){
    int index = get_global_id(0);
    if (index >= max){
        return;
    }

    int color = image[index];
    int red = color >> 16 & 0x0FF;
    int green = color >> 8 & 0x0FF;
    int blue = color & 0x0FF;

    if(red > value && green > value && blue > value){
        red = 255;
        green = 255;
        blue = 255;
    }else{
        red = 0;
        green = 0;
        blue = 0;
    }

    int rgba = 255;
    rgba = (rgba << 8) + red;
    rgba = (rgba << 8) + green;
    rgba = (rgba << 8) + blue;

    image[index] = rgba;
}

如果我用这个替换if/else中间的语句:

red += value;
if(red > 255){red = 255;}
else if(red < 0){red = 0;}

green += value;
if(green > 255){green = 255;}
else if(green < 0){green = 0;}

blue += value;
if(blue > 255){blue = 255;}
else if(blue < 0){blue = 0;}

对于该操作,我得到了我正在寻找的结果,即调整图像的亮度。

我用OpenCL错了吗?据我了解, iskernel将被调用,直到返回被击中。我在 java 中使用 JOCL 来执行此操作,这是我用来调用它的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pocketshop.graphics;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opencl.CLBuffer;
import com.jogamp.opencl.CLCommandQueue;
import com.jogamp.opencl.CLContext;
import com.jogamp.opencl.CLKernel;
import com.jogamp.opencl.CLPlatform;
import com.jogamp.opencl.CLProgram;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.nio.FloatBuffer;
import pocketshop.Canvas;
import pocketshop.dialogs.BrightnessContrastDialog;

/**
 *
 * @author Ryan
 */
public class CL {

    protected static CLBuffer<FloatBuffer> buffer;
    protected static float[] pixels;

    public static CLBuffer<FloatBuffer> getBuffer() {
        return buffer;
    }

    public static float[] getPixels() {
        return pixels;
    }

    public static void start(String script, float val) {

        CLPlatform platform = CLPlatform.getDefault(/*type(CPU)*/);
        CLContext context = CLContext.create(platform.getMaxFlopsDevice());
        try {
            CLProgram program = context.createProgram(getStreamFor("../scripts/" + script + ".cl"));
            program.build(CLProgram.CompilerOptions.FAST_RELAXED_MATH);
            assert program.isExecutable();

            BufferedImage image = Canvas.image;
            assert image.getColorModel().getNumComponents() == 3;

            pixels = image.getRaster().getPixels(0, 0, image.getWidth(), image.getHeight(), (float[]) null);
            FloatBuffer fb = Buffers.newDirectFloatBuffer(pixels);

            // allocate a OpenCL buffer using the direct fb as working copy
            buffer = context.createBuffer(fb, CLBuffer.Mem.READ_WRITE);

            // creade a command queue with benchmarking flag set
            CLCommandQueue queue = context.getDevices()[0].createCommandQueue(CLCommandQueue.Mode.PROFILING_MODE);

            int localWorkSize = queue.getDevice().getMaxWorkGroupSize(); // Local work size dimensions
            int globalWorkSize = roundUp(localWorkSize, fb.capacity());  // rounded up to the nearest multiple of the localWorkSize

            // create kernel and set function parameters
            CLKernel kernel = program.createCLKernel(script.toLowerCase());
            //adjustment(val, queue, kernel, buffer, localWorkSize, globalWorkSize);

            kernel.putArg(buffer).putArg((float) val).putArg(buffer.getNIOSize()).rewind();
            queue.putWriteBuffer(buffer, false);
            queue.put1DRangeKernel(kernel, 0, globalWorkSize, localWorkSize);
            queue.putReadBuffer(buffer, true);

        } catch (IOException e) {
        }
        context.release();
    }

    private static InputStream getStreamFor(String filename) {
        return BrightnessContrastDialog.class.getResourceAsStream(filename);
    }

    private static int roundUp(int groupSize, int globalSize) {
        int r = globalSize % groupSize;
        if (r == 0) {
            return globalSize;
        } else {
            return globalSize + groupSize - r;
        }
    }
}
4

2 回答 2

1

经过多次反复试验后我发现,这int color = image[index];实际上是红绿色或蓝色,而不是 3 中的 int。

所以,

image[0] = Red;
image[1] = Green;
image[2] = Blue;
image[3] = Red;
image[4] = Green;
image[5] = Blue;

ETC

于 2012-11-19T02:51:43.200 回答
0

你确定你的图像是浮动数组吗?如果是,那么你不应该对其进行整数操作

如果它是每个组件一个字节的 rgba,请尝试使用 uchar4 数据类型,也许这更合适,您可以对所有 4 个组件并行执行向量操作。

还尝试使用 opencl 的钳位功能来强制范围 0..255

于 2012-11-19T06:44:00.130 回答