0

この単純なコードブロックがありOpenCL、予期しない結果が得られています。パラメータimageはfloatの配列であり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間違って使用していますか?私が理解していることkernelから、リターンがヒットするまで呼び出されます。これを行うために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の整数ではなく、赤、緑、または青の色であることがわかりました。

それで、

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

于 2012-11-19T02:51:43.200 に答える
0

画像がフロートの配列であると確信していますか?はいの場合、整数操作を行うべきではありません

コンポーネントごとに1バイトのrgbaの場合は、uchar4データ型を使用してみてください。おそらくそれがより適切であり、4つのコンポーネントすべてに対して並列でベクトル演算を実行できます。

また、openclのクランプ機能を使用して範囲0..255を適用してみてください

于 2012-11-19T06:44:00.130 に答える