You are on page 1of 10

Installing OpenCV 2.4.

2 library with Visual C++ 2010 (Express) Edition


This guide is a modified version of the original installation guide found at http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html#windowsinstallation.

Downloading the Libraries


1) Get the latest version of OpenCV libraries from sourceforge (the latest version at the time of writing was 2.4.2) from http://sourceforge.net/projects/opencvlibrary/files/opencv-win/. The downloaded file will be a self-extracting archive. 2) Extract the OpenCV library into a short path (for example C:\)

3) Download the latest version of Intel Thread Building Blocks (TBB) from http://threadingbuildingblocks.org/file.php?fid=77. These libraries consist of functions optimized for multi-core processors, allowing OpenCV to take full advantage of a multi-core processor. 4) Extract the zip file to a folder inside the opencv folder. (for example C:\opencv\dep). The extracted folder should contain folders such as bin, doc, include etc (see screenshot below).

The following libraries are optional, OpenCV will run with Visual C++ 2010 with just the above two libraries as well.

Compiling with CMake


1) Download CMake from http://www.cmake.org/cmake/resources/software.html. 2) Install CMake. The default settings are fine. 3) Open CMake through the file cmake-gui.exe. It will be present in C:\Program Files\CMake 2.8\bin if the default installation directory was used. Otherwise search for it in the start menu (see screenshot).

4) Insert the path of the OpenCV library source (C:\opencv) in the first input box. Insert the address of the directory where you would want to build the libraries. (C:\opencv\cmake_build).

5) Press the configure button. If the build directory (the second one) is non-existent, CMake will create it automatically after confirming it from the user. Specify the compiler when the dialog box appears. Note : The default option selected of Visual Studio 10 is for the 32bit version. If you are using the 64 bit version, select it explicitly from the drop down menu available. (see screenshot for selecting the

64 bit option). CORRECTION (Pending confirmation) : use 32 bit builds even if you are compiling on 64 bit systems. The programs OpenCV compile will still be 32 bit programs!

6) CMake will search for as many packages as it can locate in your system and would display them. This allows us to control which package we would like to include in the final built version.

In the top window, the found packages are displayed and in the bottom area, the status of the CMake configuration are displayed. To control which packages are built, expand the WITH option in the top area (Select the grouped option to make it easier to work with the options).

Select the packages you want to install. Notice that CMake has not detected the TBB libraries. For this installation, we will select FFMPEG, JASPER, JPEG, PNG, PVAPI, TBB, TIFF, VIDEOINPUT. In the INSTALL grouping, select the option of INSTALL_C_EXAMPLES to have the examples built for you. In the ENABLE grouping, select the option of BUILD_SOLUTION_FOLDERS. This will categorize the many projects OpenCV will create. In the BUILD grouping, select the option of BUILD_EXAMPLES. These are great for testing the full compatibility of your system with OpenCV.

7) Press the configure button again.

CMake is unable to find the place where the TBB libraries are located. Click on the value to edit it. To browse for a path, click on the button and point it to the include folder in the TBB library directory (C:/opencv/dep/tbb40_20120613oss/include). Press the configure button again. If no more errors

are found, we have configured it correctly. If not, then point the erroneous paths to their correct values till they are accepted by CMake. 8) Press the Generate button to generate the binaries for OpenCV. The status window will show a message of generating done if OpenCV libraries are compiled correctly. 9) Go to C:\opencv\cmake_build and open the file OpenCV.sln. Give some time for the IDE to load up. 10) Wait for the IDE to get ready (look at the bottom left). In the screenshot below, it is still parsing the files of the solution.

We will need to build both the Debug and Release versions. First build the Debug version and then use the menu below to change the build the Release version.

The building will take some time (upto half an hour). 11) Build the INSTALL project only after both the Debug and Release versions are built.

12) Close Visual C++ Express.

Editing Paths
1) We need to include the directories of our libraries into the registrys environment variables, to facilitate the easy inclusion of these libraries into Visual C++. 2) Open command prompt (cmd.exe) as administrator. 3) Enter the commands : setx m OPENCV_DIR C:\opencv\cmake_build setx m TBB_DIR C:\opencv\dep\tbb40_20120613oss 4) The command prompt should echo back SUCCESS : Specified value was saved. 5) Next we need to add the tell the windows where the runtime DLLs of the OpenCV and the TBB libraries are located. We do this by editing the PATH variable in the registry. a) First Method i. Go to Control Panel -> System -> Advanced System Settings -> Environment Variables ii. Navigate to the system variables List box and scroll down to the Path variable iii. Click on Edit iv. Add the following paths (individual paths are separated by a semi-colon) : %TBB_DIR%\bin\ia32\vc10; %OPENCV_DIR%\bin\Release; %OPENCV_DIR%\bin\Debug; %OPENCV_DIR%\install\bin b) Second Method i. Download Path Editor from http://www.redfernplace.com/software-projects/patheditor/ ii. Install the software iii. Open Path Editor as administrator iv. Right click on the top list and select Add New Item v. Add the following paths to the list (1) %TBB_DIR%\bin\ia32\vc10 (2) %OPENCV_DIR%\bin\Release (3) %OPENCV_DIR%\bin\Debug (4) %OPENCV_DIR%\install\bin vi. Click on Save to Registry. If it succeeds, exit the program.

Setting Visual C++


1) 2) 3) 4) 5) Open Visual C++ Express. Open New Project. Select Win32 Console Application as project type. We will name it TestOpenCV. Select the option of Empty Project in the dialog box that appears after the project is created.

6) Now we need to tell visual C++ where the OpenCV debug and release libraries are built. We do this using property sheets. Go to View -> Property Manager. Expand the projects name to see two folders present there, Debug and Release. 7) Right click on the Debug folder and click on the Add New Property Sheet item.

8) Name the property sheet as OpenCV_Debug and click Add.

9) Right click on the newly created property sheet and select properties.

10) Expand C/C++ and select General. Add the following paths to the Additional Include Directories : $(OPENCV_DIR)\install\include; $(TBB_DIR)\include These paths tell the compiler where to look when referencing all #include statements in a cpp file. The first path is for the OpenCV library and while the second refers to the TBB library. The value following the $ sign refers to the environment variables we added to the system registry earlier. 11) Expand Linker and select General. Add the following paths to Additional Library Directories : $(OPENCV_DIR)\lib\Debug; $(TBB_DIR)\lib\ia32\vc10 These paths tell the compiler where additional .lib files are present which are used when linking the program.

12) Goto Input and click on additional dependencies. Click on Edit.

13) A dialogue box will appear. Add the following files to it : opencv_core242d.lib opencv_imgproc242d.lib opencv_highgui242d.lib opencv_ml242d.lib opencv_video242d.lib opencv_features2d242d.lib opencv_calib3d242d.lib opencv_objdetect242d.lib opencv_contrib242d.lib opencv_legacy242d.lib opencv_flann242d.lib tbb_debug.lib

These files refer to the libraries OpenCV should use while linking the program. 14) Apply these settings and press OK. 15) Repeat step 7 14 for the Release version with the following changes

a) Name the property sheets as OpenCV_Release b) Replace $(OPENCV_DIR)\lib\Debug with $(OPENCV_DIR)\install\lib as one the addresses in Step 11. c) In step 13, remove the last d letter from the name of each library (except for the last one) so opencv_core242d.lib becomes opencv_core242.lib. d) In step 13, replace tbb_debug.lib with tbb.lib.

Testing
Now we are ready to test a sample OpenCV code. Use the code present at http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html#source-code The code opens and displays any image, whose path is provided to it in the runtime arguments. Go to Project -> TestOpenCV Properties. Select Debugging and write the name of the image you want to display in the command arguments (Make sure such an image does exist in your project directory).

Debug the project. If the image loads up and displays then you have configured OpenCV to work correctly.

You might also like