Wednesday 8 August 2012

Game Player - Step 1 - Setting Up and Using OpenCV

This is the part I hate the most because it can be fraught with errors and you can end up spending masses of time on what is essentially just setting up the environment. I don't mind spending ages on a problem when it is a problem in development or trying to work out how to solve a tricky algorithm issue but spending ages on just setting something up drives me mad. Hopefully it will go smoothly this time...



You can get the latest version of OpenCV from it's home at http://opencv.willowgarage.com. Although we may not necessary need the latest version we will use it anyway so that we have access to all the latest bug fixes and features. Also it has the added bonus that the Windows version, the version I plan on using, comes pre-built hence removing a lot of the issue causing tasks I mentioned earlier.

Go to the downloads section and get the latest version (version 2.4.2 as of posting). If you are running Windows you can get an .exe file that will take care of most of the installation for you, if you aren't then I'm afraid you're stuck building and compiling yourself. It's ok though, you're on a non-Windows system so you're obviously smart and there are pretty clear instructions for most popular operating systems.

Assuming you are in Windows, download and run the installer. I'm installing into the directory C:\OpenCV2.4.2 so whenever you see me refer to here just substitute in your installation directory if you've installed elsewhere. Inside the directory you'll find one folder called opencv and inside there you'll find a whole host of files and folders:
The files and folders of OpenCV2.4.2
There is only a couple of these we are interested in and even then we only need to point our IDE to their folders and not actually manipulate them ourselves. So that said let's head back into Netbeans and get it set up to use OpenCV.

Go into your C project and open the Properties window. Select 'Build' -> 'C++ Compiler' and go to 'Include Directories'. There will already be 2 locations there that we added earlier, we are now going to add an OpenCV directory to the list, namely the 'include' directory at 'C:\OpenCV2.4.2\opencv\build\include'.

Then you want to jump into the 'Linker' tab and add the 'lib' directory into the 'Addition Library Directories' field. In theory the location of the 'lib' folder will depend on the architecture of the system you are using. If you are using a 64 bit system use 'C:\OpenCV2.4.2\opencv\build\x64\mingw\lib' if you are using 32 bit then use 'C:\OpenCV2.4.2\opencv\build\x86\mingw\lib'. I make a point of saying in theory because I keep getting a 'File format not recognised error' when I try and use the 64 bit ones hence I have to use the 32 bit ones despite being on 64 bit Windows.

Once you have this set go into the 'Libraries' field and add all the files from the 'lib' directory using the 'Add Library' button. It should end up looking like this:
OpenCV library files added from 'lib' folder.
We also need to add 'C:\OpenCV2.4.2\opencv\build\x64\mingw\bin' or 'C:\OpenCV2.4.2\opencv\build\x86\mingw\bin' to the system path again depending on which libraries you are using. To do this right click on 'My Computer' go to 'Properties' -> 'Advanced System Settings' -> 'Environment Variables' and select PATH from the bottom window then hit 'Edit' and add the above path to end of the list with a semicolon ( ; ) to separate it from the previous entry:
Adding the 'bin' folder to the system path.
And now we should be able to use OpenCV!

Go back into the main GamePlayer.cpp class in the C project and add an include for the OpenCV file highgui.hpp. This is done by adding the line:
#include "opencv2/highgui/highgui.hpp"

To the top of the file next to the rest of the includes. Then add the lines:

cvNamedWindow("Hello World", CV_WINDOW_AUTOSIZE);
cvWaitKey(0);

just after the call to fflush(stdout) but before the env->ReleaseIntArrayElements(data, elem, 0).

Clean and build the C project and then run the Java project. If all has went well a window titled 'Hello World' should appear showing nothing but a grey box and it should disappear whenever a key is pressed:
Window generated by OpenCV.
This is pretty unexciting in itself but it does show that our calls to the OpenCV library are working since it is making our window for us. We can also see we are still getting our output from the rest of the project in the background.

I'm going to post up about my problems getting the 64 bit versions of the libraries running in the next post and will hopefully have some answers for you as well. Until then I suggest playing about with OpenCV now that we have it going, try loading in some images from a file or if you want to get a head start on our next step trying finding out how to convert our image data back into an image OpenCV can use.

Catch you next time.

No comments:

Post a Comment