Installing and Using Intel's OpenCV Computer Vision Library
This tutorial is an introduction to installing and using OpenCV on a Windows platform. At the end of this tutorial is a link to instructions for setting up and calibrating a Kinect device that can be used to develop computer vision applications using OpenCV. A more general OpenCV installation guide for Windows, Linux and Mac users can be found at the Willow Garage OpenCV forum. If you are a Windows user, then you should install Visual Studio 2010 Express (it is free). You can also create OpenCV projects in Eclipse. If you are a Linux or Mac user, then you should install CMake to build the binaries. OpenCV 2.2 comes with pre-built binaries for Visual Studio 2010.
The following instructions are for setting up and using OpenCV 2.2 with Visual Studio 2010 on a Windows 7 machine. These instructions are adapted from the tutorial on Willow Garage:
- Install Visual C++ 2010 Express
- Install OpenCV 2.2 in C:\Program Files\OpenCV2.2\
- Open Visual C++ and click on "New Project". Choose "Win32 Console Application" and give the project a name. Click "OK" then "Finish". This will create a directory for your project in C:\Users\your_username\My Documents\Visual Studio 2010\Projects\project_name. Your code will be in a subdirectory of project_name also called project_name. I recommend that you keep all of your Visual C++ OpenCV programs in this directory - this way you will only have to configure the Visual C++ project environment once, i.e., you won't have to configure the environment every time you create a new program.
- When the project opens in Visual C++, select "Project -> Properties" from the main menu. In the Property Pages box expand "Configuration Properties", then "VC++ Directories". In the "Include Directories" box, go to the end of the list, enter a semicolon, and enter all of the following (separate each one in the list with a semicolon):
- C:\Program Files\OpenCV2.2\include
- C:\Program Files\OpenCV2.2\include\opencv
- C:\Program Files\OpenCV2.2\include\opencv2
- C:\Program Files\OpenCV2.2\include\opencv2\
- C:\Program Files\OpenCV2.2\include\opencv2\calib3d\
- C:\Program Files\OpenCV2.2\include\opencv2\contrib\
- C:\Program Files\OpenCV2.2\include\opencv2\core\
- C:\Program Files\OpenCV2.2\include\opencv2\features2d\
- C:\Program Files\OpenCV2.2\include\opencv2\flann\
- C:\Program Files\OpenCV2.2\include\opencv2\gpu\
- C:\Program Files\OpenCV2.2\include\opencv2\highgui\
- C:\Program Files\OpenCV2.2\include\opencv2\imgproc\
- C:\Program Files\OpenCV2.2\include\opencv2\legacy\
- C:\Program Files\OpenCV2.2\include\opencv2\ml\
- C:\Program Files\OpenCV2.2\include\opencv2\objdetect\
- C:\Program Files\OpenCV2.2\include\opencv2\video\
In the "Library Directories" box, go to the end of the list, enter a semicolon, and enter C:\Program Files\OpenCV2.2\lib and then click "Apply".
- In the Property Pages box, expand "Linker" and select "Input". In the "Additional Dependencies" box, go to the end of the list, enter a semicolon, and enter all of the following (separate each one in the list with a semicolon):
- C:\Program Files\OpenCV2.2\lib\opencv_core220d.lib
- C:\Program Files\OpenCV2.2\lib\opencv_calib3d220d.lib
- C:\Program Files\OpenCV2.2\lib\opencv_highgui220d.lib
- C:\Program Files\OpenCV2.2\lib\opencv_video220.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
- C:\Program Files\OpenCV2.2\lib\opencv_objdetect220d.lib
- C:\Program Files\OpenCV2.2\lib\opencv_contrib220.lib
- C:\Program Files\OpenCV2.2\lib\opencv_features2d220d.lib
- C:\Program Files\OpenCV2.2\lib\opencv_flann220d.lib
- C:\Program Files\OpenCV2.2\lib\opencv_gpu220d.lib
- C:\Program Files\OpenCV2.2\lib\opencv_ts220.lib
- C:\Program Files\OpenCV2.2\lib\opencv_ffmpeg220.lib
See why you only want to do this once!?! Notice that some of the libraries have a 'd' (for 'debug') at the end and some do not. Click "OK".
- Compile the basic application by selecting "Debug -> Build Solution" from the main menu. The project should compile with no errors (look in the "Output" box at the bottom of the screen). Run the application by selecting "Debug -> Start Debugging" from the main menu (or click on the green arrow in the toolbar menu, or hit F5 on the keyboard).
- Minimize Visual C++, and open Windows Explorer. Go to C:\Program Files\OpenCV2.2\samples\cpp\ and copy contours2.cpp to C:\Users\your_username\My Documents\Visual Studio 2010\Projects\project_name\project_name.
- Maximize Visual C++, and in the Solution Explorer pane on the left, under "Source Files", right-click on the name of the basic application (the .cpp file), click on "Remove", and choose "Remove" (not "Delete") from the dialog box. Then in the Solution Explorer pane, right-click on "Source Files" and choose "Add -> Existing Item". Double-click on contours2.cpp to bring it into the project. Double-click on contours2.cpp once again to open it up in the editor.
- At the top of the file, insert "#include stdafx.h" as the first line. Save the file, compile it, and then run it. The program should run with no errors.
- There are a lot of other sample programs to play with located in C:\Program Files\OpenCV2.2\samples\cpp\. Be sure to remove any file with a main function in it from the Source Files of the project before attempting to compile another program that also has a main function in it. Also, make sure that all of the files that are needed to run the program (images, xml files, etc.) are in the same directory as the source code file.
- To create your own application, you may want to start by copy and pasting an existing source code file from the samples directory into the project_name\project_name directory and adding it into the Source Files as we did above. Be sure that "#include stdafx.h" is the first line of code in your source code file, modify the code as desired (with proper credit given to the original author), save it, compile it, and run it.
- To supply command line arguments to your program in Visual C++, select "Project -> Properties" from the main menu. Expand "Configuration Properties" and select "Debugging". In the "Command Arguments" box enter the list of command line arguments.