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

    29 comments:

    1. You explained in simplest English. Good work !!

      ReplyDelete
    2. bro i didn't found any settings option in tools menu!!!

      ReplyDelete
      Replies
      1. If you're using Visual C++ Express, go to Tools menu -> Settings -> Expert Settings. Now go to View menu-> Property Manager. The Property Manager window should be visible now. If you're using Visual Studio you can find it under, View menu -> Other Windows -> Property Manager

        Delete
    3. i have an error - LNK1104: cannot open file 'opencv_core231d.lib'
      please help.. :) thanks..

      ReplyDelete
      Replies
      1. What's the value of your environment variables OpenCV, LIB, INCLUDE and Path?
        And directory structure of your opencv folder? Did you rename it after the extractor unzipped it?

        Delete
    4. Hi Manindra,

      Thank you for this page!

      I have a problem. I.m trying to set up on VS2010 Ultimate 64-bit.

      However, when I try this bit:

      "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"

      When I click on "Tools", there is no "Settings" option available.

      I would really appreciate a hint!

      All the best,

      P

      ReplyDelete
      Replies
      1. Hi,
        That instruction is for Visual C++ Express edition users. The 2nd line is meant for Visual Studio users. Sorry, for the confusion.

        "For Visual Studio users, View -> Other Windows -> Property Manager"

        HTH

        Best,
        Manindra

        Delete
    5. i have an error - LNK1104: cannot open file 'opencv_core231d.lib'
      i hve used opevcv helper to set environment variables and also renamed it
      but still problm persists

      please help..

      ReplyDelete
      Replies
      1. VS isn't able to find some required files.
        What is the location of your opencv folder? And what are the values of your environment variables?

        Delete
    6. my opencv folder is in c:\ drive
      my environment variables are
      INCLUDE %OPENCV%\build\include
      LIB %OPENCV%\build\x64\vc10\lib
      OpenCV C:\OpenCV 2.4.2
      path C:\ProgramFiles\Java\jdk1.7.0_03\lib;%OPENCV%\build\x64\vc10\bin

      ReplyDelete
    7. and i am using windows7 proffesional 64 bit operating system

      ReplyDelete
      Replies
      1. This post is about setting up OpenCV 2.3.1. You're using a higher version of opencv, 2.4.2. I've not added support for the latest version.

        The propertysheet files I've provided work onlyy with 2.3.1.

        Delete
    8. fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
      1>
      1>Build FAILED.
      1>
      1>Time Elapsed 00:00:00.14
      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



      bro,, plz help me,,

      ReplyDelete
    9. hi friend ....pls help me ...when i create new project it shows me, project creation failed ...why it occurs ????

      ReplyDelete
      Replies
      1. I'm not sure but this may have to do with admin privileges. Try creating the project in a drive other than C drive.

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

      ReplyDelete
    11. That's odd. The file is a self extracting archive. If the file is corrupt, redownload the file and try again.

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

      ReplyDelete
    13. thanks!! finally this tutorial worked out for me!!

      ReplyDelete
    14. Hi, I'm using Windows 7, 64-bit. I installed Visual C++ 2010 Express. I did exactly as you said (selected native 32-bit on the OpenCV_Helper thing)...I get this error:

      fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
      ===== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =======

      Please help.....

      ReplyDelete
      Replies
      1. This error occurs if you're using libraries compiled for 32 bit, with project target set as 64 bit or vice versa. Checkout this SO discussion which provides pointers:
        http://stackoverflow.com/questions/3563756/fatal-error-lnk1112-module-machine-type-x64-conflicts-with-target-machine-typ

        Your project target should be set to 32 bit since you're using 32 bit libraries. And make sure to clean and rebuild your solution after tweaking any settings.

        Delete
    15. I am not sure whether to use 32bit or 64bit for Lib and path. Please help me.

      ReplyDelete
    16. i did exactly as said in the tutorial..and the example program given in helper gets compiled and runs but then the execution infinitely keeps on saying
      ERROR: SampleCB() - buffer sizzes do not match
      and i cannot see any input onto the camera window please help

      ReplyDelete
    17. thanks alot, it worked but please can get the code for face detection?

      ReplyDelete
    18. i followed the instructions as it is mentioned in the tutorial but still am unable to compile the code. i am getting the following error.
      1>------ Build started: Project: testcv, Configuration: Debug Win32 ------
      1>LINK : fatal error LNK1561: entry point must be defined
      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
      i am using a 32bit Windows XP machine.
      can u plz help me out with this?

      ReplyDelete
    19. I did not find OPEN-CV helper.zip?where it is??

      ReplyDelete