Use OpenCV in your source code!
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/.
Install CMake http://www.cmake.org, and learn how to use it (takes 15 minutes).
- Install OpenCV. There is a linux package for most distributions and a binary installer for Windows.
Download file FindOpenCV.cmake and place it in the Modules directory of your CMake installation (where also FindOpenGL.cmake can be found). This will enable CMake to find OpenCV on your machine. I have tested it under WinXP and Ubuntu, and it just worked without any hacking.
Test your workstation by compiling a Hello World program
You probably know CMake by now, but I provide a full hello world program here. You have to make two files: hw.cxx is your program code, and CMakeLists.txt defines your configuration for compilation.
prog.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( pr )
FIND_PACKAGE( OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OPENCV_INCLUDE_DIR} )
ADD_EXECUTABLE( hw hw.cxx )
TARGET_LINK_LIBRARIES( hw ${OPENCV_LIBRARIES} )Having these files in a directory, make it your current directory, configure and compile your program.
On Linux:
$> cmake . $> make
On Windows, use the graphical config tool of CMake and select MinGW for compiler.
$> CMakeSetup . $> make
If you get an executable hw program, you are ready to move on and have fun with OpenCV!
