1. Things needed
Opencv4.5.2 zip package ( download from OpenCV official website )
2. Operation steps
1. Upload the zip package of opencv4.5.2 to the home directory (not necessarily the home directory, it depends on the operation and maintenance situation)
2. Run the decompression command
unzip opencv-452.zip
3. Enter the opencv-452 folder and create the folder
cd opencv-452
mkdir build
4. Go into the folder
cd build
5. If cmake is not installed, use the following command to install it and check the version information.
sudo yum install cmake
cmake -version
A higher version is required. I installed cmake version 3.7.2. If the version is too low, you will be prompted to upgrade the version.
Solution:
(1) Remove the old cmake version and install dependent packages
yum remove cmake -y ; yum install -y gcc gcc-c++ make automake openssl openssl-devel
(2) Download the cmake-3.7.2.tar.gz installation package and unzip the cmake official website address , or directly command the installation
wget https://cmake.org/files/v3.7/cmake-3.7.2.tar.gz ; tar -zxf cmake*.tar.gz
(3) Compile/install
cd cmake* ; ./bootstrap ; gmake -j `grep 'processor' /proc/cpuinfo | wc -l` ; gmake install
(4) View the compiled cmake version and create a connection
/usr/local/bin/cmake --version
ln -s /usr/local/bin/cmake /usr/bin/
(5) Verify new version
cmake --version
6. Next generate and configure cmake to build the executable file in our system, enter the command
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
7. At this time, pay attention to the execution results. If the build is normal, the results are as follows:
If the build fails without finding ant and java, you may get the following output:
Solution:
If both ant and JNI are (NO) without specific paths, then you need to install and set up java and install ant.
Excuting an order
sudo yum install ant
After success, perform step 6 again. If it is still (NO), try again.
sudo snap install ant --classic
8. After the above operation is successful, start generating and executing
make -j4
Among them, the 4 in (-j4) represents how many cores there are. Write it according to your own server. Write 4 for four cores and 8 for eight cores. Do it according to your ability. If you write less, the installation will be slower. If you write too much, the server may crash. Got it
9. Continue execution after the above command is executed.
sudo make install
After execution, check whether there are libopencv_java452.so files and opencv-452.jar files in the /user/local/share/java/opencv4 directory. If so, the installation is successful.
10. Copy libopencv_java452.so to /usr/lib and execute the command:
cp /usr/local/share/java/opencv4/libopencv_java452.so /usr/lib/
Then start the service
Add configuration class, opencv loading methods for different environments
import org.apache.commons.lang3.SystemUtils;
import org.opencv.core.Core;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.URL;
@Configuration
public class OpenCVConfig {
@Bean
public void openCVLoad(){
if (SystemUtils.IS_OS_WINDOWS) {
System.out.println("windows启动OpenCV");
String libName = "lib/opencv_java452.dll";
URL url = ClassLoader.getSystemResource(libName);
System.load(url.getPath());
} else if (SystemUtils.IS_OS_LINUX) {
System.out.println("linux启动OpenCV");
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
}