Getting Started with OpenCV in Visual Studio

OpenCV is a free, open source library that enables your computer application to “see” and make decisions from the image data it acquires.  Here are some guides for setting up OpenCV for use in Microsoft Visual Studio Environments:

Installing OpenCV v2.1 in Visual Studio 2003

To install OpenCV v2.1 on Visual Studio 2003, follow the instructions given over at the OpenCVWiki Page.  From personal experience, the instructions given here have been observed to work with VS 2003 and VS 2005 as well as VS 2008.

To summarize, the main things to do are as follows:

Download and install OpenCV

Download OpenCV, preferably to a directory with no white spaces eg “C:\OpenCV2.1\”.

Configure Visual Studio:

(i) pre-2010 versions of Visual Studio

In pre-2010 versions of Visual Studio the following settings are done globally. In Tools -> Options -> Projects and Solutions -> VC++ Directories choose the “Show Directories for Include Files” combo box, and add the opencv directory (highlighted):

Then choose “Show Directories for Library Files” and add the lib directory (highlighted):

Then choose “Show Directories for Source Files” and add the ‘highgui’, ‘cxcore’, ‘cvaux’ and ‘cv’ directories:

(ii) For Visual Studio 2010

For Visual Studio 2010, these settings are no longer configured globally, but on a per-project basis. Notice that when you try Tools -> Options -> Projects and Solutions -> VC++ Directories in VS2010 it says that VC++ Directories editing in Tools > Options has been deprecated.

Instead, in your VS C++ project go to project -> properties -> VC++ Directories and set the sections for

– Include Directories
– Library Directories
– Source Directories

… using the same information for each as provided above.

Configure your C++ Project:

In your Visual Studio Project, select Properties -> Linker -> Input.

Edit the “Additional Dependencies” to insert the following files:

“cv210.lib”
“cxcore210.lib”
“highgui210.lib”
“cvaux210.lib”

 You should then be able to build the following simple example as follows:

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

const std::string filepath = "orig.bmp";

int main()
{
	// Load grayscale version of coloured input image
	IplImage* original = cvLoadImage( filepath.c_str(),
                                      CV_LOAD_IMAGE_GRAYSCALE );

	return 0;
}

Installing OpenCV1.x for Visual Studio 2003

Download the executable from the SourceForge site and run it. At this time of writing, the executable I used was OpenCV_1.0.exe.

Install it in the folder (say) “C:\OpenCV1.0”.

Set the environment variables

Add all necessary dll files to the PATH environment variable. Right-click on My Computer, select Properties and in the Advanced Tab select Environment variables:

Locate the PATH environment variable and add “C:\OpenCV1.0\bin”

Set up the Visual Studio Environment

We now need to set up the necessary global properties for Visual Studio.Before starting any C++ project, open Visual Studio and in the Tools menu and select Options from the drop-down menu. In the Projects section, select VC++ Directories. Then select “Library files” from the “Show directories for” drop-down list.

Click the insert new icon, locate the folder location for the OpenCV library files (“C:\OpenCV1.0\lib”) and add this:

In the same way, select “Include files” from the “Show directories for” drop-down list and add the following directories:

"C:\OpenCV1.0\cv\include"
"C:\OpenCV1.0\cxcore\include"
"C:\OpenCV1.0\otherlibs\highgui"
"C:\OpenCV1.0\otherlibs\cvcam\include"
"C:\OpenCV1.0\cvaux\include"
"C:\OpenCV1.0\otherlibs\_graphics\include"

And then select “Source files” from the “Show directories for” drop-down list and add the following directories:

"C:\OpenCV1.0\cv\src"
"C:\OpenCV1.0\cxcore\src"
"C:\OpenCV1.0\cvaux\src"
"C:\OpenCV1.0\otherlibs\highgui"
"C:\OpenCV1.0\otherlibs\_graphics\src"
"C:\OpenCV1.0\otherlibs\cvcam\src\windows"

Set up individual Project

In Visual Studio, create a new console application:

Open the project main cpp file and include the following OpenCV-related #include directives:

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

If your console application contains a “stdafx.h”, include these after them, or you may get build errors.  When you first try to build, you will probably get linker errors. Make sure all the necessary dependencies are added. In Project -> Properties -> Linker -> Input -> Add dependencies, add the following library files:

highgui.lib,
cv.lib,
cxcore.lib,
cvaux.lib,
cvcam.lib

And here is a simple example:

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

int _tmain(int argc, _TCHAR* argv[])
{
      IplImage* img = cvLoadImage( "C:\\spots.bmp" );
      cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
      cvShowImage( "Example1", img );
      cvWaitKey( 0 );
      cvReleaseImage( &img );
      cvDestroyWindow( "Example1" );

      return 0;
}


Other posts related to image detection

Tracking Coloured Objects in Video using OpenCV
Displaying AVI Video using OpenCV
Analyzing FlyCapture2 Images obtained from Flea2 Cameras
Integrating the FlyCapture SDK for use with OpenCV
OpenCV Detection of Dark Objects Against Light Backgrounds
Object Detection Using the OpenCV / cvBlobsLib Libraries

`