环境
操作系统:Ubuntu 18.04
OpenCv版本:4.6.0
交叉工具链:arm-linux-gnueabihf-gcc-5.3.1
下载OpenCV源代码
这里推荐大家到网上找OpenCV的Linux版本安装包(.tar.gz结尾),不要github上clone(clone下来的没编译通过)
https://github.com/opencv/opencv/releases/tag/4.6.0
选择tar.gz源码
下载 Opencv Contrib
https://gitcode.net/mirrors/opencv/opencv_contrib 下载 在Tags里找到相应版本下载,
注意版本要与opencv一致
下载好解压后,将opencv_contrib放在opencv文件夹里面。
创建一个交叉编译参数文件
lu@DESKTOP-UJNM808:/WorkSpace/opencv-4.6.0$ cat arm-linux-gnueabihf.toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER "/opt/ext-toolchain/bin/arm-linux-gnueabihf-gcc")
set(CMAKE_CXX_COMPILER "/opt/ext-toolchain/bin/arm-linux-gnueabihf-g++")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_C_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon")
set(CMAKE_CXX_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon")
# cache flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "c flags")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags")
lu@DESKTOP-UJNM808:/WorkSpace/opencv-4.6.0$
创建一个编译脚本
lu@DESKTOP-UJNM808:/WorkSpace/opencv-4.6.0$ cat make.sh
cmake -DCMAKE_TOOLCHAIN_FILE=../arm-linux-gnueabihf.toolchain.cmake \
-DCMAKE_INSTALL_PREFIX=./install_arm \
-DBUILD_opencv_world=OFF \
-DBUILD_SHARED_LIBS=ON \
-DWITH_OPENEXR=OFF \
-DWITH_PNG=OFF \
-DBUILD_JAVA=OFF \
-DWITH_OPENJPEG=OFF \
-DENABLE_NEON=ON \
-DENABLE_VFPV3=ON \
-DENABLE_VFPV4=ON \
-DCMAKE_BUILD_TYPE=RELEASE \
-DENABLE_PRECOMPILED_HEADERS=OFF \
-DENABLE_FAST_MATH=ON \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.6.0/modules ..
lu@DESKTOP-UJNM808:/WorkSpace/opencv-4.6.0$
安装依赖库
#安装cmake
$ sudo apt-get install -y cmake
编译源代码
# 新建build_arm目录
$ cd opencv4.6.0/
# 创建build_arm目录用于存放编译好的配置文件等信息
$ mkdir build_arm
# 将make.sh脚本移到build_arm/下
$ mv make.sh build_arm/
# 执行编译脚本
$ ./make.sh
等待编译完成。
安装
$ sudo make install
在opencv-4.6.0/build_arm/install_arm/下可以看到如下