Setting up OpenCV with CUDA

The installation guide presented in Chapter 1, Getting Started, needs a few additional steps in order to include the GPU module. We assume that the computer in which OpenCV is going to be installed already has the software detailed in that guide.

There are new requirements to be satisfied in order to compile OpenCV with CUDA on Windows:

  • CUDA-capable GPU: This is the main requirement. Note that CUDA is developed by NVIDIA and, consequently, it is only compatible with NVIDIA graphic cards. Besides, the model of the card has to be listed at http://developer.nvidia.com/cuda-gpus. The so-called Compute Capability (CC) can also be checked on this website as it will be needed later.
  • Microsoft Visual Studio: CUDA is compatible only with this Microsoft compiler. It is possible to install the Visual Studio Express edition, which is free. Note that Visual Studio 2013 is still not compatible with CUDA at the time of writing, so we are using Visual Studio 2012 in this book.
  • NVIDIA CUDA Toolkit: This includes a compiler for GPUs, libraries, tools, and documentation. This toolkit is available at https://developer.nvidia.com/cuda-downloads.
  • Qt library for Visual C++ compiler: In Chapter 1, Getting Started, the MinGW binaries of the Qt library were installed, but they are not compatible with the Visual C++ compiler. A compatible version can be downloaded using the package manager by means of the MaintenanceTool application located in C:Qt. A good choice is the msvc2012 32-bit component, as can be seen in the following screenshot. It is also necessary to update the Path environment with the new location (for example, in our local system, it is C:Qt5.2.1msvc2012in). The Qt library is included in the compilation to take advantage of its user interface features.
    Setting up OpenCV with CUDA

    Downloading a new version of the Qt libraries

Configuring the OpenCV build

The build configuration with CMake differs in some points from the typical one explained in the first chapter. These differences are explained as follows:

  • When you select the generator for the project, you have to choose the Visual Studio compiler version that corresponds to the installed environment in the machine. In our case, Visual Studio 11 is the correct compiler, as it corresponds to the version of the compiler included in Visual Studio 2012. The following screenshot shows this selection.
  • In the selection of build options, we have to focus on the CUDA-related ones. If the installation of the CUDA toolkit was correct, CMake should automatically detect its location and activate the WITH_CUDA option. In addition, the installation path of the toolkit is shown through CUDA_TOOLKIT_ROOT_DIR. Another interesting option is CUDA_ARCH_BIN because the compilation time can be significantly reduced if we just select the corresponding version of our GPU; otherwise, it will compile the code for all the architectures. As mentioned previously, the version can be checked at http://developer.nvidia.com/cuda-gpus. The following screenshot shows the options set in our build configuration:
    Configuring the OpenCV build

    The CMake build configuration

Building and installing the library

CMake generates several Visual Studio projects in the target directory, ALL_BUILD being the essential one. Once it is opened in Visual Studio, we can choose the build configuration (Debug or Release) as well as the architecture (Win32 or Win64). The compilation starts by pressing F7 or by clicking on Build Solution. After the compilation has finished, it is recommended that you open and build the INSTALL project as it generates an install directory with all the necessary files.

Finally, the Path system needs to be updated with the location of the newly generated binaries. It is important to remove the previous location from the Path variable and have only one version of the binaries in it.

Note

Qt Creator should now find two compilers and two Qt versions: one for Visual C++ and one for MingGW. We have to choose the correct kit depending on the developed application when creating a new project. It is also possible to change the configuration of an existing project as kits are manageable.

A quick recipe for setting up OpenCV with CUDA

The installation process can be summarized in the following steps:

  1. Install Microsoft Visual Studio Express 2012.
  2. Download and install the NVIDIA CUDA Toolkit (available at https://developer.nvidia.com/cuda-downloads).
  3. Add the binaries for the Visual C++ compiler to the Qt installation and update the Path system with the new location (for example, C:Qt5.2.1msvc2012in).
  4. Configure the OpenCV build with CMake. Set the WITH_CUDA, CUDA_ARCH_BIN, WITH_QT, and BUILD_EXAMPLES options.
  5. Open the ALL_BUILD Visual Studio project and build it. Do the same operation with the INSTALL project.
  6. Modify the Path environment variable to update the OpenCV bin directory (for example, C:opencv-buildCudaQtinstallx86vc11in).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset