This section will show you, in details, how to install OpenCV (here version 2.4 released June 2012) for the MS Visual C++ platform. We selected here the 64-bit compiler. The installation has been done on Windows 7. Please refer to the
OpenCV Cookbook for more details.
See next section on how to install OpenCV for the Qt environment.
- The first step is obviously to download OpenCV. To install the OpenCV library, just go to the OpenCV official website at opencv.willowgarage.com. You'll find there the current release version in a downloadable zip file or in a Windows install. In the case of version 2.4, a superpack installer is available:
Run it and extract it to the directory of your choice:
That could be a temporary location since the complete installation process will move everything to another final destination. Once extraction completed, you now have all OpenCV source files in the specified directory. - The next step is to compile the library for the compiler you want to use; here it will be the Visual C++ 64-bit compiler. To build the library from the source files, OpenCV uses CMake, a cross-platform and open source tool designed to build library packages. We then need to install CMake. Go to cmake.org, download the Windows Win32 Installer and launch it.
- Once CMake installed, you can start the gui-based application (
cmake-gui
)
In CMake, specify the directory containing the source code and the one that will contain the built version, here it isC:\OpenCV2.4
.
Click onConfigure
. This will create the output directory.
You then specify the compilers that will generate the project. Here we selected the 64-bit compiler of Visual Studio but you could also choose the regular win32 one.
You will the be offered the opportunity to customize your build. The default options usually work well.
Once your options selected, you click onConfigure
again.
And you click onGenerate
to complete the installation.
This complete the installation step, you can closeCMake
. - You are now ready to compile the OpenCV library. Look inside the created
C:\OpenCV2.4
directory; you find find there a Visual Studio solution calledOpenCV.sln
. This is the Visual Studio project you have to open.
This project will allow us to compile the OpenCV sources. We need to do this in Debug and Release modes. Since we want to compile everything with the 64-bit compiler, the solution platform should bex64
.
If it is not the selected platform, go toConfiguration Manager
under theBuild
menu. You select here thex86
platform. If this choice is not offered, click onNew...
. Of course, you can leave the selected platform asWin32
in which case, your OpenCV will be compiled in 32-bit mode.
You can then simply build all (F7 orBuild Solution
under theBuild
menu).
Repeat the same process but this time in Release mode.
To finalize the installation you have to right-click onINSTALL
of theCMakeTargets
project; you then selectBuild
.
This last step will install the library and the include files in theinstall
directory. Note that for clarity, you can rename this directory asinstall-vs2012x64
since you ask CMake to build a Visual Studio 64-bit library.
We are done with the installation! - Before we build our first OpenCV project, we need to add the OpenCV bin folder to the
Path
environment variable. Indeed, you need to tell your system where to find the OpenCV dlls. You do it from the System Properties menu that you access from the Control Panel.
We need to edit thePATH
environment variable.
You must add theC:\OpenCV-2.3.1\install-vs2010x64\bin
dir to the list of paths. - Our last step is to build a simple OpenCV project to make sure everything is working properly. Start Visual Studio and select a Win32 Console application.
This will create a template project that we will modify.
Go to the Properties Manager tab.
We will create two new Property Sheets that we will be able to reuse in our future projects. These sheets will specify the OpenCV directories and library names. Rigth-click onDebug|Win32
and selectAdd New Property Sheet
. Give a name to the Property Sheet and position it at a generic location you will be able to access in the future.
Click onAdd
. We first specify the OpenCV directories.
First the include directory.
Then the library directory.
The next step is to list the library files.
Depending on the application, you will need different library packages. The following are the most common.
Note that for the Debug mode, you should select the libraries ending with ad
.
You will repeat the same process for the Release Property Sheet.
Our test program will simply open and display an image:#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> int main() { // read an image cv::Mat image= cv::imread("img.jpg"); // create image window named "My Image" cv::namedWindow("My Image"); // show the image on window cv::imshow("My Image", image); // wait key for 5000 ms cv::waitKey(5000); return 1; }
Make sure you have an image calledimg.jpg
in your project directory. To compile and run it, click on the green arrow.
and you should see the image displayed.
Wow! But this is just the beginning, you can do much more with OpenCV... Good