Linux environment to build OpenCV to run java-cv code

1. Windows system runs java-cv code
Install OpenCV

Installing OpenCV on Windows is relatively simple. Go to the official website, enter releases, select the Windows version, download the executable file, and then perform the visual installation step by step.
OpenCV official website: https://opencv.org
After the installation is completed, the installation directory is as follows:
image.png
Enter the build directory:
image.png
Enter the java directory:
image.png
You can see the opencv-460.jar package, which will be used later.
Enter the x64 directory:
image.png
opencv_java460.dll here is the dynamic link library that will be called by subsequent Native classes.

Run java-cv for face detection
Introduce dependencies
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv</artifactId>
    <version>1.5.8</version>
</dependency>
Add opencv-460 to classpath

Open the project structure interface of IDEA and add the opencv-460.jar package to the dependent library.
image.png

Configure opencv_java460.dll

Subsequent code needs to load the dll file, so opencv_java460.dll needs to be placed in the path. For Windows systems, it can be placed directly in the C:\Windows\System32 directory.

Code examples

The following code performs face detection:

// 加载本地库
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 新建人脸识别类对象
CascadeClassifier faceDetector = new CascadeClassifier("D:\\haarcascade_frontalface_alt.xml");
// 原始照片
Mat image = Imgcodecs.imread("D:\\test.png");
MatOfRect faceDetections = new MatOfRect();
// 进行人脸检测
faceDetector.detectMultiScale(image, faceDetections);

The above code is mainly divided into four steps:

  • Load native libraries from the system
  • Create face recognition class object
  • Read pictures
  • Perform face detection

The face detection function in the OpenCV library requires the haarcascade_frontalface_alt.xml feature classifier file. This file contains a series of classifiers for faces, which can be used to identify face areas in images or videos.

Code call chain analysis

Let’s analyze the calling link of the following code (loading the local library):

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

Load local library via libname:

public static void loadLibrary(String libname) {
    
    
    Runtime.getRuntime().loadLibrary0(Reflection.getCallerClass(), libname);
}

Check the value of libname:

public static final String NATIVE_LIBRARY_NAME = getNativeLibraryName();
private static String getNativeLibraryName() {
    
     return "opencv_java460"; }

As can be seen from the above, the code loads the local library through the name opencv_java460.
Next, let’s see where the local library is loaded from:

public void loadLibrary(String libname) {
    
    
    loadLibrary0(Reflection.getCallerClass(), libname);
}

synchronized void loadLibrary0(Class<?> fromClass, String libname) {
    
    
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
    
    
        security.checkLink(libname);
    }
    if (libname.indexOf((int)File.separatorChar) != -1) {
    
    
        throw new UnsatisfiedLinkError("Directory separator should not appear in library name: " + libname);
    }
    ClassLoader.loadLibrary(fromClass, libname, false);
}

static void loadLibrary(Class<?> fromClass, String name, boolean isAbsolute) {
    
    
    ClassLoader loader =(fromClass == null) ? null : fromClass.getClassLoader();
    if (sys_paths == null) {
    
    
        usr_paths = initializePath("java.library.path");
        sys_paths = initializePath("sun.boot.library.path");
    }
    ......
}

The above code indicates that the local library is loaded from the "java.library.path" path. As for which directory the "java.library.path" path is, you can confirm it with the following code:

System.getProperty("java.library.path")

Run the code and the output result is the path where opencv_java460.dll is located.

2. Install OpenCV on Linux system
Install jdk and configure environment variables

slightly

Install ant

During the construction process, JavaCV requires Ant to build and compile C++ code and generate the corresponding Java JNI interface and shared library files. Before installing JavaCV, you need to install the Ant tool first. Next install ant through yum

sudo yum install ant

Configure environment variables:

export ANT_HOME=/usr/share/ant
export PATH=$PATH:$ANT_HOME/bin

After the installation is complete, check the ant version:

ant -version

If the above code reports an error: Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.launch.Launcher
means that the Ant launcher JAR file is not found in the classpath, and the Ant launcher JAR file needs to be added to the classpath:

export CLASSPATH="$ANT_HOME/lib/ant-launcher.jar:$CLASSPATH"
Install OpenCV

First download the two installation packages opencv-4.5.5.tar.gz and opencv_contrib-4.5.5.tar.gz.
The download address of opencv-4.5.5.tar.gz is: https://github.com/opencv/opencv/ tags
opencv_contrib-4.5.5.tar.gz download address: https://github.com/opencv/opencv_contrib/tags
Unzip the installation package:

tar -zxvf opencv-4.5.5.tar.gz
tar -zxvf opencv_contrib-4.5.5.tar.gz

Enter the opencv-4.5.5 directory and create a new build directory:

cd ./opencv-4.5.5
mkdir build

Enter the build directory:

cd build

Execute the build:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_EXTRA_MODULES_PATH=/root/software/opencv_contrib-4.5.5/modules -D ENABLE_CXX11=ON -D WITH_1394=OFF -D BUILD_opencv_xfeatures2d=OFF -D BUILD_SHARED_LIBS=OFF -D BUILD_TESTS=OFF ..

Reference for the above construction process: https://blog.csdn.net/qq_21347647/article/details/127746951 "Compiling and using java_opencv in Linux environment"
and then compile:

make -j4
make install

After the compilation is completed, check the /usr/local/share/java/opencv4 directory
image.png
to see if so and jar files are generated. If not, you need to analyze the reasons and recompile.

Run java-cv for face detection
Possible errors

After completing the OpenCV installation, run the java-cv code in the Linux environment for face detection. If the following error is reported:
image.png
According to the above code call chain analysis, the system did not find the relevant library file from java.library.path when loadingLibrary ( The error message contains the opencv_java460.so file).

Why is the opencv_java460 library file read?

The OpenCV version installed in the Linux environment is 4.5.5, but the code is missing and an error message is reported and the local library file corresponding to opencv_java460 cannot be found.
First look at the maven dependencies:

<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv</artifactId>
    <version>1.5.8</version>
</dependency>

Check the introduced opencv version through the Dependency Analyzer plug-in:
image.png
As can be seen from the dependency tree above, the opencv introduced corresponding to javacv1.5.8 is version 4.6.0 (this version is also installed on Windows systems). As
can be seen from the following code, the system The relevant local libraries are loaded through the name opencv_java460:

public static final String NATIVE_LIBRARY_NAME = getNativeLibraryName();
private static String getNativeLibraryName() {
    
     return "opencv_java460"; }

Next, change the version of javacv to 1.5.7, and then check the introduced opencv version through the Dependency Analyzer plug-in:
image.png
It can be seen that the opencv version corresponding to javacv1.5.7 is exactly 4.5.5. Since the OpenCV installed in the Linux environment is 4.5.5, So repackage the code to run in the Linux environment.

Configure environment variables

Add the libopencv_java455.so file path in the /usr/local/share/java/opencv4 directory to the LD_LIBRARY_PATH path:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/leptonica/lib:/usr/local/tesseract/lib:/usr/local/share/java/opencv4
export LD_LIBRARY_PATH

Rerun the java-cv code.

Guess you like

Origin blog.csdn.net/Princeliu999/article/details/130953041