3 January 2012

Setting up OpenCV 2.3.1 on Visual Studio 2010

[UPDATE: This post is fairly old now. I wrote a new post on setting up OpenCV in Visual Studio 2012 here.]

OpenCV is an open-source C++ library used for computer vision based applications, and one of the most widely used ones in its field. But setting up this awesome library and getting it running is not a simple task for beginners. This post will show you a method to setup OpenCV on Visual Studio without losing one's sanity. Please note that we won’t be setting up some advanced features of OpenCV like TBB, IPP or GPU support. Those are generally required by advanced users.

Prerequisites

Before starting, get the prerequisites:


  • Get the OpenCV 2.3.1 windows super pack from sourceforge. Get it from here.


  • You need Visual C++ 2010 installed for using OpenCV. It’s a part of Visual Studio 2010 bundle. You may download only Visual C++ 2010 or the entire bundle. Students can get the professional edition of Visual Studio for free(instructions). Express edition can be downloaded here. You can work with both professional or express edition. Professional edition is paid while Express edition is free.


  • Download the OpenCV Helper app and some required project files from google code here.

  • STEP ONE: Extract


  • Run the OpenCV super pack.exe, select a directory where OpenCV would be extracted. I’d suggest something simpler like C:\ or any other root. Click Extract.


  • This step is important. A folder named "opencv" must have been created in the location you selected. Make sure you rename it to OpenCV 2.3.1.  

  • snap6

    STEP TWO: Setup Environment Variables
    The next step is to setup Environment Variables. These are some variables specific to the user account that store the location of important files which are required by multiple applications. We’re setting up variables related to OpenCV to make life easier for VC++. You can setup the variables manually(described in a later section) or you can use OpenCVHelper – a program I wrote which does this automatically. It’s present in OpenCV_Helper.zip. Make sure you close Visual Studio before using it.

    snap5



  • Click on the browse button and select the folder where you extracted OpenCV. Make sure you select the folder containing the OpenCV directories. The extractor extracts it to a directory called opencv in the location selected during extraction.


  • Select 32-bit or 64 bit according to the version of OpenCV you wish to use. If you’re using the Express edition of VC++, use 32-bit since it doesn't support 64-bit.


  • Click on Set Environment Variables. 

  • STEP THREE: “Hello World”
    Now we’re ready to create our first project. Fire up VC++.


  • Click on ‘New Project’ in the Start Page or press Crtl + Shift + N.


  • In the New Project window, select ‘General’ under Visual C++ and select the Empty Project template. Enter a name for your project. Make sure Create Directory for your Solution is checked. Click on OK.


  • Now go to Tools menu, Settings -> Click on Expert Settings. Now go to View menu-> Property Manager. The Property Manager window should be visible now. For Visual Studio users, View -> Other Windows -> Property Manager
  • snap7




  • Right click on Debug, select ‘Add Existing Property Sheet’. Browse to and select the OpenCV 2.3.1 Debug.props file. (located in OpenCV_Helper.zip)


  • Similarly, right click on Release, select ‘Add Existing Property Sheet’. Browse to and select the OpenCV 2.3.1 Release.props file. (located in OpenCV_Helper.zip)


  • Now open Solution Explorer (Crtl + Alt + L), right click on Source Files, select Add –> New Item. Select C++ File, name it HelloWorld, click on Add. Navigate to main.cpp (located in OpenCV_Helper) and copy-paste its contents in this file.


  • That’s it! Hit F5. If you followed the steps correctly the webcam should fire up. If you don’t have a webcam attached, a message would be displayed. Hit Escape to close the window. 

    Setting Up Environment Variables Manually


    In case you want to set up environment variables manually, you can follow these steps:


  • In Windows 7/Vista, open System Properties. Select Advanced System Settings –> Environment Variables. In Windows XP, right click on My Computer –> Properties. Under Advanced tab, select Environment Variables.


  • We’ll need to set four variables. Under User Variables, click on New. If any of the below variables already exist, add a semi-colon ’;’ to the value and append the new value.

    NameValue
    OpenCVFolder path where you extracted OpenCV
    INCLUDE%OPENCV%\build\include
    LIBFor 32 bit: %OPENCV%\build\x86\vc10\lib 
    For 64 bit: %OPENCV%\build\x64\vc10\lib
    PathFor 32 bit: %OPENCV%\build\x86\vc10\bin
    For 64 bit: %OPENCV%\build\x64\vc10\bin


  • For anyone further interested, the Property Sheets that we added to our project are a type of configuration files. They tell the compiler where to look for header files and other dependent libraries related to opencv when we use them in our projects. Also since the location of opencv folder has been setup as an environment variable, you can change the location of your opencv folder if you require. You'll only need to update the OPENCV environment variable and restart VC++.

    If you want to setup further features like TBB, Nvidia GPU support, etc. there's a wonderful page describing the steps in detail here.

    Do post a comment if you successfully setup OpenCV or if something went wrong.
    Cheers! :)