1

I need to install opencv and use it via java in Maven+IntelliJ (on Windows and macOS/OSX). What is the "standard" or easy way?

Dmitry Konovalov
  • 508
  • 7
  • 16
  • Found Maven http://mvnrepository.com/artifact/org.openpnp/opencv/2.4.13-0 but "import org.opencv.imgcodecs' was not resolved. – Dmitry Konovalov Nov 16 '16 at 06:07
  • Found a good how-to: http://www.rmnd.net/install-and-use-opencv-3-0-on-mac-os-x-with-eclipse-java/ . It starts from downloading opencv...zip, which solves the problem of what version is installing. – Dmitry Konovalov Nov 16 '16 at 23:00

1 Answers1

4

I googled extensively and did look at How to use OpenCV with IntelliJ IDEA 12. Here is what worked for me and it looks simple enough. Please comment if there is a "better" and/or "standard" way. Wish list: I would like to be able to remove opencv easily from my computer if not needed anymore (some of my laptops run out of disk space).

The following is done on macOS Sierra (10.12.1)

Step1: download and install opencv via command-line (i.e. Terminal on macOS/OSX): see http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html

UPDATE 17-Nov-2016: http://docs.opencv.org/3.0-beta/doc/tutorials/introduction/linux_install/linux_install.html

$ mkdir ~/dev/lib/opencv/github_opencv_161101
$ cd ~/dev/lib/opencv/github_opencv_161101
$ git clone https://github.com/opencv/opencv.git

I used ~/dev/lib/opencv/github_opencv_161101 as my install directory, so I could remove it easily if needed, and to remind myself when I installed it.

Step 2: build. Note, the last command below $ sudo make install will copy all libs to /usr/local. However, I tried to install opencv via at least two other ways (via brew and http://opencv.org/platforms/android.html), so I am not sure which libraries are which in /usr/local.

$ cd opencv
$ mkdir release
$ cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. 
$ make
$ sudo make install

Notes on used tools: You also need JavaSDK installed. My cmake is

$ cmake -version
cmake version 3.6.2

If you do not have cmake installed, use brew (or install from http://brew.sh/ )

$ brew install cmake

You may also need to install Command Line Tools not working - OS X El Capitan/ macOS-sierra via

$ xcode-select --install

Step 3: Running HelloCV.java in IntelliJ. Start IntelliJ. Create New Project. Select Maven enter image description here

Continue until you get enter image description here Enable auto-import!

Create new class HelloCV and type (from https://github.com/opencv-java/opencv-java-tutorials/blob/master/docs/source/02-first-java-application-with-opencv.rst):

public class HelloCV {
    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());
    }
}

You should get something like: enter image description here

Now, finally the payload ;-) Select File->Project Structure: enter image description here

Select Libraries (or Global libraries), and add java library: enter image description here

From https://stackoverflow.com/a/24853043/1006226: Navigate to ~/dev/lib/opencv/github_opencv_161101/opencv/release/bin in my case, or ~/path_to_your_dir/opencv/release/bin. enter image description here

You can now compile and run your HelloCV.java:

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class HelloCV {
    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());
    }
}

But you will get the following runtime error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java310 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at org.dak.HelloCV.main(HelloCV.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1

Go back to Menu->File->Project Structure, select Libraries and add ~/dev/lib/opencv/github_opencv_161101/opencv/release/lib in my case, or ~/path_to_your_dir/opencv/release/lib

enter image description here enter image description here

Now, you are linked to the native libs via JNI. Run HelloCV and you should get:

mat = [  1,   0,   0;
   0,   1,   0;
   0,   0,   1]

Process finished with exit code 0
Community
  • 1
  • 1
Dmitry Konovalov
  • 508
  • 7
  • 16
  • This looks awfully complicated for just fetching opencv to build/run. The simplest way I found is to host the opencv on your own repo. Then copy it on each build with a maven plugin and load it via a relative path. I've written a short 2-part tutorial on how I did it: http://dindoffer.eu/2016/12/30/opencv-with-maven-part-1-create-your-own-maven-repo-wo-high-level-bullshit/ – Leprechaun Jan 08 '17 at 19:06