Install OpenCV 3.1 for Java on OS X

I put here the information about installing OpenCV on OS X from a few sources together to get all required information at one place.


Building OpenCV shared Libs:

  • Download OpenCV and unzip it.
  • Create a new folder SharedLibs inside the OpenCV folder.
  • Run CMake (/Applications/CMake)
    • Click Browse Source and navigate to your openCV folder.
    • Click Browse Build and navigate to your SharedLib Folder.
    • Click the Configure button.
      • Choose Unix-Makefile
      • Uncheck BUILD_TESTS
      • Set CMAKE_OSX_SYSROOT to something like '/Applications/'
      • Set CMAKE_OSX_ARCHITECTURES to 'x86_64' 
      • Uncheck WITH_1394
      • Uncheck WITH_FFMPEG
    • Click the Configure button again.
    • Click Generate.
  • Open Terminal an type the following commands:
    cd <path/to/your/opencv/SharedLibs/folder/>
    sudo make install
  • Now there is still the mac's dynamic Library missing:
    In the Terminal go to the  OpenCV installation folder. On my System these is the folder /usr/local/share/OpenCV/java which contains the two files opencv-310.jar and  In these  folder create a symbolic link for the dynamic library.
    cd /usr/local/share/OpenCV/java
    sudo ln -s libopencv_java310.dylib

Configure Eclipse for OpenCV

  • Start Eclipse and select Eclipse / Preferences
  • Navigate to Java / Build Path / User Libraries
  • Click the 'New…' Button and enter a name. E.g. OpenCV-3.1.0 and click Ok
  • Now select your new user library and click 'Add External JARs…'
  • Browse to the OpenCV installation folder (/usr/local/share/OpenCV/java) and chose the opencv-310.jar file.
  • Expand the entry for opencv-310.jar, select 'Native library location' and click 'Edit…'
  • Select 'External Folder…' and browse to OpenCV installation folder. Make sure you didn't select a file and klick 'Open', The Location path should look something like '/usr/local/share/OpenCV/java'. Then click 'Ok' 
  • Click 'OK' once more to to close the Preferences Window.

Java OpenCV test project

  • Create a new project. At the 'Java Settings' step select 'Add Library…', choose 'User Library'  and click 'Next >'
  • Select OpenCV_3.1.0 and click 'Finish'.
  • Create a Class with a main Methode and ad some OpenCV code.
    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    public class Hello
       public static void main( String[] args )
          System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
          Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
          System.out.println( "mat = " + mat.dump() );
  • The console output should look like this:
    mat = [  1,   0,   0;
             0,   1,   0;
             0,   0,   1]


Print Email

Add comment

Security code