JavaCV (OpenCV for Java) on Raspberry Pi

The Raspberry Pi has a powerful processor and GPU making it one of the few low cost embedded platforms suitable for machine vision and video processing.  The OpenCV computer vision library is popular for C++ development.

Update 11/25/2017:
Benji found JavaCV too limited in that it used OpenCV 3.0.0 and old/poorly documented ‘C’ APIs where the newer documentation is all focused on C++. He built OpenCV with the Java bindings from source on an RPi3 using the latest Raspbian; you can read about it here

JavaCV uses JavaCPP to automatically construct JNI wrappers around the CPP OpenCV classes.  The port was done by Samuel Audet and you can read about it here.  Good sources of documentation are the OpenCV documentation and the JavaCV wiki.

The Raspbian repositories don’t have up-to-date versions of either OpenCV or JavaCV so you’ll need to install them yourself:

  1. The easy way:
  • Download the 4 pre-compiled binary .jar files here  (source page: here) or from this site: javacv-1.0.jar javacpp-1.0.jar opencv-3.0.0-1.0.jar opencv-3.0.0-1.0-linux-arm.jar
  • To test:
    • put them in a folder such as “./opencv”
    • create this test program:

      import static org.bytedeco.javacpp.opencv_core.*;
      import static org.bytedeco.javacpp.opencv_imgproc.*;
      import static org.bytedeco.javacpp.opencv_imgcodecs.*;

      public class Smoother {
      public static void smooth(String filename) {
      IplImage image = cvLoadImage(filename);
      if (image != null) {
      cvSmooth(image, image);
      cvSaveImage(filename, image);
      cvReleaseImage(image);
      }
      }

      public static void main(String[] args) {
      if (args.length > 0) {
      smooth(args[0]);
      }
      }
      }

    • Compile (note: the binary .jar files were build w/Java 7 so you must target java 7 for your bytecode too):

      javac -source 1.7 -target 1.7 -classpath “./opencv/*” ./Smoother.java

    • Execute on a sample image file; the output will overwrite the file with a smoothed version of the image:

      java -cp opencv/*:. Smoother Rough_piddock_BW.jpg

2. The hard way…build it all from source while wrestling with gobs of dependency issues:

  • OpenCV 3.1 install instructions are here and here.
  • You will need to install many dependencies including maven, ant,  and doxygen.  You can read about the dependencies for JavaCV and JavaCpp by following install instructions here or here