# 2D cross correlation

**URL:** <https://forums.developer.nvidia.com/t/2d-cross-correlation/7233>\
**Category:** CUDA Programming and Performance\
**Created:** [January 14, 2009, 1:49am UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233 "2009-01-14T01:49:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![seddona](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@seddona](https://forums.developer.nvidia.com/u/seddona)\
**Post date:** [January 14, 2009, 1:49am UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/1 "2009-01-14T01:49:14Z")

</div>

Hi,

I’m looking to do 2D cross correlation on some image sets. Typical image resolution is VGA with maybe a 100x200 template. I’m looking into OpenVIDIA but it would appear to only support small templates. I was hoping somebody could comment on the availability of any libraries/example code for my task and if not perhaps the suitability of the task for GPU acceleration. I am currently using the intel IPP ippiCrossCorr function, but I’m hoping to accelerate with CUDA.

Thanks,

---

<div class="post-metadata">

**Author:** ![Smokey](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Smokey](https://forums.developer.nvidia.com/u/Smokey)\
**Post date:** [January 14, 2009, 2:28am UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/2 "2009-01-14T02:28:25Z")

</div>

> [@](#):
>
> Hi,
> 
> I’m looking to do 2D cross correlation on some image sets. Typical image resolution is VGA with maybe a 100x200 template. I’m looking into OpenVIDIA but it would appear to only support small templates. I was hoping somebody could comment on the availability of any libraries/example code for my task and if not perhaps the suitability of the task for GPU acceleration. I am currently using the intel IPP ippiCrossCorr function, but I’m hoping to accelerate with CUDA.
> 
> Thanks,

Speaking from personal experience, implementing a 2D normalised cross-correlation is relatively efficient - although I’m yet to get ‘significant’ performance improvements over IPP - depending on the case I’m anywhere between 20% faster to 20% slower (depending on pixel format of the image/template, and size of the template). So yes, you’d likely get increases from using CUDA.

As for existing libraries that implement this, I’m not aware of any…

---

<div class="post-metadata">

**Author:** ![seddona](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@seddona](https://forums.developer.nvidia.com/u/seddona)\
**Post date:** [January 14, 2009, 9:51am UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/3 "2009-01-14T09:51:39Z")

</div>

20% over IPP is a bit dissapointing. Have you any idea where the bottleneck is?

fyi my images are 8bit greyscale and the correlation dosn’t need to be normalised. I could in fact get away with sum of absolute differences, perhaps there is code for this as it’s a common video compression step.

---

<div class="post-metadata">

**Author:** ![papag62](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@papag62](https://forums.developer.nvidia.com/u/papag62)\
**Post date:** [January 14, 2009, 1:28pm UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/4 "2009-01-14T13:28:41Z")

</div>

Here is a stereo matching cuda kernel that uses SSD. It may be of use to you. It is from NVIDIA.  
[url=“[http://sourceforge.net/project/showfiles.php?group\_id=98913&package\_id=283815](http://sourceforge.net/project/showfiles.php?group_id=98913&package_id=283815)”][http://sourceforge.net/project/showfiles.p...ckage\_id=283815[/url]](http://sourceforge.net/project/showfiles.p...ckage_id=283815%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![bog](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bog](https://forums.developer.nvidia.com/u/bog)\
**Post date:** [January 20, 2009, 11:42am UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/5 "2009-01-20T11:42:36Z")

</div>

> [@](#):
>
> Here is a stereo matching cuda kernel that uses SSD. It may be of use to you. It is from NVIDIA.
> 
> http://sourceforge.net/project/showfiles.p…ckage\_id=283815

You could use the instruction: \_\_usad. See the documentation for this. It’s prety fast.

---

<div class="post-metadata">

**Author:** ![DaManu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@DaManu](https://forums.developer.nvidia.com/u/DaManu)\
**Post date:** [January 20, 2009, 7:36pm UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/6 "2009-01-20T19:36:15Z")

</div>

Perhaps look for another algo?  
I implemented a really fast 2d correlation function with Cuda’s FFT lib.  
Good old style: iFFT(FFT(image) \* FFT(what your looking for))  
Do normalization on phase, then search for maxima, then do a normalized traditional correlation on the result for ensuring ?

---

<div class="post-metadata">

**Author:** ![Smokey](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Smokey](https://forums.developer.nvidia.com/u/Smokey)\
**Post date:** [January 20, 2009, 11:13pm UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/7 "2009-01-20T23:13:18Z")

</div>

> [@](#):
>
> 20% over IPP is a bit dissapointing. Have you any idea where the bottleneck is?
> 
> fyi my images are 8bit greyscale and the correlation dosn’t need to be normalised. I could in fact get away with sum of absolute differences, perhaps there is code for this as it’s a common video compression step.

I should note, my kernels still have room for optimization - they’re currently processor bound (each thread has a loop which iterates (for my case, which is a 9x9 template against 30x30 source regions) 9\*9 times).

That kernel at the moment only has 66% occupancy, and I could do more tricks to share results between threads - a) giving me more processing power to work with, and B) reducing the kernel’s instruction count up to 9 times in this case).

I’m guessing I could probably get double the speed of IPP if it were a priority for me - and far more than double the speed for larger source/template sizes - however the cross correlation in my application isn’t the largest bottleneck - thus I haven’t given it that much attention.

---

<div class="post-metadata">

**Author:** ![seddona](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@seddona](https://forums.developer.nvidia.com/u/seddona)\
**Post date:** [March 11, 2009, 5:57pm UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/8 "2009-03-11T17:57:31Z")

</div>

> [@](#):
>
> Perhaps look for another algo?
> 
> I implemented a really fast 2d correlation function with Cuda’s FFT lib.
> 
> Good old style: iFFT(FFT(image) \* FFT(what your looking for))
> 
> Do normalization on phase, then search for maxima, then do a normalized traditional correlation on the result for ensuring ?

Hi DaManu, is there any chance of posting your code? If not what sort of performance are you seeing?

---

<div class="post-metadata">

**Author:** ![Ashutosh\_Gupta](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Ashutosh\_Gupta](https://forums.developer.nvidia.com/u/Ashutosh_Gupta)\
**Post date:** [January 20, 2011, 4:48am UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/9 "2011-01-20T04:48:28Z")

</div>

> [@](#):
>
> Hi DaManu, is there any chance of posting your code? If not what sort of performance are you seeing?

Hi, I have implemented NCC on GPU where; for smaller template and image size I am getting a 1.5~2X speedup &

for larger sizes a speedup of 6X is achievable. my observation says that if u do correlation on GPU and normalization on CPU, the results are good.

- small size : Image = 32X32 template = 16X16

- larger : Image=512X512 template = 64X64

using cuFFT.

---

<div class="post-metadata">

**Author:** ![RyuKa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@RyuKa](https://forums.developer.nvidia.com/u/RyuKa)\
**Post date:** [May 18, 2011, 12:05pm UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/10 "2011-05-18T12:05:12Z")

</div>

How can I implement the classical iFFT[FFT(images1)\*FFT(images2)] the easiest way ?

---

<div class="post-metadata">

**Author:** ![RyuKa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@RyuKa](https://forums.developer.nvidia.com/u/RyuKa)\
**Post date:** [May 19, 2011, 7:05am UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/11 "2011-05-19T07:05:28Z")

</div>

Another question :

does anyone have a link where a correlation is done with CUDA ?

---

<div class="post-metadata">

**Author:** ![HenrikAndresen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@HenrikAndresen](https://forums.developer.nvidia.com/u/HenrikAndresen)\
**Post date:** [May 19, 2011, 7:52am UTC](https://forums.developer.nvidia.com/t/2d-cross-correlation/7233/12 "2011-05-19T07:52:53Z")

</div>

> [@](#):
>
> Hi DaManu, is there any chance of posting your code? If not what sort of performance are you seeing?

The typical difference between correlation using time domain and that using frequency domain is going from On^2 to On log2(n), assuming you can zero-pad your data sizes to be of size 2^k.
