我正在尝试实现一个OpenCL项目,它应该输入两个“灰色”通道图像并返回一个“灰色”通道缓冲区
我的OpenCL代码:
__kernel void motion_detection(__global uchar* bgImage, __global uchar* motionImage,__global uchar* outputImage)
{
int i = get_global_id(0);
int tsh = 7;
uchar bgEl = bgImage[i];
uchar motionEl = motionImage[i];
float bg = convert_float(bgEl);
float mo = convert_float(motionEl);
float temp = fabs(bg-mo);
if(temp>tsh){temp=255.0;
}else
{temp=0.0;
}
outputImage[i] = convert_uchar(bg);
}
我的主人:
// get width and height of input image
height = inputImage->rows;
width = inputImage->cols;
// get the pointer to pixel data
cl_uchar* inputImageData = inputImage->data;
cl_uchar* motionImageData = motionImage->data;
cl_uchar* outputImageData = outputImage->data;
设置缓冲区后:
// Create memory object for input Image
inputImageBuffer = clCreateBuffer(
context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
width * height * greyPixelSize,
inputImageData,
&status);
CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (inputImageBuffer)");
motionImageBuffer = clCreateBuffer(
context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
width * height * greyPixelSize,
motionImageData,
&status);
CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (inputImageBuffer)");
// Create memory objects for output Image
outputImageBuffer = clCreateBuffer(
context,
CL_MEM_READ_WRITE,
width * height * greyPixelSize,
0,
&status);
设置参数并执行OpenCL内核:
// input buffer image
status = clSetKernelArg(
kernl,
0,
sizeof(cl_mem),
&inputImageBuffer);
CHECK_OPENCL_ERROR(status, "clSetKernelArg failed. (inputImageBuffer)");
status = clSetKernelArg(
kernl,
1,
sizeof(cl_mem),
&motionImageBuffer);
CHECK_OPENCL_ERROR(status, "clSetKernelArg failed. (inputImageBuffer)");
// outBuffer imager
status = clSetKernelArg(
kernl,
2,
sizeof(cl_mem),
&outputImageBuffer);
CHECK_OPENCL_ERROR(status, "clSetKernelArg failed. (outputImageBuffer)");
status = clEnqueueNDRangeKernel(
commandQueue,
kernl,
1,
NULL,
globalThreads,
localThreads,
0,
NULL,
&ndrEvt);
clFinish(commandQueue);
这是我的问题,当执行此指令时,它将outputImageData返回为“\ 0”而不是其他内容:
status = clEnqueueReadBuffer(
commandQueue,
outputImageBuffer,
CL_TRUE,
0,
width * height * greyPixelSize,
outputImageData,
0,
NULL,
NULL);
CHECK_OPENCL_ERROR(status, "clEnqueueReadBuffer failed.");