Configure OpenCV 3.1.0 for Java in Ubuntu 16.04

Source: Internet
Author: User

Configure OpenCV 3.1.0 for Java in Ubuntu 16.04

We all know that OpenCV is an open source computer vision Library Based on C ++, but Java binding is provided starting from 2.4.4, that is, we can also use Java to develop Computer Vision Applications Based on OpenCV. Currently, the latest version is 3.1.0. In this article, we will introduce how to build the development environment of OpenCV for Java on Ubuntu 16.04. Assume that you are currently using a new Linux operating system.

1. Configure the Java environment

Before the Makefile of OpenCV is generated, the cmake tool checks whether the Java environment has been configured in the current system to determine whether the corresponding Java development package will be generated. Therefore, we must first confirm that the Java development environment has been configured in the current system. This part of content is not the focus of this Article. Please refer to other articles.

2. Install and compile the dependency package

Compiling OpenCV in Linux requires many software packages, which will be used for later Computer Vision Applications. In Ubuntu, you can easily use the apt-get tool. The corresponding command is as follows:

sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libttb2 libttb-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libdc1394-22-dev ant

3. Unzip opencv-3.1.0 source package

Because the source code package has different sources, the format of the compressed package may also be different. There are two major formats: zip and tgz. You can decompress them using the unzip and tar commands respectively. The top-level Directory Name of the folder extracted in this article is:

opencv-3.1.0

4. Create a compilation Output Folder

To prevent the directory structure of the opencv source code package from being damaged during compilation, it is generally not recommended to directly compile the source code under the top-level directory of the source code package, generally, a build folder is created under this directory. All the intermediate compilation files and the final target files are output to this file. The process is as follows:

cd opencv-3.1.0mkdir buildcd build

5. Generate the Makefile file

First, check that the current working directory is the build folder, and then use the cmake tool to generate the Makefile file for compiling the entire project. The command is as follows:

cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local ../

However, unfortunately, it is very likely that this process will not succeed, but the Makefile generation will fail because the download of the ippicv_linux_20151201.tgz third-party package times out. The solution is to download the source package from the link provided by reference [1], copy it to the directory of the same level of the opencv source package, and then execute the following command:

ipp_file=../ippicv_linux_20151201.tgz             &&ipp_hash=$(md5sum $ipp_file | cut -d" " -f1)      &&ipp_dir=3rdparty/ippicv/downloads/linux-$ipp_hash &&mkdir -p $ipp_dir &&cp $ipp_file $ipp_dir

Note: You can also disable the IPP compilation option by adding the option in the cmake command:

cmake -DWITH_IPP=OFF

After the preceding command is executed, run the cmake command again to generate the Makefile file. If a file is successfully generated, check whether the output information in the creation process contains a line as shown in:

In the "To be built" line, it indicates the corresponding module that will eventually be compiled. If the Java environment is detected successfully, the "java" option must exist, as shown in row 3rd. If it is not found, it indicates that the Java Runtime Environment is not properly configured. You can use a command similar to the following to reconfigure it:

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

After exporting the corresponding environment variables, run the cmake command again.

6. Compile the source code

Next, you need to execute the source code compilation process according to the generated Makefile rules. For the current multi-core system, you can execute the following command to achieve parallel Compilation:

make -j8

In the end, /bin/"directory to generate a JAR file containing the Java interface" opencv-310.jar ", will be in the". the/lib/"directory generates the corresponding dynamic link library file" libopencv_java310.so ". These two files are the library files used to develop OpenCV-Based Computer Vision Applications using Java.

7. Install the library file

Finally, you need to install the generated library file to the corresponding sub-files under the/usr/local directory. In this process, you need the Administrator permission to create files and folders. The command is as follows:

sudo make install

The dynamic link library files and jar packages related to Java Development are located in the directory:

/usr/local/share/OpenCV/java/

8. Test

The next step is to test on Eclipse whether the OpenCV library can be used to develop Java Computer Vision Applications.

(1) create a Project, select File> New> Java Project, as shown in, enter the Project name, and click Finish.

(2) Configure project properties. Right-click the project and choose Properties From the shortcut menu. Select the "Libraries" sub-tab in "Java Build Path", select the "Add Library..." button, select the "User Library" type in the pop-up dialog box, and click Next.

Then click the New button, in the pop-up dialog box, enter the name of the user library, such as opencv-3.1.0 ". Next, we need to associate this library with the corresponding JAR file. Select the Edit button, in the pop-up file selection dialog box, find the path to the opencv-310.jar file, and select the file, OK.

Next, expand the JAR package, select "Native Library Location", click "Edit" next to it, and select the path of libopencv_java310.so, that is, the/usr/local/share/OpenCV/java folder. In this way, all attributes are configured. Click Finish.

(3) Compile the test code. Create a Sample class and enter the following code.

 1 import org.opencv.core.*; 2 import org.opencv.imgcodecs.Imgcodecs; 3  4 public class Sample { 5     static {System.loadLibrary(Core.NATIVE_LIBRARY_NAME);} 6      7     public static void main(String[] args){ 8         Mat m = Imgcodecs.imread("/home/tang/Pictures/baby.jpg"); 9         System.out.println(m);10     }11 }

The program reads an image from the local file system, stores the data in the Mat class, and then outputs the image to the information. The running result is as follows:

If you see information similar to the above, congratulations! Your OpenCV Java development environment has been configured!

References:

[1] http://www.linuxfromscratch.org/blfs/view/svn/general/opencv.html

[2]

[3]

OpenCV official tutorial Chinese version (For Python) PDF

Install the required OpenCV2.4.1 package in Ubuntu Linux

Install OpenCV2.4.2 on Ubuntu 12.04

OpenCV in CentOS cannot read Video Files

Summary of installing OpenCV 2.4.5 in Ubuntu 12.04

Install OpenCv2.1 in Ubuntu 10.04

Face Recognition System Based on QT and OpenCV

Install OpenCV 2.4.9 in Ubuntu 14.04 and 13.10

OpenCV details: click here
OpenCV: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.