先安装系统,你的armhf是啥就安装啥,免得踩坑

更新系统

sudo apt update
sudo apt -y upgrade

安装环境

sudo dpkg --add-architecture armhf
sudo apt update
sudo apt install qemu-user-static

安装Python环境

sudo apt-get install python3.5-minimal:armhf 
sudo apt-get install liblapack3:armhf
sudo apt-get install python:armhf
sudo apt-get install python3-dev:armhf
sudo apt-get install python3-numpy:armhf
sudo apt-get install python-dev:armhf
sudo apt-get install python-numpy:armhf

写个脚本,自动下载依赖包。

#!/bin/bash

logfile=log.log
ret=""

function getDepends()
{
   echo "fileName is" $1 >> $logfile
   ret=`apt-cache depends $1|grep Depends |cut -d: -f2 |tr -d "<>"`
   echo $ret|tee  -a $logfile
}

libs=$1

i=0
while [ $i -lt 3 ] ;
do
    let i++
    echo $i
    newlist=" "
    for j in $libs
    do
        added="$(getDepends $j)"
        newlist="$newlist $added"
        apt install $added:armhf --reinstall -d -y
    done
    libs=$newlist
done

把上面的保存到install.sh里,给install.sh权限

sudo chmod 777 install.sh

安装依赖库

sudo bash install.sh libgtk-3-dev:armhf
sudo bash install.sh libcanberra-gtk3-dev:armhf
sudo bash install.sh libtiff-dev:armhf
sudo bash install.sh libtiff-dev:armhf
sudo bash install.sh zlib1g-dev:armhf
sudo bash install.sh libjpeg-dev:armhf
sudo bash install.sh libpng-dev:armhf
sudo bash install.sh libavcodec-dev:armhf
sudo bash install.sh libavformat-dev:armhf
sudo bash install.sh libswscale-dev:armhf
sudo bash install.sh libv4l-dev:armhf
sudo bash install.sh libxvidcore-dev:armhf
sudo bash install.sh libx264-dev:armhf

不妨再装一次,免得踩坑

sudo apt install libgtk-3-dev:armhf libcanberra-gtk3-dev:armhf
sudo apt install libtiff-dev:armhf zlib1g-dev:armhf
sudo apt install libjpeg-dev:armhf libpng-dev:armhf
sudo apt install libavcodec-dev:armhf libavformat-dev:armhf libswscale-dev:armhf libv4l-dev:armhf
sudo apt-get install libxvidcore-dev:armhf libx264-dev:armhf

下载编译工具

sudo apt install crossbuild-essential-armhf
sudo apt install gfortran-arm-linux-gnueabihf
sudo apt install cmake git pkg-config

下载OpenCV包

cd ~
mkdir opencv_all && cd opencv_all
wget -O opencv.tar.gz https://github.com/opencv/opencv/archive/4.0.0.tar.gz
tar xf opencv.tar.gz
wget -O opencv_contrib.tar.gz https://github.com/opencv/opencv_contrib/archive/4.0.0.tar.gz
tar xf opencv_contrib.tar.gz
rm *.tar.gz

加入环境变量

export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig
export PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig

预构建

cd opencv-4.0.0
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
     -D CMAKE_INSTALL_PREFIX=/opt/opencv-4.0.0 \
     -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake \
     -D OPENCV_EXTRA_MODULES_PATH=~/opencv_all/opencv_contrib-4.0.0/modules \
     -D OPENCV_ENABLE_NONFREE=ON \
     -D PYTHON2_INCLUDE_PATH=/usr/include/python2.7 \
     -D PYTHON2_LIBRARIES=/usr/lib/arm-linux-gnueabihf/libpython2.7.so \
     -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2/dist-packages/numpy/core/include \
     -D PYTHON3_INCLUDE_PATH=/usr/include/python3.5m \
     -D PYTHON3_LIBRARIES=/usr/lib/arm-linux-gnueabihf/libpython3.5m.so \
     -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \
     -D BUILD_OPENCV_PYTHON2=ON \
     -D BUILD_OPENCV_PYTHON3=ON \
     -D ENABLE_NEON=ON \
     -D ENABLE_VFPV3=ON \
     -D WIDTH_GTK=ON \
     -D BUILD_TESTS=OFF \
     -D BUILD_DOCS=OFF \
     -D BUILD_EXAMPLES=OFF ..

开始编译

sudo make -j8
sudo make install/strip