Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java OpenCV from Maven

Tags:

java

opencv

maven

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?

like image 214
deem Avatar asked Jun 18 '16 20:06

deem


People also ask

Can you use OpenCV with Java?

4, OpenCV supports desktop Java development using nearly the same interface as for Android development. This guide will help you to create your first Java (or Scala) application using OpenCV.

What is OpenCV in Java?

OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection.

How do I get OpenCV jar?

From the menu navigate under Java > Build Path > User Libraries and choose New... . Enter a name for the library (e.g., opencv) and select the newly created user library. Choose Add External JARs... , browse to select opencv-3xx. jar from your computer.

What is JavaCV?

JavaCV is a Java wrapper for commonly used computer vision libraries and uses JavaCPP technology for the binding of native libraries (based on JNI). JavaCPP Presets define the respective bindings (e.g. OpenCV, FFmpeg, OpenKinect, videoInput, flandmark, ARToolkitPlus, …) that can then be used within Java.


8 Answers

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.

like image 134
Anirban Chakraborty Avatar answered Oct 02 '22 20:10

Anirban Chakraborty


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>
like image 44
Sachin Aryal Avatar answered Oct 02 '22 21:10

Sachin Aryal


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.

like image 36
Melinda Avatar answered Oct 02 '22 19:10

Melinda


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.

like image 22
E_net4 stands with Ukraine Avatar answered Oct 02 '22 19:10

E_net4 stands with Ukraine


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 {
}
like image 32
Pr4y Avatar answered Oct 02 '22 20:10

Pr4y


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);
    }
}
like image 40
user1198289 Avatar answered Oct 02 '22 21:10

user1198289


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

like image 36
Halil Avatar answered Oct 02 '22 21:10

Halil


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.

like image 29
mayank1513 Avatar answered Oct 02 '22 20:10

mayank1513