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 :)

  • Steven

    ah, there is also a macro that shows a dialog (yes, no, cancel) while building the source code, I don’t which one I should chose? Last time, I tried all but it’s still failed !

    • Anonymous

      Select “Cancel”

  • http://twitter.com/Xtr3mor Louis-P. Bourret

    Hi,
    I’ve successfuly compiled every project except opencv_gpu. I get the following errors:

    Error 3 error C3861: ‘nppiCannyGetBufferSize': identifier not found C:OpenCV2.2modulesgpusrcimgproc_gpu.cpp 601
    Error 4 error C3861: ‘nppiCanny_32f8u_C1R': identifier not found C:OpenCV2.2modulesgpusrcimgproc_gpu.cpp 605
    Error 1 error C2664: ‘nppiSum_8u_C1R’ : cannot convert parameter 4 from ‘Npp32s *’ to ‘Npp8u *’ C:OpenCV2.2modulesgpusrcarithm.cpp 503
    Error 2 error C2664: ‘nppiSum_8u_C4R’ : cannot convert parameter 4 from ‘Npp32s *’ to ‘Npp8u *’ C:OpenCV2.2modulesgpusrcarithm.cpp 510

    I have linked the nppi sdk folder in CMake as mentioned. I don’t know why I get this, and I thought I needed to recompile the nppi lib with the olution given with Cuda Toolkit 3.2, but I also get errors:

    Error 32 error LNK2001: unresolved external symbol _cudaEventCreate@4 C:Program FilesNVIDIA GPU Computing ToolkitCUDAv3.2npp_SDKsamplesbinarySegmentationbinarySegmentation.obj
    Error 29 error LNK2001: unresolved external symbol _cudaEventElapsedTime@12 C:Program FilesNVIDIA GPU Computing ToolkitCUDAv3.2npp_SDKsamplesbinarySegmentationbinarySegmentation.obj
    Error 31 error LNK2001: unresolved external symbol _cudaEventRecord@8 C:Program FilesNVIDIA GPU Computing ToolkitCUDAv3.2npp_SDKsamplesbinarySegmentationbinarySegmentation.obj
    Error 30 error LNK2001: unresolved external symbol _cudaEventSynchronize@4 C:Program FilesNVIDIA GPU Computing ToolkitCUDAv3.2npp_SDKsamplesbinarySegmentationbinarySegmentation.obj

    I would appreciate any help you can give, thank you for your time.

    • http://twitter.com/anatoliy_svt Anatoliy Sviridenkov

      Hi, download NPP 3.2 and use it, I had some problem with NPP 4.0 integrated to CUDA toolkit 4.0

      • Tomlensky

        Dear Anatoliy,
        I did download NPP 3.2, and complied with no problem. However, then I ran gpu_test it asked for NPP3.2 dll  which I added to the path variable, after it asked for another DLL (dont remember the name)  which was implemented only in NPP 4.0.
        So it seems that either OpenCV should be compiled with CUDA toolkit 4.0 and NPP 4.0 integrated in it or the toolkit and NPP should be version 3.2. It the latter case I have to install VS2008 and in the former case CMake does not accept NPP that is built into CUDA toolkit.

        Artem

    • lorenzo

      hi!…I have the same problem. have you found a solution somewhere?
      thanks
      Lorenzo

    • lorenzo

      Hi! I just found a solution to compile the opencv_gpu project:

      – Code generation -> runtime library -> Multithreaded dll
      – Linker -> Input -> Ignore Specific Library = nothing
      – C/C++ -> General -> Additional Include Libraries -> invert the order of the include path for CUDA and NPP (put first npp include folder and then CUDA include folder)

      for me this worked !
      ciao

  • Tom Anderson

    Correct me if I’m wrong, but these instructions are for 32 bit CUDA drivers. I think in my case, where I have a 64-bit Windows 7 OS, I can only install the 64-bit Nvidia drivers and therefore I am unable to succeed with these instructions.

    • Sotomsa

      These instructions worked fine for me.
      Windows 7 x64
      Visual Studio 10 -> Using V90 compiler
      Compiled OpenCV with GPU support in debug and release for x64

  • Bhavya

    Hi @salmanulhaq:disqus

    I tried every step on your blog but got these type of errors when I built opencv.sln

    1>imgproc_gpu.obj : error LNK2001: unresolved external symbol _nppiWarpPerspective_32s_C3R

    1>graphcuts.obj : error LNK2019: unresolved external symbol _nppiGraphcut_32s8u referenced in function “void __cdecl cv::gpu::graphcut(class cv::gpu::GpuMat &,class cv::gpu::GpuMat &,class cv::gpu::GpuMat &,class cv::gpu::GpuMat &,class cv::gpu::GpuMat &,class cv::gpu::GpuMat &,class cv::gpu::GpuMat &)” (?graphcut@gpu@cv@@YAXAAVGpuMat@12@000000@Z)

    1>matrix_operations.obj : error LNK2019: unresolved external symbol _nppiConvert_8u32f_C1R referenced in function “public: static void __cdecl `anonymous namespace'::NppCvt::cvt(class cv::gpu::GpuMat const &,class cv::gpu::GpuMat &)” (?cvt@?$NppCvt@$0A@$04$1?nppiConvert_8u32f_C1R@@YA?AW4NppStatus@@PBEHPAMHUNppiSize@@@Z@?A0xe04e0903@@SAXABVGpuMat@gpu@cv@@AAV345@@Z)

    Been stuck here for days ….

    Would really appreciate any help…

  • rkoglin

    Just in case someone has the same Problem… I was struggling to generate the project files with cmake 2.8.2 (everywhere i read it says that 2.8 should be ok). I guess the FindCUDA cmake files are wrong in CMake 2.8.2. In CMake,  and i wasnt able to see the CUDA_NPP_LIBRARY_ROOT_DIR variable values, what makes me think CMake wasnt even looking for NPP. Now with CMake 2.8.4 everything works fine im also able to build using VS2008 (32 bits).values, what makes me think CMake wasnt even looking for NPP. Now with CMake 2.8.4 everything works fine im also able to build using VS2008 (32 bits).

  • ben

    I followed your steps and I built the library fine, and the sample test program at the end compiles fine; however when I run the program I get an error message “The procedure entry point cuCtxCreate_v2 could not be located in nvcuda.dll. I am using VS2008, Cuda version 3.2 32 bits.

  • Niket Goel

    Hey there…
    I did all the steps you mentioned above successfully, but when i compiled the final program, i got the errors shown in the image… Can you plz help me on this… 

  • Felipe Fehse

    I am using SVN, OpenCv 2.3.2, Cuda 4.0 and VS2010 getting the following errors. Any idea of what’s the problem?

    Creating library C:/opencv/vs2010/lib/Debug/opencv_gpu232d.lib and object C:/opencv/vs2010/lib/Debug/opencv_gpu232d.exp
    2>cuda_compile_generated_stereobm.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_stereobp.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_stereocsbp.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_surf.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_remap.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_resize.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_row_filter.cu.obj : error LNK2019: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z) referenced in function lgamma
    2>cuda_compile_generated_split_merge.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_matrix_operations.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_matrix_reductions.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_pyr_down.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_pyr_up.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_hog.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_imgproc.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_match_template.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_mathfunc.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)
    2>cuda_compile_generated_column_filter.cu.obj : error LNK2001: unresolved external symbol “void __cdecl cv::gpu::error(char const *,char const *,int,char const *)” (?error@gpu@cv@@YAXPEBD0H0@Z)

  • Pingback: YauB » OpenCV: Installation()

  • Dino

    My problem is to add include, bin and lib paths to visual studio

  • -->