Ask Your Question

What's causing the assertion fail in cv::ocl::convertFromImage

asked 2015-09-04 14:42:39 -0500

biaspoint gravatar image

I'm getting an assertion fail in the last assertion in ocl.cpp (line 5466?),

OpenCV Error: Assertion failed (clEnqueueCopyImageToBuffer(q, clImage, clBuffer, src_origin, region, offset, 0, NULL, NULL) == CL_SUCCESS) in convertFromImage, file /home/xxxxx/opencv-3.0.0/modules/core/src/ocl.cpp, line 5466

This line:

CV_Assert(clEnqueueCopyImageToBuffer(q, clImage, clBuffer, src_origin, region, offset, 0, NULL, NULL) == CL_SUCCESS);

which I would have thought it wouldn't have made it all the way through the function and then fault there. Here is the code, I'm probably mixing up the library notation somehow.

int main()
cv::Mat mat(480, 640, CV_32FC4);
cv::imshow("CVforCLimage", mat);

cl_platform_id platform;
cl_device_id device;
cl_context context;
cl_int err;
clGetPlatformIDs(1, &platform, NULL);
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);

cl_mem IDKmem;
cl_image_format tif_format;
tif_format.image_channel_order =   CL_RGBA;
tif_format.image_channel_data_type =   CL_FLOAT;

IDKmem = clCreateImage2D(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,&tif_format,mat.cols, mat.rows, 0,,&err);
cv::UMat Umat;
cv::ocl::convertFromImage(IDKmem, Umat);
cv::imshow("CVforCLimage", Umat);

return 0;

The function convertFromImage is causing the problem, I tried to make it as basic as I could to try and understand the numenclautre. Thanks!

edit retag flag offensive close merge delete



I'm not a guru at all of opencl but you are a creatiing an opencl context and after you ask opencv to use opencl. But I think there is already a context in opencv

Instead of creating your own context can you use opencv context.

LBerger gravatar imageLBerger ( 2015-09-04 15:11:37 -0500 )edit

I'll give it a shot, thanks! The one problem I see (and yes, it is a problem of my own doing), is that I have combined the C++ of OpenCV with the C of OpenCL. Granted I can use the C++ wrapper for OpenCL but I have not gotten that smart yet! Thanks!

biaspoint gravatar imagebiaspoint ( 2015-09-07 19:11:05 -0500 )edit

I tried it this way and got a segmentation fault instead of an assertion fail, so I assume I can not use the OpenCL C++ to create the cl::image2D

cl::Platform platform;
vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
cl::Context context(devices[0]);

cl::ImageFormat format (CL_RGBA, CL_FLOAT);
cl::Image2D IDKmem(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, format, mat.cols, mat.rows, 0,,&err);

cv::UMat Umat;
cv::ocl::convertFromImage(&IDKmem, Umat);
biaspoint gravatar imagebiaspoint ( 2015-09-07 19:47:26 -0500 )edit

I also tried:

    cv::ocl::Context cvContext;
    cl::Image2D IDKmem(cvContext, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, format, mat.cols, mat.rows, 0,,&err);

and I got "no matching function for call to cl::Image2d::Image2D(cv::ocl::Context&, ... error

biaspoint gravatar imagebiaspoint ( 2015-09-07 19:51:17 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-09-09 00:25:12 -0500

biaspoint gravatar image

It turns out that LBerger was on the right track. You have to use

cv::ocl::attachContext(PlatformName, platforms[0], context(), devices[0]());  // C++

or for C code of OpenCL:

cv::ocl::attachContext(PlatformName, platform, context, device);   // C

to use the context created by OpenCL, I was able to do it using the c++ code from my comment and I had to borrow some code to get the first 2 values, I'm sure there is a more efficient method (and one that doesn't assume you only have 1 platform):

// C Code Changes from my original Question
    cl_image_desc desc_src;
    desc_src.image_type        = CL_MEM_OBJECT_IMAGE2D;
    desc_src.image_width       = mat.cols;
    desc_src.image_height      = mat.rows;
    desc_src.image_depth       = 0;
    desc_src.image_array_size  = 0;
    desc_src.image_row_pitch   = mat.step[0];
    desc_src.image_slice_pitch = 0;
    desc_src.num_mip_levels    = 0;
    desc_src.num_samples       = 0;
    desc_src.buffer            = 0;

    size_t sz = 0;
    clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, 0, &sz);
    char PlatformName[sz+1];
    clGetPlatformInfo(platform, CL_PLATFORM_NAME, sz, PlatformName, 0);
    cv::ocl::attachContext(PlatformName, platform, context, device);
    IDKmem = clCreateImage(context, CL_MEM_READ_WRITE |  CL_MEM_COPY_HOST_PTR,&tif_format,&desc_src,mat.ptr(),&err);

Hope that helps someone else.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-09-04 14:42:39 -0500

Seen: 797 times

Last updated: Sep 09 '15