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

 

NOOBS on Raspberry Pi

Notes on setting up NOOBS (Debian Linux) on a Raspberry Pi

  1. Download NOOBS linux (offline version) from here and copy all of the files in the .zip to the root of an SD card
  2. Boot the SD card with a monitor and keyboard attached to the Pi
  3. Use the Pi GUI to configure the WiFi network (if desired)
  4. Configure the Pi to use a default IP on the Ethernet interface that’s on the same subnet as the PC auto-IP subnet (169.254.x.y) – I used 169.254.82.80 (‘R’.’P’); this will let you boot the Pi headless, connect it to the Ethernet port of your PC, and ssh into it with no other hardware.  To do this, modify the file /boot/cmdline.txt and add “ip=169.254.82.80” to the end of the single long line.  See details here.
  5. Login (default user is ‘pi’ and default password is ‘raspberry’)
  6. sudo apt-get update
  7. sudo apt-get upgrade