Tuesday, January 13, 2009

Compiling OpenCV for Gumstix

It's common sense that OpenCV is one of the best computer vision libraries available nowadays and it's certainly very useful to benefit from it in embedded environments. There's a trade-off between locally processing images and uploading them for remote processing, but some algorithms might as well run smoothly on embedded devices.

In order to compile OpenCV for Gumstix, firstly one needs to download it from:

The version I've testes was opencv-linux 1.1pre1, but I believe any other release will barely follow the same ideas provided here.
After the file has been extracted (tar -xzvf opencv-1.1pre1.tar.gz), one should configure the environment variables for the compiler:

export CC=/home/developer/gumstix/gumstix-oe/tmp/cross/bin/arm-angstrom-linux-gnueabi-gcc
export CXX=/home/developer/gumstix/gumstix-oe/tmp/cross/bin/arm-angstrom-linux-gnueabi-g++

$ ./configure --host=arm-linux --build=i686-linux --prefix=/home/developer/opencvgum --without-gthread --without-gtk --without-python --disable-apps

Notice that we've defined that the prefix=/home/developer/opencvgum is the place it will be installed when we type 'make install'. By the way, make sure you have created this directory.
Be sure to substitute the /home/developer path to your user path, as well as the /gumstix/gumstix-oe/ to your installed gumstix environment.
We've also disabled the gtk environment since we are not interested in running the GUI applications inside the gumstix. I've also disabled python and building the applications.
Now that configuration has been successful. Type:

$ make

And then:

$ make install

If everything went well, you'll have the binaries and samples installed to /home/developer/opencvgum
Now, it would be useful to try and compile the samples so that we are sure they will run in the Gumstix.
In order to build them, go to /home/developer/opencvgum/share/opencv/samples/c and edit the build_all.sh script.
Make it executable:

$chmod +x ./build_all.sh

And then change all gcc and g++ to its arm-likes. My build_all.sh ended up like this:

export PKG_CONFIG_PATH=/home/developer/opencvgum/lib/pkgconfig/

if [[ $# > 0 ]] ; then
        base=`basename $1 .c`
        echo "compiling $base"
        /home/developer/gumstix/gumstix-oe/tmp/cross/bin/arm-angstrom-linux-gnueabi-gcc -ggdb `pkg-config opencv --cflags --libs` $base.c -o $base
        for i in *.c; do
            echo "compiling $i"
            /home/developer/gumstix/gumstix-oe/tmp/cross/bin/arm-angstrom-linux-gnueabi-gcc -ggdb `pkg-config --cflags opencv` -o `basename $i .c` $i `pkg-config --libs opencv`;
        for i in *.cpp; do
            echo "compiling $i"
            /home/developer/gumstix/gumstix-oe/tmp/cross/bin/arm-angstrom-linux-gnueabi-g++ -ggdb `pkg-config --cflags opencv` -o `basename $i .cpp` $i `pkg-config --libs opencv`;

Notice that we've also defined the export PKG_CONFIG_PATH=/home/developer/opencvgum/lib/pkgconfig/ so that the correct includes and linked libraries are built correctly.
Run this command and you'll notice the executable files will be created. I think that if you don't disable the flag "--disable-apps" in the configure application and make this change to the build_all.sh earlier it might also work.

Well, now that OpenCV has been built, you should be able to copy it to your gumstix. There's a small problem though. If you compact the files, you'll notice it's very big, so, a good idea is to delete a couple files we are sure we are not going to use.
An advice would be to delete some of the haarcascades.

Choose some of them you are sure you won't use, like the haarcascade_profileface.xml inside the data sub-directory, for instance. Delete a couple others as well.
This way, you'll be able to create a .tar.gz of around 6.5MB.
When it's done, copy it to your gumstix through scp:

$scp opencvgum.tar.gz root@192.168.YOUR-GUM.IP:/tmp

Make sure you copy it to /tmp, because you'll probably be out of space copying it somewhere else.
Extract it and then try to run one of the demos that does not use windows... the  ./letter_recog application, for instance... it's located at:

Well, you might be able to see it running. Else, some libstdc++.so is missing error could also happen.
This means you don't have this library installed. One easy way to install it is through the command

$ipkg install libstdc++6

In case some other libraries are missing as well, repeat the procedure with their names. Notice that some packages have odd names.
For instance, if you had typed:
$ root@gumstix-custom-verdex:/usr/share$ ipkg install libstdc++

Then you'd have received the following message:

Nothing to be done
An error ocurred, return value: 4.
Collected errors:
Cannot find package libstdc++.
Check the spelling or perhaps run 'ipkg update'

Actually the name of the library is libstdc++6. Make sure you type the correct library name.

Well, in case libstdc++ is really installed, you might as well get some error like:

./letter_recog: error while loading shared libraries: libcxcore.so.2: cannot open shared object file: No such file or directory

It means the LD_LIBRARY_PATH is not pointing at your opencv libraries.
Simply type:

$ export LD_LIBRARY_PATH=/tmp/opencvgum/lib/

You will eventually be able to run your letter_recognition application.
Well, in case you want to run other applications, like the face recognition one, make sure you disable the GUI related functions and write your results to files.
We'll be able to see it working at Campus-Party Brasil
Make sure you'll be there!


Anonymous said...

As opencv is written by Intel you will probably see poor performance on the ARM processors.... Cheers

Daniel Lélis Baggio said...

Well, I don't think IPPL gives that much of a boost. Correct me if I'm wrong :)


Azhar said...

Hi Danny

How would I modify the following configure command to enable GUIs, ffmpeg etc. on the Gumstix opencv?

$ ./configure --host=arm-linux --build=i686-linux --prefix=/home/developer/opencvgum --without-gthread --without-gtk --without-python --disable-apps


Daniel Lélis Baggio said...

Hey Azhar,
I'm not with the script right now here, but I think
./configure --help
will show you all the options.
Good luck,
kind regards,

Anonymous said...

Hi Danny,

Was just wondering how this went - you have openCV up and running on a Gumstix board? How is the performance?

Thanks for posting these instructions.

Ritesh Ranjan said...

Hi I tried to modify the following configure command to enable GUIs but it failed. Do u have any idea?