You are on page 1of 6

Using OpenCV in Microsoft Visual C++

September 2009

This is a short tutorial on how to compile and run a simple OpenCV program using Microsoft Visual
C++.

You should already know how to create a simple “Hello world” program in Microsoft Visual C++. If not,
please go through the tutorial called “Creating Your First Program in Microsoft Visual C++”. Also, if
you have not already done so, download OpenCV following the instructions in the class lecture notes.

Setting up Path Environment Variable

If you did not install OpenCV by running the executable file, you will have to manually set up your
computer’s path environment variable to point to the OpenCV binary directory. This will allow the
programs you create to use the “dll” library files in that directory. Go to “Start” -> “Settings”
-> “Control Panel” and select “System”. Select the “Advanced” tab as shown below.

Click on “Environment Variables”. This will bring up the following pop-up window:

1
Under “User variables”, create a variable called “Path” (or edit) the variable if it already exists.
Append the location of the OpenCV binary directory to the path. On my computer this is
C:\Program Files\OpenCV\bin;
However, if you loaded OpenCV into your home directory on “adit”, this location will be different;
probably something like:
Z:\adit\My Documents\OpenCV\bin;

Verify if OpenCV was correctly installed. Go to where OpenCV is located and go into the directory
called “samples/c”. Double click on the program called contours.exe. If this doesn’t run, and
you get some error something like “cxcore110.dll was not found”, then the path was incorrectly set.

Setting up Visual C++

Start up Microsoft Visual C++. Go to Tools->Options. In the pop-up window, select “VC++
Directories”. Under the tab “Show Directories for”, choose “Include files”. Add the
directories in the list box that are surrounded by a red line in the following figure. Of course, you may
have to change the location if OpenCV is not located in “Program Files”.

2
Now choose Library files and add “C:\Program Files\OpenCV\lib” as shown in the following figure:

Now choose Source files and add the directories that are surrounded by a red line in the following figure:

3
Now just click OK to close the dialog.

Creating a Simple OpenCV Program

Create a simple “Hello world” program in Microsoft Visual C++ (or you can use the program from the
previous tutorial).

Every time you create a program, you have to add dependency information to VC++. Select “Project-
>myproject1 Properties …” This will bring up a window as shown below. Select
Configuration Properties -> Linker -> Input. Go to the tab for Additional
Dependencies, and add the following:

cv.lib cxcore.lib highgui.lib cvaux.lib

Then just click OK to close the window.

4
In the main window, enter the following code for main.cpp. (You should be able to copy and paste it.)

#include <iostream>

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

using namespace std;

int main(int argc, char* argv[])


{
printf("Hello world\n");

IplImage *img = cvLoadImage("C:/Program Files/OpenCV/samples/c/lena.jpg");

// Create a window
cvNamedWindow( "result",
CV_WINDOW_AUTOSIZE // allow window to resize to fit image true size
);

cvShowImage( "result", img ); // Show image in window already created

// Wait for a keystroke. If a positive argument is given, it will wait for


// that number of milliseconds and then continue. If 0 is given, the
// program will wait indefinitely for a keypress.
cvWaitKey(0);

// Clean up (not really necessary, but good programming practice)

5
cvReleaseImage( &img );
cvDestroyWindow("result");

system("PAUSE");
return EXIT_SUCCESS;
}

Choose Debug->Start Without Debugging to compile and run the program. If there are no compile errors,
the program should display an image. If a blank image is displayed, then the program cannot find the
image file specified (check that the location is correct).

Congratulations! You have created your first OpenCV program. For help, see documentation at
C:\Program Files\OpenCV\docs\index.htm.

You might also like