This tutorial will guide you through how to build and use gpu module of OpenCV version 2.4.6 with Microsoft Visual Studio. I have attached a sample source file for Microsoft Visual C++ that simply performs color conversion on GPU using OpenCV. You can download the source file at the end of this post.

OpenCV's  GPU module is a set of classes and functions to utilize computational capabilities of NVIDIA's CUDA capable GPUs. To do so we can either use the pre-built binaries shipped with OpenCV, or we can compile it from scratch. In this tutorial, we will use the latter approach. To compile OpenCV with GPU support, we need A C++ compiler (Microsoft Visual C++, MinGW etc), CUDA toolkit and CMake. For today's tutorial we're  using Microsoft Visual Studio 2010, CUDA Toolkit 5.0, and CMake 2.8.11 and Windows 7 as platform.

Once you download, extract the OpenCV for Windows package in a folder. For this tutorial the directory is "D:\opencv" and we call it the Root Directory. Making sure you have the administrative rights to the extracted folder, launch CMake GUI, click on Browse Source and select the root directory.

Create a new folder (say opencv_build) in the Root Directory, where the binaries will be built. We call it the Build Directory. Click on Browse Build and select the Build Directory.

CMake Browse Build

Click on ConfigureSelect a compiler from the drop down list, in our case "Visual Studio 10". This will generate 32 bit OpenCV binaries. If you want to generate 64 bit binaries, select "Visual Studio 10 Win64" instead. Select "Use Default Native Compilers" and click finish. CMake will take some time to search for the compilers and libraries required to build OpenCV. After that, a list of CMake options will be displayed which can be changed to customize the compilation.

Make sure BUILD_opencv_gpu and WITH_CUDA are checked.CMake will automatically find the CUDA Toolkit root directory, however, we can also specify another toolkit version by changing CUDA_TOOLKIT_ROOT_DIR to point to another directory. Note that any change in the CMake options will require us to press the Configure button again. After finalizing the options for compilation, click Generate. CMake will generate a CUDA/C++ project of the specified compiler in the Build Directory.

After the project has been generated, exit CMake and open the Build Directory. Open the project named OpenCV. Select Release configuration and simply build the project. The compilation will take a considerable amount of time. Ignore any warning messages shown during the compilation. The following output will be generated after successful compilation:

lib files in "D:\opencv\opencv_build\lib\Release" directory.

dll files in "D:\opencv\opencv_build\bin\Release" directory.

Cleaning up: Except the lib and bin folders, delete all the other files and folders inside the Build Directory.

Since header files remain same for any kind of OpenCV build, just copy the include folder shipped in opencv\build\ to our opencv_build folder. You, at this point, have successfully build OpenCV's GPU module on your machine. Your build folder must have three sub folders namely; bin, lib and include. We recommend creating environment variables for these three folders in your system for ease of use.

bgr to gray

If you're having trouble compiling and running the example program kindly refer to this: Step by Step Guide on Running OpenCV in Microsoft Visual Studio

Downloadcv_gpu_example.zip