Using OpenCV 2.1 with MS Visual Studio

OpenCV 2.1 (supports the original C interface as well as the original C++ interface, but may have issues with 64-bit and TBB) can be downloaded as a prebuilt installer (that is easy to use), or the OpenCV library can be built using CMake (takes longer but you can customize the features):

Using OpenCV 2.2 with MS Visual Studio

OpenCV 2.2 (supports the newer C++ interface) has not been documented much so far, so if you have trouble with OpenCV 2.2, you might want to stick with OpenCV 2.1 for now, until OpenCV 2.2 is more established.

If you use VS2010 (32-bit only), you can just download the prebuilt OpenCV EXE installer, and skip to the "Building your own projects using OpenCV 2.2 in Visual Studio" section below. Otherwise (such as for VS2008 or 64-bit), OpenCV 2.2 must be built using CMake for your compiler:

Building the OpenCV 2.2 library using CMake

  1. Install CMake
  2. Start the CMake GUI from Start menu
  3. Select:
    • Where is the source code: (the directory where CMakeLists.txt in OpenCV is stored), eg: "C:\Program Files\OpenCV2.2"
    • Where to build the binaries: (the directory where the OpenCV library will be built), eg: "C:\Program Files\OpenCV2.2\build"
  4. Press Configure and select Visual Studio 2008 or 2010 project.
  5. Enter any parameters, like if you want to build Python bindings, documentation, shared libraries, use TBB, CUDA, Eigen2 libraries, etc. (Note that TBB, 64-bit and CUDA may still be under development and therefore buggy)
  6. Repeat pressing Configure until no more red rows are highlighted.
  7. When ready, press Generate.
  8. Open the "build" directory, where you will find your VS2008 or VS2010 "sln" file.
  9. Open the sln file in VS2008 or VS2010, and Built the Solution. This should compile the OpenCV 2.2 library into the build folder.

Building your own projects using OpenCV 2.2 in Visual Studio

After you have compiled the OpenCV 2.2 library using CMake, you should configure your Visual Studio projects to use OpenCV.

For the below instructions its assume that you installed OpenCV to the default location C:\OpenCV2.2 - if you selected something different (e.g. C:\Program Files\OpenCV2.2) then please adjust accordingly.

First of all create your new (full or empty) C/C++ project.

For an application project (*.exe) add this to your project properties:

  1. Go to VC++ Directories
  2. Add 2 new Include Directories (it's the path where you installed OpenCV, include folder):
    • C:\Program Files\OpenCV2.2\include
    • C:\Program Files\OpenCV2.2\include\opencv
  3. Add 1 new Library Directory (it's the path where you installed OpenCV, lib folder):
    • C:\Program Files\OpenCV2.2\lib
  4. Go to Linker in the left menu and select Input option
  5. Add these entries on Additional Dependencies option:
    • C:\Program Files\OpenCV2.2\lib\opencv_core220d.lib
    • C:\Program Files\OpenCV2.2\lib\opencv_highgui220d.lib
    • C:\Program Files\OpenCV2.2\lib\opencv_video220d.lib
    • C:\Program Files\OpenCV2.2\lib\opencv_ml220d.lib
    • C:\Program Files\OpenCV2.2\lib\opencv_legacy220d.lib
    • C:\Program Files\OpenCV2.2\lib\opencv_imgproc220d.lib
  6. Make the content of 'OpenCV_Helloworld.cpp' as such:

For a shared dynamically loadable library project (DLLs) add this to your project properties:

  1. C/C++ - General - Additional include Directories:
    • C:\OpenCV2.2\include\opencv;C:\OpenCV2.2\include
  2. Linker - General - Additional library Directories:
    • C:\OpenCV2.2\lib
  3. Linker - Input - Additonal Dependencies (debug):
    • opencv_core220d.lib opencv_highgui220d.lib opencv_video220d.lib opencv_ml220d.lib opencv_legacy220d.lib opencv_imgproc220d.lib
  4. Linker - Input - Additonal Dependencies (release):
    • opencv_core220.lib opencv_highgui220.lib opencv_video220.lib opencv_ml220.lib opencv_legacy220.lib opencv_imgproc220.lib

// OpenCV_Helloworld.cpp : Defines the entry point for the console application.
// Created for build/install tutorial, Microsoft Visual Studio and OpenCV 2.2.0

#include "stdafx.h"

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int _tmain(int argc, _TCHAR* argv[])
{
        // Open the file.
        IplImage *img = cvLoadImage("photo.jpg");
        if (!img) {
                printf("Error: Couldn't open the image file.\n");
                return 1;
        }

        // Display the image.
        cvNamedWindow("Image:", CV_WINDOW_AUTOSIZE);
        cvShowImage("Image:", img);

        // Wait for the user to press a key in the GUI window.
        cvWaitKey(0);

        // Free the resources.
        cvDestroyWindow("Image:");
        cvReleaseImage(&img);
        
        return 0;
}

OpenCVWiki: VisualC++ (last edited 2011-06-07 09:39:51 by SaurabhKumar)