15 December 2013

Setting up OpenCV in Visual Studio 2012

I had previously written a post on setting up OpenCV 2.3.1 on VS 2010. It's been long since I updated it. So here's the new post on setting up OpenCV in Visual Studio 2012.

I'll be demonstrating this with OpenCV 2.4.6 on Visual Studio 2012 Express. I assume that you've got Visual Studio 2012 already installed.

Part 1

  1. Download the OpenCV installer for Windows from the official download page. Using the version just previous to the latest release is generally a good idea.
  2. Run the downloaded .exe. Select the 'Extract to' directory and press 'Extract'. (Selecting a path without spaces is a good idea.)
  3. A folder named opencv must have been created in your selected directory. Rename it to 'opencv<your-version-number>'. For ex, with OpenCV 2.4.6, I renamed it to 'opencv2.4.6'
Part 2

We need to set two environment variables. Refer this if you haven't setup environment variables before.
  1. Create a new User Variable named 'OPENCV' and set its value to the directory where you extracted earlier. 
  2. Edit the variable named 'Path'. Add ";%OPENCV%\build\x64\vc11\bin" (without quotes) if you're running a 64 bit machine or ";%OPENCV%\build\x86\vc11\bin" (without quotes) if you're running a 32 bit machine. If the Path variable doesn't exist, create a new one named 'Path'.

Part 3

Start VS 2012. If it was open previously, exit it and restart.
  1. Click on New Project. (Ctrl+Shift+N)
  2. Under Visual C++, choose Win32 Console Application. Give your project a name. Click on OK.
  3. A Win32 Application Wizard must have popped up. Click Next.
  4. On the next window, make sure you tick 'Empty Project' under Additional Options. Click on Finish.
Extra Steps for 64 bit machines. If you're on a 32 bit machine, skip to Part 4.
  1. If you're on a 64 bit machine, click on Build Menu and select 'Configuration Manager'.
  2. Click the combo box under 'Active solution platform' and select New 
  3. In the next window, under new platform, select x64. Click OK. Close the configuration manager. 

Part 4
  1. Download this zip. Extract its contents to your project folder.
  2. If you're using a version of OpenCV other than 2.4.6, open the OpenCV2.4.6.props file(Choose 32 or 64 bit accordingly) in a text editor. Find and Replace all '246' with your OpenCV version number. For ex, I would replace all 246 to 247 for OpenCV 2.4.7.
  3. In VS, open the Property Manager window. (View  Other Windows  Property Manager)
  4. Click on 'Add Existing Property Sheet'. Select the OpenCV .props file(Choose 32 or 64 bit accordingly). Click Open.
  5. In the 'Solution Explorer Window', right click on 'Source Files' folder → Add → Existing Item. Select main.cpp that you had copied to your project folder.
  6. Set the build target to Release. If you followed all steps correctly, hit the F5 key. The camera should fire up. Press Esc key to close the program.
If you face any problems, leave a comment. I'll try to help. 

Cheers!

26 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you for this. I've been looking at and trying out several ways since two days, stumbled upon this today. Of immense help.

    ReplyDelete
  3. Compiled and linked the main.cpp on VC2012.
    When I try to run the program I get:
    "The program cannot start because MSVCP120.dll is missing...".
    How do I fix this?
    Thanks
    Peter

    ReplyDelete
    Replies
    1. That dll file looks like be related to VS2012. I've not seen such an error before. You can try the steps here, maybe it solves your issue: http://stackoverflow.com/questions/19741097/missing-dll-file-when-i-run-my-application-on-another-machine

      Delete
  4. I downloaded opencv 2.4.8 from sourceforge.com and extracted it to C:\OpenCV folder. I noticed there was no bin folder in build\x86\vc11. Thus I can't set the variable path. Where can I get the bin folder and all its dll files?

    ReplyDelete
  5. Did you download the 2.4.8 file from this link http://opencv.org/downloads.html? You might have downloaded just the source archive instead of the executable.

    ReplyDelete
  6. Hi Mahindra. I followed all the steps as you mentioned. But when I try to build the main.cpp It doesn't build and gives the following error:
    Cannot open include file: 'opencv2/highgui/highgui.hpp': No such file or directory.

    Can you help me with this problem?

    ReplyDelete
  7. I have downloaded the exe file. It was of 345mb. But no bin folder and its dll files.
    It gives the error:
    IntelliSense: cannot open source file "opencv2\highgui\highgui.hpp" c:\Users\Shrikant\Documents\Visual Studio 2012\Projects\opencv trial\opencv trial\Source.cpp 4 1 opencv trial

    Can you tell me from where can I get the dll files?

    ReplyDelete
  8. I'm getting the following errors:
    1.Error 1:error LNK2001: unresolved external symbol WinMain
    2.Error 2:error LNK1120: 1 unresolved externals

    ReplyDelete
  9. It work on win7 x64 and visual studio 2013 profesional. thanks!

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. first i get this error :
    -----
    openCV_testing.obj : error LNK2019: unresolved external symbol _cvDestroyWindow
    referenced in function _main
    ----
    but i was doing this mistake :
    ---
    active solution platform is X64 but you are trying to link x86 OpenCV libraries.
    -----
    Thanks

    ReplyDelete
  12. hollo, tnx for this, but I have some problems:
    1>LINK : fatal error LNK1104: cannot open file 'opencv_haartraining_engined.lib'
    Can You help me?

    ReplyDelete
  13. opencv2/highgui/highgui.hpp
    this error appears because you were supposed to extract the folder in part 1 to regular directory in my case "C:\Users\Robert\Downloads\opencv2.4.8"

    Which is not the same as the location of the VS Project

    I do have an error that MSVCP120.dll is missing

    Will try to fix it and update y'all

    ReplyDelete
    Replies
    1. so what should I do? I extract the content in C:\Users\**\Downloads\Compressed\opencv and then I copied the content only to C:\Users\**\Document\Visual Studio 2012\Project\coba but the result is same, the same error keep appearing, please help

      Delete
  14. In order to fix the dll error install the Redistributable

    http://www.microsoft.com/en-us/download/details.aspx?id=30679

    ReplyDelete
  15. Everything works
    Thanks a lot!

    windows 8.1
    Visual Studio 2012
    OpenCV 2.4.8

    ReplyDelete
  16. If you would like to choose between built in webcam and usb webcam then just change the number in the code

    capture = cvCaptureFromCAM(0);

    or

    capture = cvCaptureFromCAM(1);

    ReplyDelete
  17. Robert: I have "opencv2/highgui/highgui.hpp" error. I reinstalled opencv-2.4.9 to C:\opencv\OpenCV2.4.9. Previously installed in C:\projects\opencv. I followed all steps above. Any suggestions? Thank you

    ReplyDelete
  18. Replies
    1. How do you fix your error? I still have the same problem as you mentioned above, but I use opencv 2.4.8 and I put the opencv folder in C:\opencv
      how should I do to fix this? please help

      Delete
  19. Will this work with visual studio 2013 express in widows 8.1?

    ReplyDelete
  20. Thank you for teaching opencv. I did all steps same with your vedio. But, I got error message that "Unable to start program ' C:\User\Administrator\documents\visual studio 2012\Projects\Demo\x64\Debug\Demo.exe". In the 'Debug' folder, just Demo.pdb file exist. How can I get Demo.exe? Thank you for reading my message =)

    ReplyDelete
  21. Can anyone Please help me in configuring opencv 3.0 + CUDA 6.5 + Visual Studio 2012

    ReplyDelete
  22. You really did a great Job. I have been trying to setup Visual Studio for OpenCV for long time. I started thinking that setting OpenCV up is a myth :P
    Thanks

    ReplyDelete