OpenCV for Windows: easy installation using CMake

OpenCV is growing exponentially as do the associated manuals, tutorials and installation guides that can be found elsewhere (including the complete official guide). Nevertheless I will like to share the way I’ve learnt to create Visual Studio projects that use OpenCV. It’s a simple and fast way that allow you to create projects in almost no time.

The solution is to use CMake, and avoid to configure the include directories, libraries and so on for each single project. In a few steps you will have a CMakeLists.txt file that can be used to generate an empty project with all the dependencies and required OpenCV libraries that you can use as the basis for your project.

Here you are the steps:

1.- Install Tortoise SVN to download the latest OpenCV version

Although you can download the binaries directly, I strongly recommend to spend a few minutes to download instead the source code and compile it. It’s simple!

First, download and install Tortoise SVN, which is a nice tool to have access to SVN repositories (take a look to wikipedia in case you do not know yet and are interested about SVN), and CMake.

Second, create a folder called OpenCV somewhere in your PC, for instance C:\OpenCV. Then right-click and press SVN Checkout. A window will appear where you have to specify the url of the OpenCV SVN repository:

Click OK and finally you will see how all the OpenCV source code is downloaded into your computer. Currently the repository contains two main folders C:/OpenCV/opencv and C:/OpenCV/opencv_extra. We will focus on the former one. Whent its done, open the CMake GUI (I like the GUI, although you can learn how to do it by commands):

Press configure and select the version of Visual Studio you have. Press it again to configure the solution. And when done, press Generate.

Now you have a BUILD folder which contains the OpenCV.sln visual studio solution file and everything configured and necessary to compile OpenCV. I like to have the BUILD folder inside the same folder, C:/OpenCV/opencv/BUILD, although you can set it wherever you like.

2.- Compile OpenCV, in Release and Debug mode

Start OpenCV.sln and build the whole solution, in Release, and when done, in Debug  (the order is not important). This will create the necessary OpenCV libraries that you will need for your projects.

If you selected to build the examples as well (during the configure step within the CMake), you will have a nice set of OpenCV examples that you can run to check how it works!

I recommend to set the opencv path to the binaries in the PATH variable, so that you won’t have to copy all the dll files in your projects during development (remember you will need to have them in case you install your project into a different PC without OpenCV installed).

So add to your path the following C:\OpenCV\opencv\BUILD\bin\Release;C:\OpenCV\opencv\BUILD\bin\Debug;.

3.- Create  a CMakelists.txt file for your first OpenCV project

Once you have compiled OpenCV you can create your first OpenCV project. It’s simple, just create a folder for your project wherever you want, for instance C:\myProject. Inside this folder you can order things as you want. I like to have a docs subfolder for documentation, a videos or images in case I need them, and so on. Apart from it, I have one for the source code C:\myProject\src.

We just need to create a CMakeLists.txt file in C:\myProject, with the following content:

PROJECT( myProject )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( myProjectExe main.cpp )
TARGET_LINK_LIBRARIES( myProjectExe ${OpenCV_LIBS} )

Make sure you have a main.cpp file in C:\myProject\src, and that is a correct c/cpp file that uses OpenCV, for instance:

#include 
#include 
using namespace cv;
int main ( int argc, char **argv )
{
  /// Read image 
  Mat img = imread("someImage.jpg");
  if( !img.data ) { printf("Error loading img \n"); return -1; }

  /// Create Windows
  namedWindow("Image", 1);

  /// Show image
  imshow("Image", img);

  /// Press any key to continue
  cvWaitKey();
  return 0;
}

Now go to CMake and create your solution as you did for OpenCV. Set the BUILD folder with CMake (it will create it for you), for instance in C:\myProject\code\BUILD. Press configure, configure and generate and your myProject.sln file will be created, ready to build with Visual Studio.

And that’s all!

This entry was posted in OpenCV and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s