Processing + OpenCV on 64 bit Linux

OK, so I recently decided I wanted to try my hands on processing, opencv, hopefully eventually interfacing with some arduino hacks. Nothing special, nothing new, just in the mood of experimenting a little bit with video and computer vision, you've all seen how big and popular augmented reality is becoming.

Anyway, I configure my kernel to enable the video4linux ps2 camera drivers, I download processing, I install OpenCV, and finally get my hands on the OpenCV library for processing. You have all the documentation you need at the links above to set up the environment. So I'm all pumped and ready to roll, about to testdrive a simple processing+opencv code snippet when I get:

!!! required library not found : /usr/lib/libOpenCV.so: /usr/lib/libOpenCV.so: wrong ELF class: ELFCLASS32 (Possible cause: architecture word width mismatch)
Exception in thread "Animation Thread" java.lang.UnsatisfiedLinkError: hypermedia.video.OpenCV.capture(III)V
at hypermedia.video.OpenCV.capture(Native Method)
at hypermedia.video.OpenCV.capture(OpenCV.java:945)
at blobs.setup(blobs.java:38)
at processing.core.PApplet.handleDraw(PApplet.java:1403)
at processing.core.PApplet.run(PApplet.java:1328)
at java.lang.Thread.run(Thread.java:619)

We don't really need to be geniuses to figure out what was going on there. Obviously the library shipped is a 32 bit binary. In my case I am on a 64 bit system, with a 64 bit OpenCV. But looking into the OpenCV library for processing I was relieved to see that the sources were included (this is why open source is so beautiful). Anyway, all we really have to do to solve this is recompile the library. Here are the steps you should follow:

  1. change to the directory where you installed the  opencv library, in my case I put it in the libraries directory within the processing directory.[bash]cd /opt/processing-1.2.1/libraries/OpenCV/source/java/hypermedia/video[/bash]
  2. Compile the library.[bash]javac -classpath /opt/processing-1.2.1/lib/core.jar Blob.java OpenCV.java[/bash]
  3. Generate the C Header/Stub File using javah.[bash]cd ../../ && javah -jni hypermedia.video.OpenCV[/bash]
  4. Compile and link the actual libOpenCV.so OpenCV library for processing.
    [bash]g++ -shared OpenCV.cpp -o libOpenCV.so -I/usr/include/opencv/ -I/opt/sun-jdk-1.6.0.20/include -I/opt/sun-jdk-1.6.0.20/include/linux/ -lcv -lhighgui -fPIC[/bash]

Make sure the included  (-I) directories in the last command above refer to your current JDK. You need the $JDK_HOME/include/linux (as you can see, in my case $JDK_HOME was /opt/sun-jdk-1.6.0.20) to be able to link properly with jni.h.

Once you have your libOpenCV.so compiled and linked, all you need to do is replace the 32 bit version that was originally shipped:

[bash]cp libOpenCV.so /opt/processing-1.2.1/libraries/OpenCV/library/[/bash]

And that's it really, you are now good to go and start playing around!

Written on August 29, 2010