How to Use OpenCV With CUVI

From day 1 we have focused on making CUVI compatible with the existing Vision and Imaging libraries and what comes to mind right away is: OpenCV. We have been asked a many times whether CUVI functions work with OpenCV. Here’s a little tutorial on how you can use OpenCV’s Image reading and writing functions with CUVI functions.

I have shown how simple it becomes when you read an image using IplImage, feed that to CUVI’s RGB to Gray function and write back the result grayscale image using IplImage. All of this happens in just a couple of lines! And the best part is that you don’t need to know a bit about GPU programming because that is what CUVI will do for you. So let’s get started:

Step 1: Pre-requisites

  • CUVI (Download and install from here) (I am using CUVI 0.4 32bit)
  • OpenCV (I am using OpenCV v2.1)

Note: I have Visual Studio 2008 and CUDA Toolkit 3.2RC and my Graphics driver version is 260.99 but any driver you have will work and you don’t need to have CUDA toolkit installed since we distribute the core DLLs with CUVI’s distribution.

Note2: Here’s the list of additional dependencies that you need to feed Visual Studio’s Linker:

  • cv210.lib
  • cvaux210.lib
  • cxcore210.lib
  • highgui210.lib
  • cuvi32.lib

If you already have a OpenCV project then the only additional dependency is cuvi32.lib (or cuvi32D.lib for the debug version)

Step 2: The Code

Read the input image:

Specify the Region of Interest using the NppiSize struct:

Create an image to store the output. You’ll need to define dimensions using the CvSize struct:

Now you’re all set to call the CUVI’s GPU accelerated RGB_to_Gray function:

The first argument is the input image’s data pointer statically casted into unsigned char since the IplImage reads image data into char* and CUVI requires image data read into unsigned char*. Second argument is the input image’s pitch, third argument is output image’s data pointer, next is it’s pitch and the last argument is the input image’s ROI.

Now once you get the gray scale image as output, you can save it to the file system:

How easy could it get to add GPU acceleration into your existing applications? This is the most basic example I have quoted here but it suffices for almost all functions that CUVI has to offer e.g. you can try the same for CUVI’s Optical Flow (LK or HS) or CUVI’s DWT/iDWT etc.

You can download the source code for this example from here