Use OpenCV in your source code!
The recommended way of using OpenCV in your code is to use CMake with your code. If you are not using CMake with your code, the intructions below are not for you.
Solution
There are many ways to include OpenCV in your projects. I describe my way here which has the following advantages:
- No need to change anything when porting between Linux and Windows.
- Can easily be combined with other tools by CMake. (I use QT, ITK and VTK.)
Setup your workstation:
Install GCC on linux, or MinGW on Windows http://www.mingw.org/, Visual Studio for Windows.
Install CMake http://www.cmake.org, and learn how to use it (takes 15 minutes).
- Download OpenCV 2.0.0a source code. Unpack it in a folder, I will call this folder MYHOME/src/OpenCV from now on.
- Start CMake. Choose the source folder as MYHOME/src/OpenCV . Choose a folder for your build. I will use MYHOME/build/OpenCV from now on.
- Click Configure. Choose the options that you want (such as building Python wrapper, Octave wrapper, ...).
- Keep clicking configure, resolve conflicts until you can click on Generate.
- Click on Generate.
- Navigate to MYHOME/build/OpenCV, there should be a solution/make file there.
- Build OpenCV (in Windows, this means open the OpenCV.sln file and build. In other Makefile-based systems, this means type make -j8).
Test your workstation by compiling a Hello World program
You probably know CMake by now, but I will provide a full hello world program here. You have to make two files: helloworld.cxx is your program code, and CMakeLists.txt defines your configuration for compilation.
helloworld.cxx looks like this:
#include <cv.h>
#include <highgui.h>
int main ( int argc, char **argv )
{
cvNamedWindow( "My Window", 1 );
IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
CvFont font;
double hScale = 1.0;
double vScale = 1.0;
int lineWidth = 1;
cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,
hScale, vScale, 0, lineWidth );
cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font,
cvScalar( 255, 255, 0 ) );
cvShowImage( "My Window", img );
cvWaitKey();
return 0;
}CMakeLists.txt looks like this:
PROJECT( helloworld_proj )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( helloworld helloworld.cxx )
TARGET_LINK_LIBRARIES( helloworld ${OpenCV_LIBS} )It is assumed that you'll put the above two files (helloworld.cxx and CMakeLists.txt) in a folder. We will call this folder MYHOME/src/hw.
- Start CMake and choose your source folder to be MYHOME/src/helloworld
- Choose your build folder. I will choose MYHOME/build/helloworld
- Click Configure
- You will now have a variable called OpenCV_DIR. This variable is most likely pre-set to C:\OpenCV2.0.0 if you are on Windows. It may be undefined on other systems.
- Make sure you set the above variable to your BUILD folder of OpenCV, i.e. MYHOME/build/OpenCV. Since you built OpenCV with CMake, it remembers the path and all necessary stuff so that choosing the build folder will automatically pick up the necessary library path, include the correct include directory, set the correct list of library. In other words, the magic of CMake happens.
- Now you can click Generate to create the solution/makefile of your choice.
- Open this solution/makefile and build it.
If you get an executable helloworld program, you are ready to move on and have fun with OpenCV!