私は WPF - C# プロジェクトを持っており、そこで行っている画像処理を高速化しようとしています。OpenCL と Cloo を使用して実行します。
ビットマップをグレースケールに変更できるカーネルを動作させることができましたが、何らかの理由で、作成した白黒カーネルは完全に黒の画像しか出力しません。
ここに私のカーネル.cl
コードがあります:
kernel void ConvertToBlackAndWhite(read_only image2d_t sourceImage, write_only image2d_t outputImage, float threshold)
{
const sampler_t mainSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;
int2 position = (int2)(get_global_id(0), get_global_id(1));
float4 pixel = read_imagef(sourceImage, mainSampler, position);
float grayValue = (0.299 * pixel.z) + (0.587 * pixel.y) + (0.114 * pixel.x); // Get Grayscale Value
const float thresholdValue = threshold * 255.0;
if (grayValue > thresholdValue)
{
const float4 white = (float4){ 255.0, 255.0, 255.0, 255.0 }; // #FFFFFF
write_imagef(outputImage, position, white);
}
else
{
const float4 black = (float4){ 0.0, 0.0, 0.0, 255.0 }; // #000000
write_imagef(outputImage, position, black);
}
}
どこが間違っていますか?
ヘルプやヒントをいただければ幸いです。
@ProjectPhysX の役立つアドバイスに基づいて、カーネル コードを更新しました。
kernel void ConvertToBlackAndWhite(read_only image2d_t sourceImage, write_only image2d_t outputImage, float threshold)
{
const sampler_t mainSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;
int2 position = (int2)(get_global_id(0), get_global_id(1));
float4 pixel = read_imagef(sourceImage, mainSampler, position);
float grayValue = (0.299f * pixel.z) + (0.587f * pixel.y) + (0.114f * pixel.x); // Get Grayscale Value
if (grayValue > threshold)
{
const float4 white = (float4){ 1.0f, 1.0f, 1.0f, 1.0f }; // #FFFFFF
write_imagef(outputImage, position, white);
}
else
{
const float4 black = (float4){ 0.0f, 0.0f, 0.0f, 1.0f }; // #000000
write_imagef(outputImage, position, black);
}
}
画像を OpenCL 画像メモリ バッファーに追加する C# は次のとおりです。
...
// Allocate OpenCL Image Memory Buffer
inputImage2DBuffer = Cl.CreateImage2D(GPUManipulation.OpenCLContext, MemFlags.CopyHostPtr | MemFlags.ReadOnly, imageFormat,
(IntPtr)width, (IntPtr)height,
(IntPtr)ConstantsMain.Int0, inputByteArray, out error);
...
// Allocate OpenCL image memory buffer
IMem outputImage2DBuffer = Cl.CreateImage2D(GPUManipulation.OpenCLContext, MemFlags.CopyHostPtr | MemFlags.WriteOnly, imageFormat,
(IntPtr)width, (IntPtr)height, (IntPtr)ConstantsMain.Int0, outputByteArray, out error);
...