How to Build OpenCV 2.2 with GPU (CUDA) on Windows 7

OpenCV version 2.2 was released in December last year with GPU support. This GPU moduimagele was written in CUDA which means it’s hardware dependent (only NVIDIA CUDA enabled GPUs can make use of this module). It has opened the gateways of GPU accelerated Image Processing and Computer Vision available right in OpenCV. Using it can be a nightmare for most of you so I decided to log my way of making it work which is not very much different from what’s on the documentation with some added steps. 

I have broken down the whole procedure into the following steps. If you face problem and I am sure you will 😉 then kindly mention the step number, your CUDA Toolkit version, NPP version and your machine and Visual Studio version in the comment section below. Let’s get started:

Step 1: Pre-Requisites:

Even though you can build OpenCV 2.2 with GPU-Emulation mode, that is not recommended at all. Only try it if you have a CUDA enabled GPU. Here’s what I have on my machine:

  • GPU: GeForce 9800 GT
  • CUDA Toolkit version 3.2 (I have 4.0RC installed but I used 3.2 instead)
  • CUDA SDK version 3.2
  • NVIDIA NPP version 3.2 (Starting from CUDA 4.0, NPP is now part of the toolkit and not offered separately)
  • Visual Studio 2008 (You can make it work on VS2010 too)

And yes, you need to have:

  • OpenCV 2.2 (Download the Windows binaries from here)
    A small note on the release binary: It’s not the most updated version and since GPU module is in active development stage, It’s recommended that you download the latest source from OpenCV SVN. Also, the binary is not compiled with CUDA and hence the reason for writing this blog 🙂
  • CMake (download link) – This is a cross-compile tool with Windows shell available. You will need this to configure and generate the OpenCV.sln (Visual Studio solution file)

Step 2: Configuring and Generating OpenCV.sln solution file

Install OpenCV 2.2 and also CMake (I have CMake version 2.8) and then open CMake:

image

I have installed OpenCV 2.2 in “d:/Projects/OpenCV 2.2/” and I will refer to this as <OPENCV_INSTALL_DIR>.
Specify this directory in the first field which says “Where is the source code” and specify the directory where you want the OpenCV.sln solution file and everything else to be built. I have specified it as “d:/Projects/OpenCV 2.2/build” like this:

image 

Once this is done, click the Configure button. A dialog box will appear with a dropdown. Select “Visual Studio 9 2008” and click Finish:
image

A whole table of fields will appear displaying all the options available. Scroll down and you will find the option “WITH_CUDA”, check it and then click Configure again:

image

Once you Configure again, you will most probably see a bunch of red highlighted fields indicating that some errors have occurred. These errors, in most cases have to do with the path of NPP, CUDA Toolkit and CUDA SDK. Here’s what you will probably see:

image

These field have to do with the GPU module so make sure these are correct. By default, all the CUDA source files are compiled for 1.1, 1.2, 1.3 and 2.0 architecture. You can change this to any specific version. Next is the NPP root directory. If you’re getting CUDA_NPP_LIBRARY_ROOT_DIR_NOTFOUND then you need to specify the correct directory. I have my NPP 3.2 installed in “D:/Projects/Backup”. Here’s what you need to specify in this field: <NPP_DIRECTORY>/SDK where <NPP_DIRECTORY> is the NPP directory for example “D:/Projects/Backup/npp_3.2.16_win_32”.

Also make sure that the CUDA Toolkit and SDK paths are properly specified. Now Configure once again and you should see the message Configuring Done at the bottom:

image

Now click Generate and behold, OpenCV.sln solution file will be generated in the Build folder! If everything went right, you should see the Generating Done message too:

image

Now browse to your Build directory (mine is D:/Projects/OpenCV 2.2/build) and open the OpenCV.sln solution file. We’re now at last step:

Step 3: Building OpenCV.sln

Make sure you have CUDA include, bin and lib paths added in Visual Studio. From the projects listed in the solution file, browse down to opencv_gpu –> Properties –> C/C++ and make sure that Code Generation is either Multithreaded Debug or Multithreaded depending upon your configuration (Debug or Release). Also move down to Linker –> Input and add libcmtd and libcmt in the
Ignore Specific Library field:

image

Click OK and then Build the solution. Lean back and wait while the solution is built. If you did everything alright then you will have the new Binaries and Library files built in the build directory. My configuration was

Step 4: Testing the OpenCV GPU Module

Now to make sure that the GPU module works, create a test project (eg. Win32 Console Application), Add a C++ source file and check the GPU module like this:

image

All that this code is doing is dumping the GPU name on console. If it does then you have successfully built OpenCV 2.2 with GPU module 🙂