Convert cv::UMat to cv::ogl::Texture2D?

asked 2017-12-14 02:39:20 -0500

mr.s.h.h gravatar image

I want to use opencl and opengl together.

I have a cv::UMat and i want to convert it to cv::ogl::Texture2D in order to bind and render with opengl.

I have already a code that is worked fine but it takes too much of time:

cv::UMat _umat; // initialized before (a 1920*1080 image)
cv::ogl::Texture2D tex(_umat); // it takes about 14 millisecond on my machine (cpu: corei7, gpu: GTX-1080)
tex.bind();
// opengl code ...
// ...

Is there another way to do it more efficient?? It seems that the reason it consumes too much of time is that the data is copied but we should convert it.

There is another function in opencv : void convertToGLTexture2D(InputArray src, Texture2D& texture).

I have tried it but its not workd or maybe I do not know how to use it. I used it like this:

cv::ogl::ocl::initializeContextFromGL();
cv::ogl::Texture2D tex;
tex.create(1080,1920, cv::ogl::Texture2D::RGB);
cv::ogl::convertToGLTexture2D(_umat, tex);

When I run it, I encounter a segmentation fault:

OpenCV Error: Unknown error code -220 (OpenCL: clEnqueueAcquireGLObjects failed) in convertToGLTexture2D

Could you help me?

edit retag flag offensive close merge delete

Comments

Have you tried doing a copy to GPU RAM using:

Mat image = imread("dot.png");
cvtColor(image, image, CV_BGR2RGB);
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.cols, image.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, image.data);

See how long it takes.

sjhalayka gravatar imagesjhalayka ( 2017-12-14 12:00:17 -0500 )edit

I already did this and it took about 7 milliseconds. But the point to be considered is that I have UMat. I want to convert cv::UMat data to opengl texture directly (without converting to cv::Mat). converting UMat to Mat is a time-consuming job. You can follow this on stackoverflow

mr.s.h.h gravatar imagemr.s.h.h ( 2017-12-17 04:28:03 -0500 )edit