Ask Your Question

accelerate OpenCV functions on gpu through CUDA

asked 2020-07-23 07:03:40 -0600

jvincent gravatar image

Dear all,

I'm working on an image procession python algorithm using openCV. Since I have a lot of images to proccess, I'm trying to accelerate it on a NVIDIA Jetson card using CUDA. During the last two months, I've tried many and many different solutions found on the Internet.

The best solution I've found is to translate my python algorithm in c++, and then use the accelerated functions provided in cv::cuda.

Is it the fastest solution ??

But I still need to accelerate functions (as cv::undistort tody, but it could be others in a near future).

How could I accelerate Opencv functions using CUDA ??

I'm actually working on building my own accelerate version of the undistort function from source code, but it is more complex than I though

How can I easily build an opencv function after modification ??


edit retag flag offensive close merge delete


Why not use the Python API directly instead of translating to C++? By accelerate OpenCv functions using CUDA do you mean, how can you write CUDA kernel's? Is there anyway you can overlap processing on the GPU with functions on the host which don't have OpenCV CUDA implementations?

cudawarped gravatar imagecudawarped ( 2020-07-23 07:51:12 -0600 )edit

Since opencv is implemented in c and c++, its easier for me to implement my whole code in thoses languages in case I have to modify some opencv functions. Using opencv functions using cuda means that I use functions from cv::cuda, giving them GpuMat in arguments. I could try to overlap, but the host functions are like 6 or 7 times slower than the GPU ones, so overlaping corresponds to being restricted by the CPUs

jvincent gravatar imagejvincent ( 2020-07-24 04:16:14 -0600 )edit

OK so what is your question, how to implement cv::undistort in CUDA?

cudawarped gravatar imagecudawarped ( 2020-07-24 05:03:44 -0600 )edit

how to implement non accelerated opencv functions on CUDA ?

jvincent gravatar imagejvincent ( 2020-07-27 03:29:31 -0600 )edit

I was hoping that wasn't going to be your question. My answer unfortunately is that:

  1. You would need a basic understanding of the Single Instruction Multiple Thread (SIMT) execution model used by CUDA to decide whether the undistort function can be/is worth accelerating on the GPU.
  2. To do the implementation you would need to learn how to program in CUDA.

Then including in OpenCV should be relatively straight forward as all the API's are there to access and manipulate the GPU's memory.

cudawarped gravatar imagecudawarped ( 2020-07-27 10:05:41 -0600 )edit

Thank you I think I have a lot of work now

jvincent gravatar imagejvincent ( 2020-07-28 08:19:41 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-07-23 07:44:08 -0600

LauLauThom gravatar image

As far as I know you would need to compile the opencv python bindings from source to activate the CUDA support. This is rather technical unless you are used to compiling stuff.
Alternatively instead of CUDA you can use OpenCl to run code on GPU which is activated by default in opencv releases.
Just convert your Mat objects into UMat using the cv2.UMat(Mat) command.
Then the opencv functions will automatically run on the gpu using opencl.
See this blog post

Keep in mind that you might not always see a speed up depending on the size of the images and the operations.

edit flag offensive delete link more


You have to compile from src regardless if you want to use the cv::cuda API. Not all functions have an OpenCl implementation so the acceleration will depend on that plus the size of the images.

cudawarped gravatar imagecudawarped ( 2020-07-23 08:03:36 -0600 )edit

I've already tried to use opencl and umat, but it is not faster enough for my images. Moreover, I want to remain free to be able to modify a opencv function for my own utilisation

jvincent gravatar imagejvincent ( 2020-07-24 04:19:14 -0600 )edit

Question Tools

1 follower


Asked: 2020-07-23 07:03:40 -0600

Seen: 2,395 times

Last updated: Jul 23 '20