Java OpenCV from Maven

Is there a way to get OpenCV from the repository? What artifact should be added to pom.xml ? Every tutorial I found is from β€œ14”, and it seems that something has changed - they say that it is no longer in the official Maven repository, but I found the entry:

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

Sorry, I am getting an error

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

when I use System.loadLibrary(Core.NATIVE_LIBRARY_NAME) . Is it possible to add this library so that my project will include it and β€œforget” about manually adding it to the classpath?

+16
source share
7 answers

It worked for me.

 nu.pattern.OpenCV.loadLibrary(); 

I am using the following maven dependency

 <dependency> <groupId>nu.pattern</groupId> <artifactId>opencv</artifactId> <version>2.4.9-4</version> </dependency> 
+16
source

Add the following dependency to 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 also fix the problem in WINDOWS . Good coding.

+16
source

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

  • java.lang.UnsatisfiedLinkError was generated because you need to install the binary binaries (i.e. opencv_java) separately. Most likely, this unofficial artifact does not include them (or are not compatible with your system). To build the bindings:

    1. git clone repository OpenCV.
    2. git checkout intended version (it looks like you are using version 2.4.9, although more recent versions are available)
    3. Follow the instructions here to create OpenCV and its Java bindings, which provides 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 the application starts). At this point, you should be prepared to use this artifact.
  • An alternative is to use other bindings: JavaCPP presets for OpenCV seem to work just as well as the official ones, and they are registered in maven (binaries for various platforms are included!). Just remember that the API may not match.

  • This decision may seem too far, but in the past it justified me completely. Essentially, you can avoid the use of bindings: implement your solution in C ++, and then either link it to the JVM via the JNI, or make it a standalone application used by the main application through other mechanisms of your system (spawning a process, I / O channels, you name this is). For example, I once created a utility component for extracting functions to which other programs could connect via ZeroMQ sockets.

+6
source

Try it, see if it works:

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

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

There is also a 2.4.9-7 opencv dependency.

+6
source

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

write a class using this static void method

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

and after calling it in your application class (with static main) for the web application (e.g. w760> boot), e.g.

 static { Test.loadOpenCVNativeLibrary(); } ... public static void main(String[] args) throws UnknownHostException { } 
+2
source

For those who want to use OpenCV 3.2 on a MacOS environment, you can use the following repository definition:

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

There is also a sample project at https://github.com/kodfarki/opencv-example .

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

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

+1
source

All you need: install jar in your 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); } } 
0
source

All Articles