How to install opencv in java

360learntocode

Install OpenCv in Linux/Ubuntu for Java Application.

In this blog post, we are going to install and set up OpenCV in ubuntu os for the java applications. OpenCV is a widely used great computer vision library. You can learn more about the OpenCV tutorial from here. For documentation, you can follow here. We will also cover some tutorials for Java bindings too.

You can download OpenCV from the public Github repository of OpenCV or from their official website from here. Select the desired version and click «Sources», which will download the zip file. Unzip the file.

In order to build OpenCV, go to the OpenCV path in my case it’s under «/opt/opencv-4.3.0» so I will use the same.

sudo apt-get install cmake 
cmake -DBUILD_SHARED_LIBS=OFF .. 

Note: When OpenCV is built as a set of static libraries (-DBUILD_SHARED_LIBS=OFF option) the Java bindings dynamic library is all-sufficient, i.e. doesn’t depend on other OpenCV libs, but includes all the OpenCV code inside.

Java: ant: NO JNI: NO Java tests: YES 
sudo apt install openjdk-8-jdk sudo apt-get install ant
sudo snap install ant --classic 

Note: Be careful not to run out of memory during the build. We need 4GB of memory per core. For example, if we compile with 4 cores (e.g. make -j4) we need a machine with at least 16GB of RAM.

Читайте также:  Как поймать льва в пустыне?

If everything is fine, you successfully build OpenCV. Make sure the following files are packaged in the corresponding directory.

/opt/opencv-4.3.0/build/lib/libopencv_java430.so /opt/opencv-4.3.0/build/bin/opencv-430.jar 

The path of those files is created according to your OpenCV version and directory. You need to make sure the so and jar file must be created. This jar file contains the java wrapper code which we will used in the sample example.

Источник

Installing OpenCV for Java¶

As of OpenCV 2.4.4, OpenCV supports desktop Java development. This tutorial will help you install OpenCV on your desktop SO.

Install the latest Java version.¶

Download the latest Java JDK at the Oracle link. Now you should be able to install the last Java JDK by open the file just downloaded.

Install the latest Eclispe version.¶

Download the latest Eclipse version at the Eclipse Download page choosing the Eclipse IDE for Java Developers version (suggested). Put the downloaded folder wherever you want to. You don’t need to install anything.

Install Open CV 2.4.6 under Windows.¶

First of all you should download the OpenCV library (version 2.4.6) from here. Once you get the folder opencv put in wherever you prefer. Now the only two things you will need are: the opencv-246.jar file located at \opencv\build\java and the dll library located at \opencv\build\java\x64 (for 64-bit systems) or \opencv\build\java\x86 (for 32-bit systems). If not present create a data folder under \opencv .

Install Open CV 2.4.6 under Linux or Mac.¶

The following instructions are useful if you wan to compile OpenCV under Windows. As first step, if you don’t have these already, download and install CMake and Apache Ant. Extract the downloaded OpenCV file in a location of your choice and open CMake ( cmake-gui ). Put the location of the extracted OpenCV library in the Where is the source code field (e.g., /opencv2.4.6.1/) and put the destination directory of your build in the Where to build the binaries field (e.g., /opencv2.4.6.1/build), at last, check the Grouped and Advanced checkboxes.

_images/01-00.png

Now press Configure and use the default compilers for Unix Makefiles . In the Ungrouped Entries group, insert the path to the Apache Ant executable (e.g., /apacheant1.9.2/bin/ant ). In the BUILD group, unselect: * BUILD_PERF_TESTS . * BUILD_SHARED_LIBRARY to make the Java bindings dynamic library all-sufficient. * BUILD_TESTS . * BUILD_opencv_python .

In the CMAKE group, set to Debug (or Release ) the CMAKE_BUILD_TYPE In the JAVA group: insert the Java AWT include path (e.g., /usr/lib/jvm/java7oracle/include/ ) insert the Java include path (e.g., /usr/lib/jvm/java7oracle/include/ ) insert the alternative Java include path (e.g., /usr/lib/jvm/java7oracle/include/linux ) Once we have pressed Generate twice the CMake window should appear with a white background. Now close CMake.

_images/01-01.png

Now open the terminal , go to the build folder of OpenCV and build everything with the command: make -j8 (wait for the process to be completed. ). If everything went well you should have opencv-246.jar and libopencv_java246.so files in the /opencv-2.4.6.1/build/bin directory and the data folder in the /opencv-2.4.6.1/ directory.

Set up OpenCV for Java in Eclipse¶

Open Eclipse and select a workspace of your choice. Create a User Library, ready to be used on all the next projects: go to Window > Preferences. .

_images/01-02.png

From the menu navigate under Java > Build Path > User Libraries and choose New. . Enter a name for the library (e.g., opencv246) and select the newly created user library. Choose Add External JARs. , browse to select opencv246.jar . After adding the jar, extend it and select Native library location and press Edit. .

_images/01-03.png

Select External Folder. and browse to select the folder containing the OpenCV libraries (e.g., C:\opencv\build\java\x64 under Windows).

© Copyright 2014-2015, Luigi De Russis, Alberto Sacco. Revision 28a0a1b2 .

Versions latest stable Downloads pdf htmlzip epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.

Источник

Installing OpenCV for Java¶

As of OpenCV 2.4.4, OpenCV supports desktop Java development. This tutorial will help you install OpenCV on your desktop operating system.

Install the latest Java version¶

Download the latest Java JDK from the Oracle website. Now you should be able to install the last Java JDK by opening the file just downloaded.

Install the latest Eclipse version¶

Download the latest Eclipse version at the Eclipse Download page choosing the Eclipse IDE for Java Developers version (suggested).

Extract the downloaded compressed file and put the resulting folder wherever you want to. You don’t need to install anything. Alternatively, you can try the Eclipse installer.

Install OpenCV 3.x under Windows¶

First of all you should download the OpenCV library (version 3.x) from here.

Then, extract the downloaded OpenCV file in a location of your choice. Once you get the folder opencv put in wherever you prefer.

Now the only two things that you will need are: the opencv-3xx.jar file located at \opencv\build\java and the opencv_java3xx.dll library located at \opencv\build\java\x64 (for 64-bit systems) or \opencv\build\java\x86 (for 32-bit systems). The 3xx suffix of each file is a shortcut for the current OpenCV version, e.g., it will be 300 for OpenCV 3.0 and 330 for OpenCV 3.3.

Install OpenCV 3.x under macOS¶

The quickest way to obtain OpenCV under macOS is to use Homebrew. After installing Homebrew, you have to check whether the XCode Command Line Tools are already installed on your system.

To do so, open the Terminal and execute: xcode-select —install If macOS asks for installing such tools, proceed with the download and installation. Otherwise, continue with the OpenCV installation.

As a prerequisite, check that Apache Ant is installed. Otherwise, install it with Homebrew: brew install ant . Ant should be available at /usr/local/bin/ant .

To install OpenCV (with Java support) through Homebrew, you need to edit the opencv formula in Homebrew, to add support for Java: brew edit opencv In the text editor that will open, change the line: -DBUILD_opencv_java=OFF in -DBUILD_opencv_java=ON then, after saving the file, you can effectively install OpenCV: brew install —build-from-source opencv

After the installation of OpenCV, the needed jar file and the dylib library will be located at /usr/local/Cellar/opencv/3.x.x/share/OpenCV/java/ , e.g., /usr/local/Cellar/opencv/3.3.1/share/OpenCV/java/ .

Please, notice that this method doesn’t work if you update OpenCV from a previous version: you need to uninstall OpenCV and install it again.

Install OpenCV 3.x under Linux¶

Please, note: the following instructions are also useful if you want to compile OpenCV under Windows or macOS. Linux package management systems ( apt-get , yum , etc.) may provide the needed version of the OpenCV library.

As first step, download and install CMake and Apache Ant, if you don’t have any of these. Download the OpenCV library from its website. Extract the downloaded OpenCV file in a location of your choice and open CMake ( cmake-gui ). Put the location of the extracted OpenCV library in the Where is the source code field (e.g., /opencv/) and insert the destination directory of your build in the Where to build the binaries field (e.g., /opencv/build). At last, check the Grouped and Advanced checkboxes.

_images/01-00.png

Now press Configure and use the default compilers for Unix Makefiles . Please, be sure to have installed a C/C++ compiler. In the Ungrouped Entries group, insert the path to the Apache Ant executable (e.g., /apache-ant-1.9.6/bin/ant ). In the BUILD group, unselect:

  • BUILD_PERF_TESTS
  • BUILD_SHARED_LIBRARY to make the Java bindings dynamic library all-sufficient
  • BUILD_TESTS
  • BUILD_opencv_python

In the CMAKE group, set to Debug (or Release ) the CMAKE_BUILD_TYPE

  • insert the Java AWT include path (e.g., /usr/lib/jvm/java-1.8.0/include/ )
  • insert the Java AWT library path (e.g., /usr/lib/jvm/java-1.8.0/include/jawt.h )
  • insert the Java include path (e.g., /usr/lib/jvm/java-1.8.0/include/ )
  • insert the alternative Java include path (e.g., /usr/lib/jvm/java-1.8.0/include/linux )
  • insert the JVM library path (e.g., /usr/lib/jvm/java-1.8.0/include/jni.h )

Press Configure twice, and the CMake window should appear with a white background. If not, fix the red lines and press Configure again. Now, press Generate and close CMake.

_images/01-01.png

Now open the terminal, go to the build folder of OpenCV and compile everything with the command: make -j . Notice that the -j flag tells make to run in parallel with the maximum number of allowed job threads, which makes the build theoretically faster. Wait for the process to be completed. If everything went well you should have opencv-3xx.jar in the /opencv/build/bin directory and libopencv_java3xx.so in the /opencv/build/lib directory. The 3xx suffix of each file is a shortcut for the current OpenCV version, e.g., it will be 300 for OpenCV 3.0 and 330 for OpenCV 3.3. This is everything you need.

Set up OpenCV for Java in Eclipse¶

Open Eclipse and select a workspace of your choice. Create a User Library, ready to be used on all your next projects: go to Window > Preferences. .

_images/01-02.png

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. After adding the jar, extend it, select Native library location and press Edit. .

_images/01-03.png

Select External Folder. and browse to select the folder containing the OpenCV libraries (e.g., C:\opencv\build\java\x64 under Windows).

In case of MacOS, if you installed OpenCV without Homebrew, you need to create a soft link with .dylib extension for the .so file. E.g., from the terminal, type: ln -s libopencv_java300.so libopencv_java300.dylib

Set up OpenCV for Java in other IDEs (experimental)¶

If you are using IntelliJ, you can specify the location of the library with the VM argument -Djava.library.path=/opencv/build/lib .

© Copyright 2014-2016, Luigi De Russis, Alberto Sacco. Revision f734e31f .

Versions latest stable Downloads pdf htmlzip epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.

Источник

Оцените статью