26

Is there any way to get OpenCV from repository? Which artifact should I add to pom.xml? Every tutorial I'd found is from '14 and it seems like something changed - they say it is'nt in official Maven repository yet, but I've found entry:

<!-- https://mvnrepository.com/artifact/nu.pattern/opencv -->
<dependency>
   <groupId>nu.pattern</groupId>
   <artifactId>opencv</artifactId>
   <version>2.4.9-7</version>
</dependency>

Sadly, I get error

Caused by: java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path

when I'm using System.loadLibrary(Core.NATIVE_LIBRARY_NAME). Can I add this library in a way that would make my project include it and 'forget' about manually adding it to classpath?

deem
  • 1,252
  • 1
  • 19
  • 38
  • That doesn't look like to be an official release.. look at https://github.com/Itseez/opencv/issues/4588 – Tunaki Jun 18 '16 at 21:03
  • Oh, and I hoped it would be much easier with Maven. Can you tell me then, how can I add this library to WildFly? – deem Jun 18 '16 at 21:10

8 Answers8

23

Add the following dependency in your POM file:

<dependency>
    <groupId>org.openpnp</groupId>
    <artifactId>opencv</artifactId>
    <version>3.2.0-0</version>
</dependency>

and replace the following lines:

System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

with

nu.pattern.OpenCV.loadShared();

This should solve the problem in WINDOWS also. Happy Coding.

21

This worked for me.

nu.pattern.OpenCV.loadLibrary();

I'm using following maven dependency

<dependency>
  <groupId>nu.pattern</groupId>
  <artifactId>opencv</artifactId>
  <version>2.4.9-4</version>
</dependency>
Sachin Aryal
  • 781
  • 14
  • 36
  • 2
    This worked for me. Just replace System.loadLibrary(Core.NATIVE_LIBRARY_NAME) with OpenCV.loadLibrary(); – tomtomssi Mar 08 '17 at 23:37
  • 2
    I am getting this error " Operating system "WINDOWS" and architecture "X86_64" are not supported. at nu.pattern.OpenCV.loadLibrary(OpenCV.java:187)". Anyone had this problem? – John Mar 21 '17 at 14:50
  • 2
    @John, I unzipped the opencv jar and I didn't find the library for windows, only for Linux and MacOS. This might be the reason why it didn't work for you. I have no suggestions on this case :( – luizfzs Jun 13 '17 at 13:30
  • @luizfzs Thank you for your help. – John Jul 31 '17 at 15:51
  • This groupId unfortunately only contains OpenCV 2. If you need newer features like for example **DNN (Deep Neural Networks)**, use the one below (org.openpnp) with version 3.4.2-0 – jenald Aug 27 '19 at 11:55
  • If you are getting runtime error - checkout this answer - https://stackoverflow.com/a/64799844/9640177 – Mayank Kumar Chaudhari Nov 12 '20 at 07:51
  • Downvote, as ***unmaintained***, use the maintained fork package org.openpnp instead (see in that same page) – Remy Mellet Jan 08 '21 at 11:05
7

Try this, see if it works:

  • nu.pattern.OpenCV.loadShared();
  • System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);

More info here in API section: https://github.com/patternconsulting/opencv

Also have 2.4.9-7 opencv dependency.

asmmahmud
  • 4,844
  • 2
  • 40
  • 47
Melinda
  • 71
  • 1
  • 1
  • PS loadShared is no longer usable in Java 12+ including the new LTS version 17. This means you have to install the exact version of opencv java (unlikely to be in apt) and add to java.libraries.path. I've yet to get this to work – Novaterata Oct 08 '21 at 15:17
6

There is currently no official way to use the official Java bindings for OpenCV as a Maven dependency (as already mentioned in the comments, the Maven artifact was already requested in #4588, but is still unattended). Nevertheless, there are 3 possible approaches to your problem:

  • java.lang.UnsatisfiedLinkError was thrown because you need to install the binding's binaries (that is "opencv_java") separately. Most likely, that unofficial artifact does not include them (or not the ones compatible with your system). In order to build the bindings:

    1. git clone the OpenCV repository.
    2. git checkout the intended version (it appears that you are using version 2.4.9, although more recent versions are available)
    3. Follow the instructions here to build OpenCV and its Java bindings, thus yielding a dynamically linked library ("opencv_java249.dll", "libopencv_java249.so", or something else depending on your OS).
    4. Copy the shared library file to your java.library.path (again, this variable is system-dependent, but can be defined when running your application). At this point you should be ready to use that artifact.
  • An alternative is to use other bindings: the JavaCPP presets for OpenCV seem to work just as nicely as the official ones, and these are registered in maven (binaries for various platforms included!). Just remember that the API may not be exactly the same.

  • This solution may sound too far out, but it has legitimately worked for me in the past. Basically, you can avoid using the bindings: implement your solution in C++, then either link it with the JVM via JNI or make it a separate application, used by the main application via other mechanisms of your system (process spawning, I/O channels, you name it). For instance, I have once made a service component for feature extraction that other programs would connect to via ZeroMQ sockets.

E_net4
  • 27,810
  • 13
  • 101
  • 139
3

Just use it nu.pattern.OpenCV.loadShared();

write a class with this static void method

class Test {
public static void loadOpenCVNativeLibrary() {
nu.pattern.OpenCV.loadShared();
}
}

and after call it in your application class (with static main) for web application (spring boot for example) like this

static {
Test.loadOpenCVNativeLibrary();
}
...
public static void main(String[] args) throws UnknownHostException {
}
Pr4y
  • 31
  • 5
2

All you need: install jar in local maven repository with:

mvn install:install-file -Dfile=C:\opencv411\build\java\opencv-411.jar -DgroupId=org -DartifactId=opencv -Dversion=4.1.1 -Dpackaging=jar

create a dependency in pom.xml:

 <dependency> 
 <groupId>org</groupId> 
 <artifactId>opencv</artifactId>
 <version>4.1.1</version>  
</dependency> 

Now that jar is on, we need to somehow add the OpenCV libraries. I did this by adding the lib folder in java.library.path to the maven-surefire plugin:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.22.2</version>
 <configuration>
  <argLine>-Djava.library.path=${project.build.outputDirectory}/lib</argLine>
 </configuration>
</plugin>

    public static void main(String[] arges) throws MalformedURLException, 
IOException, Exception {
    loadLibraries();

    // create and print on screen a 3x3 identity matrix
    System.out.println("Create a 3x3 identity matrix...");
    Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
    System.out.println("mat = " + mat.dump());

    // prepare to convert a RGB image in gray scale
    String location = "resources/Poli.jpg";
    System.out.print("Convert the image at " + location + " in gray scale... ");
    // get the jpeg image from the internal resource folder
    Mat image = Imgcodecs.imread(location);
    // convert the image in gray scale
    Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY);
    // write the new image on disk
    Imgcodecs.imwrite("resources/Poli-gray.jpg", image);
    System.out.println("Done!");

}

    private static void loadLibraries() {

    try {
        InputStream in = null;
        File fileOut = null;
        String osName = System.getProperty("os.name");
//            String opencvpath = System.getProperty("user.dir");
        String opencvpath = "C:\\opencv411\\build\\java\\";
        if (osName.startsWith("Windows")) {
            int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
            if (bitness == 32) {
                opencvpath = opencvpath + "\\x86\\";
            } else if (bitness == 64) {
                opencvpath = opencvpath + "\\x64\\";
            } else {
                opencvpath = opencvpath + "\\x86\\";
            }
        } else if (osName.equals("Mac OS X")) {
            opencvpath = opencvpath + "Your path to .dylib";
        }
        System.out.println(opencvpath);
//        System.out.println("Core.NATIVE_LIBRARY_NAME = " + Core.NATIVE_LIBRARY_NAME);
        System.out.println("Core.NATIVE_LIBRARY_NAME = " + "opencv_java411.dll");
//        System.load(opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll");
        System.load(opencvpath + "opencv_java411.dll");
    } catch (Exception e) {
        throw new RuntimeException("Failed to load opencv native library", e);
    }
}
user1198289
  • 637
  • 1
  • 5
  • 14
1

For those who wants to use OpenCV 3.2 in MacOs environment, you can use following repository definition:

<repositories>
   <repository>
      <id>kodfarki</id>
      <url>https://raw.githubusercontent.com/kodfarki/repository/master/</url>
   </repository>
</repositories>

There is also an example project in https://github.com/kodfarki/opencv-example.

To use this example project, you still need to install OpenCV binaries

brew tap homebrew/science brew install opencv3 --with-java --with-contrib

Halil
  • 1,795
  • 1
  • 24
  • 39
1

For windows there was a problem with @Sachin Aryal's answer. The answer by @Anirban Chakraborty is a very good hint. But, there was still issues at runtime as described in this thread.

Finally replacing OpenCV.loadShared(); with OpenCV.loadLocally(); worked for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122