# Processing image stack with CUDA function

**URL:** https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316
**Category:** Uncategorized
**Tags:** cuda
**Created:** [May 13, 2021, 4:17am UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316 "2021-05-13T04:17:15Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![grsabourin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/grsabourin/32/1068_2.png) [@grsabourin](https://forum.opencv.org/u/grsabourin)
#### Post date: [May 13, 2021, 4:17am UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/1 "2021-05-13T04:17:15Z")

</div>

Hi,

I have a stack of images (DICOM) that I would like to apply CUDA version of fastNlMeansDenoising filter. Is someone can suggest a good example to perform this operation. I’m looking to process stack as a batch because applying CUDA function on a single image one after the other is not efficient and longer than using CPU.

Thank you

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [May 13, 2021, 6:46am UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/2 "2021-05-13T06:46:23Z")

</div>

i’m having some doubts, if this is possible.

[the batch version](https://docs.opencv.org/master/d1/d79/group __photo__ denoise.html#ga21abc1c8b0e15f78cd3eff672cb6c476) only applies to CPU

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [May 13, 2021, 9:00am UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/3 "2021-05-13T09:00:56Z")

</div>

Hi, when you say not efficient do you mean that it does not take advantage of the dependency between consecutive frames which I assume `fastNlMeansDenoisingMulti()` does or do you just mean it is slower than the CPU version? If it is the latter you may be able to optimize your code.

---

<div class="post-metadata">

### Author: ![grsabourin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/grsabourin/32/1068_2.png) [@grsabourin](https://forum.opencv.org/u/grsabourin)
#### Post date: [May 13, 2021, 1:01pm UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/4 "2021-05-13T13:01:17Z")

</div>

My experience with fastNlMeansDenoisingMulti() on GPU is it takes more time to process single image on GPU than on a CPU. My job is to process between 500 to 1000 images. I see my problem as doing processing on a video stream. Can I have a gain to do it as a video stream?

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [May 13, 2021, 1:04pm UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/5 "2021-05-13T13:04:58Z")

</div>

How are you timing that, are you including the upload/download to the GPU in the timing? Which GPU/CPU are you comparing? Are you timing a single call, the time for the first run on the GPU is always orders of magnitude greater than subsequent ones? Are you using C++ or python?

---

<div class="post-metadata">

### Author: ![grsabourin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/grsabourin/32/1068_2.png) [@grsabourin](https://forum.opencv.org/u/grsabourin)
#### Post date: [May 13, 2021, 1:25pm UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/6 "2021-05-13T13:25:17Z")

</div>

Yes my timing for now is on a single call. Do you think is I do a loop on my stack it can be faster? Latest test were on a I9 CPU and a Quadro T2000 for GPU

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [May 13, 2021, 1:38pm UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/7 "2021-05-13T13:38:34Z")

</div>

You may find this post useful

> [@OpenCV CUDA extremely slow](https://forum.opencv.org/t/opencv-cuda-extremely-slow/3050):
>
> I made some tests comparing OpenCV performance with some basic operations with or without CUDA. I just threw in a few simple operators: greyscale conversion, thresholding, morphological operators, resizing. To my surprise, the CUDA code was 50-60 times slower than the CPU!!! I tested on my laptop (core i7 vs GeForce MX130) and on a Nvidia Nano (ARM CPU) with similar results. CUDA code took 0.6 sec on my laptop, which is really a lot for a 5MP image. CUDA 10.1/10.2 was used, and OpenCV 4.5.2 w…

Essentially there could be a lot of things at play but I would first try timing the second or third run on the GPU as it should be substantially faster than the first.

Additionally your GPU is on the lower end of Nvidia’s line up. For example an RTX 3080 has ~8x the floating point performance of your card (that’s crazy when compared to a CPU, I would really struggle to find something with 1/8 the performance of an i9). I mention this because you may not see such a great performance increase in going from a top end Intel chip to a low end Nvidia GPU.

---

<div class="post-metadata">

### Author: ![grsabourin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/grsabourin/32/1068_2.png) [@grsabourin](https://forum.opencv.org/u/grsabourin)
#### Post date: [May 13, 2021, 1:45pm UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/8 "2021-05-13T13:45:57Z")

</div>

Thank you for reply. The features I gave you is about my development system. The system that is targeted has other type of CPU with probably less performances.

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [May 13, 2021, 3:49pm UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/9 "2021-05-13T15:49:27Z")

</div>

some material on cuda streams, which is _not_ necessarily beginner-friendly, so feel free to ignore it, or browse out of curiosity:

- [https://developer.download.nvidia.com/CUDA/training/StreamsAndConcurrencyWebinar.pdf](https://developer.download.nvidia.com/CUDA/training/StreamsAndConcurrencyWebinar.pdf)
- [https://on-demand.gputechconf.com/gtc/2014/presentations/S4158-cuda-streams-best-practices-common-pitfalls.pdf](https://on-demand.gputechconf.com/gtc/2014/presentations/S4158-cuda-streams-best-practices-common-pitfalls.pdf)
- [https://developer.nvidia.com/blog/gpu-pro-tip-cuda-7-streams-simplify-concurrency/](https://developer.nvidia.com/blog/gpu-pro-tip-cuda-7-streams-simplify-concurrency/)

according to [this](https://www.techpowerup.com/gpu-specs/quadro-t2000-mobile.c3434), a Quadro T2000 has 3.6 Tflop/s of FP32, so that’s still orders of magnitude better than a CPU.

( @cudawarped please don’t compare to the latest and greatest. newbies lack the context to understand such judgments. this hardware is plenty powerful. it’s absolutely not the issue here. )

your whole problem is not realizing that transfer of data and commands takes **time**.

getting a GPU to perform is all about dealing with latency.

those are the basics.

please don’t jump _into_ GPU programming. your issues will be tiresome because they wouldn’t come up with a proper approach.

start with the basics of GPU programming. that means learning what’s special about GPUs and what to pay attention to.

you _do not_ start by coming up with your own questions and poking around for answers. you look for structured teaching. get experts to _tell_ you what you need to know. as a newbie you _can’t_ know what you need to know, and you aren’t expected to.

hell, nvidia has tons of documentation and tutorials and introductions. they wouldn’t make any money if their educational content sucked. please go and look for good learning material.

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [May 13, 2021, 4:15pm UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/10 "2021-05-13T16:15:25Z")

</div>

> [@crackwitz](#):
>
> ( @cudawarped please don’t compare to the latest and greatest. newbies lack the context to understand such judgments. this hardware is plenty powerful. it’s absolutely not the issue here. )

Fair enough, that was an aside the real culprits are detailed in the post I linked to. In my defense the latest i9 is the premium offering from Intel so I would argue that it is not fair to compare its performance to anything other than a premium offering from Nvidia, when determining if the GPU is suitable for your workload.

I would urge caution against comparing the Tflop/s of a CPU vs a GPU as this is highly algorithm dependent and even more so in OpenCV where a lot of the CUDA algorithms were written for depreciated compute capabilities (old), use the npp’s which can have poor performance, and some which have to examine GPU results on the CPU half way through to determine the next round of GPU computation.

---

<div class="post-metadata">

### Author: ![grsabourin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/grsabourin/32/1068_2.png) [@grsabourin](https://forum.opencv.org/u/grsabourin)
#### Post date: [May 16, 2021, 11:28am UTC](https://forum.opencv.org/t/processing-image-stack-with-cuda-function/3316/11 "2021-05-16T11:28:31Z")

</div>

Thank you for all you answers. I had a first success with using a for loop and apply GPU processing successively. 88 seconds compare to 3.5 seconds on GPU with the same dataset. This is a good start.
