Overview of OpenCV and setting up OpenCV in Visual Studio 2005/08/10

Posted by SUYOG PATIL on 8:22 PM in , , ,
Overview of OpenCV :
  • CV:Image processing and vision algorithms
  • HighGUI-GUI, Image and Video I/O
  • CXCORE-basic structures and algoritms,XML support, drawing functions
  • CvauxAuxiliary(experimental) OpenCv functions
  • CvCamcross-platform module for processing video stream from digital video cameras
  • ML-MachineLearning methods
Documentation via the Wiki
OpenCV’s documentation Wiki is more up-to-date than the html pages that ship with
OpenCV and it also features additional content as well. Th e Wiki is located at http://
opencvlibrary.SourceForge.net. It includes information on:
Instructions on compiling OpenCV using Eclipse IDE
• Face recognition with OpenCV
• Video surveillance library
• Tutorials
• Camera compatibility
• Links to the Chinese and the Korean user groups
Another Wiki, located at http://opencvlibrary.SourceForge.net/CvAux, is the only documentation
of the auxiliary functions discussed in “OpenCV Structure and Content”
(next section).
CvAux includes the following functional areas:
• Stereo correspondence
• View point morphing of cameras
• 3D tracking in stereo
• Eigen object (PCA) functions for object recognition
• Embedded hidden Markov models (HMMs)

Regardless of  documentation source, it is often hard to know:
• Which image type (floating, integer, byte; 1–3 channels) works with which
• Which functions work in place
• Details of how to call the more complex functions (e.g., contours)

So questions above will be answered soon when we will discuss about actual programming....Now let us start....

Setting up OpenCV in Visual Studio 2005/08/10:

1)Creating the Project:

A project is initially created by selecting:
File -> New -> Project

Create a “Win32 Console Project

Make it an “Empty Project” by selecting the box under “Application Settings”


Create the First File
Right Click the “Source Files” Folder under the project name 
Add -> Add new Item

Select “C++ file” and give it a name

Creating a file makes it possible to set “Additional Include Directives” in the C/C++ pane under the project properties.
In order to build projects using OpenCV the required libraries and directives must be included in the project’s properties.
Open the Properties Pane
Right Click the name of the project and select “Properties”

Set Additional Include Directives
Under the C/C++ tab select “General

Select the “Additional Include Directives
Add the full path to each of the folders which contain “.h” files required to use OpenCV
Be sure to include trailing “\
Utilized Directives
C:\Program Files\OpenCV\cvaux\include\
C:\Program Files\OpenCV\cxcore\include\
C:\Program Files\OpenCV\cv\include\
C:\Program Files\OpenCV\otherlibs\highgui\
                                                 C:\Program Files\OpenCV\otherlibs\cvcam\include
 (This was for old Opencv versions)

For Opencv 2.2


Set Additional Dependencies
Under the Linker tab select “Input
Select the “Additional Dependencies
Add the full path to each of the “.lib” files required to use OpenCV
Be sure to keep the paths in quotes
Utilized Dependencies
"C:\Program Files\OpenCV\lib\cv.lib“
"C:\Program Files\OpenCV\lib\cvaux.lib“
"C:\Program Files\OpenCV\lib\cxcore.lib“
"C:\Program Files\OpenCV\lib\cvcam.lib“
                                                  "C:\Program Files\OpenCV\lib\highgui.lib"


 For Opencv 2.2

If you are using  OpenCV2.3 then just change names of include and library files accordingly.
Now that the environment is configured it would be a good idea to test it to make sure that a program will correctly build and run.

Testing the First Program
The sample code provided with Opencv can be cut and pasted into the file created in the project space to test OpenCV

You can try following program as given in one tutorial which I read at beginning days of learning Opencv.

#include <cv.h>
#include <highgui.h>
  This will pop up a small box with "Hello World" as the text.
  @author: Gavin Page, gsp8334@cs.rit.edu
  @date: 28 November 2005
int main( int argc, char** argv ) {
  //declare for the height and width of the image
  int height = 320;
  int width = 240;
  //specify the point to place the text
  CvPoint pt = cvPoint( height/4, width/2 );
  //Create an 8 bit, 3 plane image
  IplImage* hw = cvCreateImage(cvSize(height, width), 8, 3);
  //initialize the font
  CvFont font;
  cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX,
  1.0, 1.0, 0, 1, CV_AA);
  //place the text on the image using the font
  cvPutText(hw, "Hello World", pt, &font, CV_RGB(150, 0, 0) );
  //create the window container
  cvNamedWindow("Hello World", 0);
  //display the image in the container
  cvShowImage("Hello World", hw);
  //hold the output windows
  return 0;

Building the Program
The program is built by selecting:
Build -> Build Solution
Or by pressing “F7
Running the Program
The program is run by selecting:
Debug -> {Start||Start without Debugging}
Or by pressing “F5” or

At this point you should have a working OpenCV project. If the program is not working you should go back and carefully recheck the steps.
From here you can explore the documentation to review the functions available.
There are also a number of tutorials on the web including:
Or you can just search for them 
You should also join the OpenCV Community located at:
As of today there are >45000 members available to answer questions. There is also a searchable message board where you can look up previous queries.

For DevC++ or other development environment


We will suppose that the OpenCV library is installed in the C:\Program Files\OpenCV directory. Now, we have to configure DevCpp that he can take into account, all the includes files (.h), all the static libraries in OpenCV (.lib), and all the dynamic library (.dll) useful for the execution and not for the compilation.
This configuration has been tested with the version 4.9.9.x of DevCPP.
First of all, you have to indicate the header files you want to add. To do that, select Tools->Compiler Options.
compilerOptions.jpg .
Then click on the plus sign to add a new compiler (in fact, only some different options) named here, OpenCV.
To finish, on the section Add the following ... write : -L"C:\Program Files\OpenCV\lib" -lcxcore -lcv -lcvaux -lhighgui -lml -lcvcam. Or the following for OpenCV 2.1 -L"C:\OpenCV2.1\lib" -lcxcore210 -lcv210 -lcvaux210 -lhighgui210 -lml210

Include files configuration

Next, click on Directories and then on C Includes to add all the headers, located in some C:\Program Files\OpenCV subdirectories as shown in the picture.
For OpenCV2.x, you will notice that there are changes in the overall directory structure and hence you won't find the folders shown in the image below. You only need to add C:\Program Files\OpenCV2.x\include\opencv in the include tab to get things to work.
Of course, if you want to write C++ programs, do the same thing on the C++ Includes tab.

Static library files configuration

The following picture shows the static libraries paths to add.For OpenCV2.x there is no otherlibs\highgui folder and hence just adding C:\Program FIles\OpenCV2.x\bin is sufficient.

Dynamic library files configuration

And to finish, add the bin directory where the dlls are:


Let us choice a C program on the samples directory of OpenCV and try to execute it by typing F9. As you can see, all is OK.
That's all folk. I hope this short tutorial was useful.


                               For CodeBlocks
CodeBlocks is an GPL based and cross-platform IDE. This is the tutorials using CodeBlocks with OpenCV. 

Create a simple console project.

We can use project wizard to create a simple console project. Here is the steps
Give this project name of "test_opencv"
Then copy the sample code to main.cpp

Configuring Code::Blocks for OpenCV v2.2

Now you need to configure the compiler to find the OpenCV header files and libraries. OpenCV was made of just 4 libraries that could each be manually added (until v2.1), but from OpenCV v2.2 onwards, there are many more library files so you are highly recommended to use the tool "pkg-config" to setup your OpenCV configuration, as mentioned on CompileOpenCVUsingLinux.
pkg-config is a free command-line tool (available on Windows, Mac and Linux) that should have been automatically setup correctly when you built OpenCV with CMake, so that if you type "pkg-config opencv --cflags" or "pkg-config opencv --libs", it will display the compiler and linker arguments to successfully compile your own OpenCV projects without worrying about where OpenCV is installed or worrying about which version you have installed, or how to link to the library in each Operating System, etc.
If you were compiling your project on the command line you could type:
    cc `pkg-config opencv --cflags` -c main.cpp
    cc `pkg-config opencv --libs` -o test_opencv main.o

(Note: pkg-config is surrounded by back-tick characters, not quote or apostrophe characters (its usually the same key as the Tilda key ~, next to the 1 and Esc keys).
To setup Code::Blocks to use pkg-config, first you should right-click on your project and open the "Build options ..." dialog.
Now you can simply put this into "Linker settings -> Other linker options":
    `pkg-config opencv --libs`

And put this into "Compiler-> Other options":
    `pkg-config opencv --cflags`

(Remember to include the back-tick characters, by copy-pasting those lines directly into Code::Blocks).
The beauty of this method is that it should work for Linux, Windows and Mac, and for all versions of OpenCV, whereas the old manual method has different lib filenames for different Operating Systems and different versions of OpenCV.


If you have tried the pkg-config method above but Code::Blocks does not find OpenCV headers and libs but you have verified that pkg-config works for OpenCV on a command-line, then Code::Blocks probably doesn't know where pkg-config is installed. So you should place a link into "/usr/bin/" (Linux & Mac) or "C:\Windows\" (Windows) so that it will be found. For Linux or Mac, you can find the path to pkg-config if you type this into a terminal:
    which pkg-config

This might print something like "/opt/local/bin/pkg-config".
So then type this in a terminal:
    cd /usr/bin
    ln -s /opt/local/bin/pkg-config pkg-config

This allows accessing pkg-config from the most common program folder "/usr/bin/" as well as it's actual location.

Configuring Code::Blocks using old method for OpenCV v2.0

Set the include header file path (OpenCV v2.0):
Set the library path (OpenCV v2.0):