7

I have uploaded several screenshots in this album: https://i.stack.imgur.com/TELST.jpg

I am trying to get GPU up and running in OpenCV in Visual Studio 2008. I am running one of the OpenCV GPU example codes, bgfg_segm.cpp. However, when I compile (with no compile errors) it throws an "OpenCV Error: No GPU suport".

  • Windows 7, 32-bit
  • Visual Studio 2008
  • nVidia Quadro 1000M with driver version 301.27
  • OpenCV 2.4.3rc (using the precompiled libs that came with it)
  • CUDA Toolkit 4.2, CUDA SDK.

I can run the .exe files in C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\bin\win32\Release without any errors, so it would seem that CUDA is working.

I really hope you can help because I feel I must be missing something obvious here. Any thoughts or suggestions are highly appreciated.

EDIT November 9th 2012:

I ended up following sgar91's instructions and it seems like things are working now!

One sidenote: When you enter Environment Variables check out the paths for CUDA. One of mine had one backslash (\) too many before bin like this C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\\bin;. There are three references to CUDA and its SDK, so check them out. Maybe it was just a one-time fluke. I am not sure this matters at all.

One more sidenote: I installed Visual Studio 2010 Express, and note that sgar91's instructions are meant for Visual Studio 2010 (aka "vc10"). It will not work in Visual Studio 2008 (aka "vc9") or Visual Studio 2012 (aka "vc11"), because there are no prebuilt lib files with OpenCV 2.4.3 for vc9 and vc11 (only vc10). Also, be aware if you are on 64-bit Windows you should change all X86 paths (32-bit) to X64 (64-bit) instead when you follow his guide, and in Visual Studio you need to change the Solution Platform from Win32 (dropdown menu in the top, middle next to Debug or Release) to x64.

Yet one more sidenote: OpenCV 2.4.3 supports CUDA 4.2 (or rather the libs have been compiled with CUDA 4.2). If you install CUDA 5.0 it will not work. It throws an error message. Can't recall which. If you absolutely need CUDA 5.0 you have to either wait for OpenCV to include it in future versions or compile your own libs via CMake.

I ran the code below (it's from here, but I had to correct one line in it to make it compile) and it compiled and showed the image, so I would expect that this means things are working?

#ifdef _DEBUG
#pragma comment(lib,"opencv_gpu243d")
#pragma comment(lib,"opencv_core243d")
#pragma comment(lib,"opencv_highgui243d")
#else
#pragma comment(lib,"opencv_core243")
#pragma comment(lib,"opencv_highgui243")
#pragma comment(lib,"opencv_gpu243")
#endif

    #include <iostream>
    #include "opencv2/opencv.hpp"
    #include "opencv2/gpu/gpu.hpp"

    int main (int argc, char* argv[])
    {
        try
        {
            cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
            cv::gpu::GpuMat dst, src;
            src.upload(src_host);

            cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);

            cv::Mat result_host(dst);
            //cv::Mat result_host = dst;         //old line commented out
            cv::imshow("Result", result_host);   //new line added by me
            cv::waitKey();
        }
        catch(const cv::Exception& ex)
        {
            std::cout << "Error: " << ex.what() << std::endl;
        }
        return 0;

    }

I can't get any of the code in C:\opencv\samples\gpu to work. It compiles, but then throws an error. But screw it, I'll figure that out somehow :)

ubehagelig
  • 173
  • 1
  • 2
  • 7

1 Answers1

10

You are using those OpenCV binaries which are compiled without GPU support.

C:\opencv\build\x86\... are without GPU support.

You have to use the binaries and lib files which are present in the build\gpu folder.

C:\opencv\build\gpu\x86\... are with GPU support.

UPDATE:

Procedure:

In Visual Studio 2010, go to project properties. In the VC++ Directories, you will see the following page:

Add the path of OpenCV include folder in the Include Directories textbox. Make sure that multiple paths are separated by a semicolon and there is no space in any of the path.

Similarly add the path of OpenCV lib folders for both the GPU and Non-GPU versions in the Library Directories Textbox. (Don't forget the semicolon)

Important: When writing the paths in the box, first write the GPU path and after that, the Non-GPU path.

enter image description here

Next step is adding the path of bin folder of OpenCV. But not in visual studio, but in the Path Environment variable as shown below:

enter image description here

  • Right Click My Computer
  • Go To Properties
  • Go to Environment Variables section
  • Edit the System Variable Path
  • Append C:\OpenCV\build\gpu\x86\vc10\bin and C:\OpenCV\build\x86\vc10\bin to the Path. Don't forget to separate different values with semicolon. Also---> GPU Comes First.
  • Save and exit.

Restart Visual Studio. The linker and #include directive will now recognize the OpenCV libraries. As we have added the path of the GPU libraries also, so complete GPU support will be available in OpenCV.

To use GPU functionality of OpenCV, you just have to do the following:

  • #include opencv2/gpu/gpu.hpp
  • Specify opencv_gpu243d.lib for Debug Configuration, or opencv_gpu243.lib for Release Configuration in the Additional Dependencies field in the Linker->Input section of project properties.

Some Additional Info:

In Visual Studio, there is an easy way to link libraries instead of specifying them in the project properties.

Just write these lines in the very start of your code:

#ifdef _DEBUG
#pragma comment(lib,"opencv_core243d")
#pragma comment(lib,"opencv_highgui243d")
#else
#pragma comment(lib,"opencv_core243")
#pragma comment(lib,"opencv_highgui243")
#endif
sgarizvi
  • 16,623
  • 9
  • 64
  • 98
  • Thank you. A few questions: (1) I see that c:\opencv\build\gpu\x86 only contains a vc10 folder and I am on vc9 - does it matter? Can I use vc10 files on vc9? Or do I have to install VS2010? (2) I would add C:\opencv\build\gpu\x86\vc10\lib to my list of Library Files, correct? If so, do I keep or delete the current reference to C:\opencv\build\x86\vc9\lib? (3) Where in Visual Studio do I add a reference to C:\opencv\build\gpu\x86\vc10\bin? Executable Files? – ubehagelig Nov 05 '12 at 12:26
  • I have now installed Visual Studio 2010 Express as well. When I get home later I will give that a go. Since I am clearly not setting the directories correctly up in Visual Studio, I appreciate any and all input, so if you know which directories go where in VS2010, do tell :) – ubehagelig Nov 05 '12 at 15:27
  • 1
    I think [this](http://siddhantahuja.wordpress.com/2011/07/18/getting-started-with-opencv-2-3-in-microsoft-visual-studio-2010-in-windows-7-64-bit/) may help. – sgarizvi Nov 05 '12 at 17:17
  • Thank you very much for your help. That guide sadly didn't provide any new information. Some are saying I *have to* compile OpenCV via CMake to get GPU and CUDA support while others, like you, don't. If you are right, then I would appreciate it immensely if you (or anyone else) could tell me *exactly* which paths to put where in VS2010 specifically to gain GPU support. – ubehagelig Nov 06 '12 at 13:26
  • 1
    Please check the update in the answer. Is it enough or do you need some more info? This is the complete procedure to run OpenCV GPU. I'm saying this because I did this whole procedure on a new Windows installation with only visual studio 2010 and successfully ran some functions. – sgarizvi Nov 06 '12 at 15:59
  • That is absolutely beautiful. Thank you so much for your help! I will try it out immediately and return with a result. – ubehagelig Nov 09 '12 at 08:47
  • You are awesome. Things work now. It's been over a week of trying so many things, and now it finally works! A final question: The code I have posted above in my revised original post, is it valid GPU code? If so, the thing is finally working. – ubehagelig Nov 09 '12 at 13:26
  • I had no clue VS supported #pragma – Joel B Nov 09 '12 at 14:50
  • 1
    Yes the code is valid. That is how OpenCV GPU programming is done. – sgarizvi Nov 09 '12 at 16:24