Ask Your Question

Neamah's profile - activity

2015-07-16 14:01:45 -0500 commented answer OpenCV OpenCL- Where is creation of context?

Hi, thanks for the answer!

I've read through most of those links and they're pretty helpful in understanding how I can use Context, Kernel and Program classes to execute my own kernels.

What I'm more curious about is how OpenCV runs its own kernels. The best example I can give is cvtColor. Color conversions are one area that OpenCV has enabled OpenCL for. Reading through the code, I see how the cv::cvtColor function decides whether to use the CPU implementation or the GPU implementation.

Once it decides to use GPU implementation, it calls the function ocl_cvtColor. My question: inside this function, there is no mention of a context being created. Where does OpenCV do this setup? Do they do it at all? Is OpenCV creating and running a kernel without a context??

2015-07-15 12:33:09 -0500 answered a question Weird behavior of ocl matrix in opencv

The printf probably makes the kernel fail to compile and defaults to using the CPU. This happened to me when I tried adding a printf statement. There's a certain way printf's need to be added. Check this link out: https://www.khronos.org/registry/cl/e...

Again, check to make sure after adding printf that your code is ACTUALLY calling the kernel successfully.

2015-07-15 12:30:33 -0500 asked a question OpenCV OpenCL- Where is creation of context?

I'm using OpenCV OpenCL and I've been tracing the code that gets executed when OpenCL is on. I see the creation of a kernel, filling the kernel args and running the kernel, but I see no setup in terms of creating a context and program. Where does this happen, if at all?

For example, in /modules/imgproc/src/color.cpp, I see that cv::cvtColor calls ocl_cvtColor. Inside ocl_cvtColor, I see how the kernel is set up and run, but there isn't any mention of a context. I thought a context is necessary for OpenCL programs to run on the GPU.

2015-07-09 23:49:39 -0500 asked a question Enable OpenCV-OpenCL on Android devices

I'm trying to figure out how to enable OpenCL on an Android device.

I've built opencv so that OpenCL is YES. I've tried some functions like cv::ocl::setUseOpenCV(true) and haveOpenCL() and they all seem to indicate that OpenCL kernels are now available to use through calls to OpenCV.

I'm trying to convert the following code so that it runs on the GPU with OpenCV. I'm just trying to convert an image from the camera on an android phone to one that is RGB. That's all. (Ignore the line numbering).

BEFORE:

JNIEXPORT void JNICALL Java_com_me_main_Activity_toRGB(
 JNIEnv *env, jobject obj, jbyteArray yuv, int width, int height,
 jintArray dst) {
     std::stringstream logd;

    jbyte *_yuv      = env->GetByteArrayElements(yuv, 0);
    jint *_dst      = env->GetIntArrayElements(dst, 0);

    cv::Mat myuv(height + height/2, width, CV_8UC1, (uchar *)_yuv);
    cv::Mat mdst(height, width, CV_8UC4, (uchar *)_dst);

    cv::Mat mrgb;

    cv::cvtColor(myuv, mrgb, CV_YUV420sp2RGB, 4);

    mdst.create(mrgb.size(), mrgb.type() );

    mrgb.copyTo(mdst);

   env->ReleaseByteArrayElements(yuv, _yuv, 0);
   env->ReleaseIntArrayElements(dst, _dst, 0);

   LOGD(TAG, logd);
 }

AFTER:

JNIEXPORT void JNICALL Java_com_me_main_Activity_toRGB(
JNIEnv *env, jobject obj, jbyteArray yuv, int width, int height,
jintArray dst)
{
  std::stringstream logd;

  jbyte *_yuv      = env->GetByteArrayElements(yuv, 0);
  jint *_dst      = env->GetIntArrayElements(dst, 0);

  cv::Mat myuv(height + height/2, width, CV_8UC1, (uchar *)_yuv);
  cv::Mat mdst(height, width, CV_8UC4, (uchar *)_dst);
  //UMats for OpenCL use
  cv::UMat umyuv = myuv.getUMat(cv::ACCESS_READ, cv::USAGE_ALLOCATE_DEVICE_MEMORY);
  cv::UMat umdst = mdst.getUMat(cv::ACCESS_WRITE, cv::USAGE_ALLOCATE_DEVICE_MEMORY);

  cv::Mat mrgb;
  //UMats for OpenCL use
  cv::UMat umrgb;

  cv::cvtColor(umyuv, umrgb, CV_YUV420sp2RGB, 4);

  umdst.create(umrgb.size(), umrgb.type() );

  umrgb.copyTo(umdst);
  umdst.copyTo(mdst); //Because this holds the pointer to where our destination data needs to go

  env->ReleaseByteArrayElements(yuv, _yuv, 0);
  env->ReleaseIntArrayElements(dst, _dst, 0);

  LOGD(TAG, logd);
}

I get a black screen. Any idea why?