Linux源码编译安装OpenCV4(Ubuntu19)
1.1 安装lib依赖
必须要安装的依赖,通过命令安装:
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
如果需要其他的功能,可选安装:
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
1.2 安装ant依赖
注:此前因没安装ant,导致构建静态jni库时总是不生成,make也不报错,必须安装!
Apache ant官方下载最新版 https://ant.apache.org/bindownload.cgi
2.1 安装OpenCV(这里使用4.1.1)
下载Opencv安装包,OpenCV官网下载 只有Windows和Android等
若想构建Centos版只能从源码构建 Github源码下载
解压:
unzip opencv-4.1.1.zip
2.2 在解压的主目录里新建build空目录用来存放编译文件(因OpenCV4.x要求不允许生成在有源码的目录):
cd opencv-4.1.1
mkdir build
cd build
【注: cmake/make编译的路径不要包含中文,否则可能导致一些奇葩问题,亲身踩坑!】
报错:
...
[ 43%] Linking CXX static library ../lib/liblibprotobuf.a
[ 43%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/matrix_c.cpp.o
[ 43%] Building target libprotobuf xxx CMakefile (:163 all)
2.3.1 然后在新建的这个文件里运行cmake(为了减少升级cmake2到cmake3源码安装的折腾,直接可在线安装 yum install -y cmake3):
cmake3 -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=OFF -DBUILD_opencv_java=ON -DCMAKE_INSTALL_PREFIX=/usr/local ..
2.3.2 更多可选参数,如:
-DCMAKE_BUILD_TYPE=RELEASE
-DWITH_OPENCL=OFF
-DWITH_CUDA=OFF
-DWITH_OPENCL=ON
-DWITH_OPENGL=OFF
-DWITH_OPENMP=OFF
-DWITH_OPENNNI=OFF
-DWITH_OPENNNI2=OFF
-DWITH_OPENVX=OFF
-DWITH_VULKAN=OFF
-DBUILD_CUDA_STUBS=OFF
-DBUILD_DOCS=OFF
-DBUILD_EXAMPLES=OFF
-DBUILD_IPP_IW=ON
-DBUILD_ITT=ON
-DBUILD_JASPER=ON
-DBUILD_JAVA=OFF
-DBUILD_JPEG=ON
-DBUILD_OPENEXR=ON
-DBUILD_PACKAGE=ON
-DBUILD_PERF_TESTS=OFF
-DBUILD_PNG=ON
-DBUILD_PROTOBUF=ON
-DBUILD_SHARED_LIBS=ON
-DBUILD_TBB=OFF
-DBUILD_TESTS=ON
-DBUILD_TIFF=ON
-DBUILD_USE_SYMLINKS=OFF
-DBUILD_WEBP=ON
-DBUILD_WITH_DEBUG_INFO=OFF
-DBUILD_WITH_DYNAMIC_IPP=OFF
-DBUILD_ZLIB=ON
-DBUILD_opencv_apps=OFF
-DBUILD_opencv_calib3d=OFF
-DBUILD_opencv_core=ON
-DBUILD_opencv_dnn=ON
-DBUILD_opencv_features2d=OFF
-DBUILD_opencv_flann=OFF
-DBUILD_opencv_gapi=ON
-DBUILD_opencv_highgui=OFF
-DBUILD_opencv_imgcodecs=ON
-DBUILD_opencv_imgproc=ON
-DBUILD_opencv_java_bindings_generator=OFF
-DBUILD_opencv_js=OFF
-DBUILD_opencv_ml=OFF
-DBUILD_opencv_objdetect=OFF
-DBUILD_opencv_photo=OFF
-DBUILD_opencv_python2=OFF
-DBUILD_opencv_python3=OFF
-DBUILD_opencv_python_bindings_generator=OFF
-DBUILD_opencv_stitching=OFF
-DBUILD_opencv_ts=OFF
-DBUILD_opencv_video=OFF
-DBUILD_opencv_videoio=OFF
-DBUILD_opencv_world=OFF
-DCMAKE_INSTALL_PREFIX=./install
如上参数表示,只关注core、dnn、imgcodecs、imgproc、gapi模块。
若需要编译扩展模块,则还需要加入:
-DOPENCV_EXTRA_MODULES_PATH=/path/opencv_contrib/modules
如果是嵌入式设备,还需要加入相应的编译工具链和其他一些配置,如下所示:
-DCMAKE_C_COMPILER=/path/hisi-linux/x86-arm/aarch64-himix100-linux/bin/aarch64-himix100-linux-gcc -DCMAKE_CXX_COMPILER=/path/hisi-linux/x86-arm/aarch64-himix100-linux/bin/aarch64-himix100-linux-g++ -DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_C_FLAGS=-fPIC -DCMAKE_EXE_LINKER_FLAGS=-lpthread -lrt -ldl
2.4 OpenCV4默认不生成.pc文件,OPENCV_GENERATE_PKGCONFIG=ON才会生成。
之后就是编译和安装了,我的电脑是四核八线程,可以运行make,若不想编译时时间太长影响工作,四核的PC可用 make -j7。
make -j7
sudo make install
FQA:
问题1:使用clion或者CMake命令时报错,错误消息:CMake Error at CMakeLists.txt:11 (message):FATAL: In-source builds are not allowed.You should create a separate directory for build files.
解决方案:
进到opencv主目录
rm CMakeCache.txt
mkdir build
cd build
cmake ..
make
问题2:编译成功后,如将 libopencv_xxx.jar和libopencv_xxx.so拷贝只Java工程运行,报错消息:
libopencv_core.so.xxx : cannot open shared object file libopencv_dnn.so.xxx: no such file or directory
解决方案:
检查编译参数是否使用静态编译,因为动态编译生成的so文件会使用编译的时候的系统绝对路径,要想在java工程动态引用,必须使用静态编译,显示指定参数:-DBUILD_SHARED_LIBS=OFF(默认是ON)
已在应用于本人一个开源项目:
参考1:https://github.com/wl4g/super-devops-tool-opencv-native
参考2:https://github.com/wl4g/super-devops/tree/master/super-devops-tool/super-devops-tool-opencv