一、获取资源:

  1. 下载opencv3.4源码
  2. 下载opencv_contrib3.4源码
  3. 下载ffmepg3.2.15源码
  4. 按以下目录结构解压:
    当前用户主目录下,如:/home/user_name/ ├── opencv
    │ └── opencv
    │ └── opencv_contrib
    ├── ffmepg
    解压命令:
    sudo tar -xzvf /home/forlinx/final_ffmpeg.tar.gzsudo tar -xzvf /home/forlinx/final_opencv.tar.gz

二、编译安装ffmepg源码

  • 安装Cmake:
    sudo apt-get install cmake
  • 安装依赖环境:
    sudo apt-get install yasm libx264-dev
  • configure编译:
    ./configure --prefix=/usr/local/ffmpeg/ --enable-shared --disable-static --enable-gpl --enable-libx264
  • 编译程序:
    make
  • 安装程序:
    sudo make install
  • 找到添加动态库路径和添加环境变量路径:
    程序的安装目录为:
    /usr/local/ffmpeg/ 那么添加动态库的路径为:
    /usr/local/ffmpeg/lib 添加的环境变量路径为:
    /usr/local/ffmpeg/bin/
  • 添加动态库:
    打开ld.so.conf
    sudo vim /etc/ld.so.conf 写入内容:
    /usr/local/ffmpeg/lib 保存并使用指令刷新:
    sudo ldconfig
  • 添加环境变量:
    打开/etc/profile
    sudo vim /etc/profile 写入内容:
    PATH=/usr/local/ffmpeg/bin/:$PATH 保存并使用指令刷新:
    source /etc/profile
  • 测试ffmepg是否可以运行:
    ffmepg

如果正常工作会显示版本号等内容

  • 复制ffmpeg/lib中的部分动态库到被pkg-config扫描的路径下:
  1. 查看路径:pkg-config --variable pc_path pkg-config 运行结果:
/usr/local/lib/aarch64-linux-gnu/pkgconfig:
/usr/local/lib/pkgconfig:
/usr/local/share/pkgconfig:/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig
  1. 复制动态库到以上lib文件夹中:
    sudo cp /usr/local/ffmpeg/lib/{libavcodec.so,libavformat.so,libavutil.so} /usr/local/lib/sudo cp /usr/local/ffmpeg/lib/{libavcodec.so,libavformat.so,libavutil.so} /usr/lib/sudo cp /usr/local/ffmpeg/lib/{libavcodec.so,libavformat.so,libavutil.so} /usr/lib/aarch64-linux-gnu/sudo cp -d/usr/local/ffmpeg/lib/{libswscale.so,libswscale.so.4,libswscale.so.4.2.100} /usr/lib/aarch64-linux-gnu/

以上就是安装ffmepg的过程

三、编译安装opencv

1.修改opencv部分源码:

绝对路径是指 opencvopencv_contrib同在的目录的绝对路径

  • 修改:
    绝对路径/opencv/modules/stitching/include/opencv2/stitching/detail/matchers.hpp 中的# include "opencv2/xfeatures2d/cuda.hpp" 为:# include "绝对路径/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp"
  • 修改:
    绝对路径/opencv/modules/stitching/src/precomp.hpp 中的# include "opencv2/xfeatures2d/cuda.hpp" 为:# include "绝对路径/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp"
  • 修改:
    绝对路径/opencv/modules/stitching/src/matchers.cpp 中的#include "opencv2/xfeatures2d.hpp" 为:绝对路径/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp
  • 修改:
    绝对路径/opencv/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp 中的#include "opencv2/xfeatures2d/nonfree.hpp" 为:绝对路径/opencv/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d/nonfree.hpp
  • 向文件夹添加缺失内容:缺失内容下载地址 将下载的压缩包中的内容拷贝到绝对路径/opencv/opencv_contrib/modules/xfeatures2d/src
2.编译opencv的准备:
  • 添加1G swap空间防止内存不够
    sudo dd if=/dev/zero of=/swapfile bs=64M count=16sudo mkswap /swapfilesudo swapon /swapfile
3.编译opencv:
  • 设定cmkelist:
    cmake -DCMAKE_BUILD_TYPE=RELEASE \ -DCMAKE_INSTALL_PREFIX=/usr/local \ -DPYTHON_DEFAULT_EXECUTABLE=$(which python3.5) \ -DPYTHON3_EXECUTABLE=$(which python3.5) \ -DPYTHON3_INCLUDE_DIR=/usr/include/python3.5 \ -DPYTHON3_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python3.5m \ -DPYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.5m.so \ -DPYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include/ \ \ -DBUILD_DOCS=OFF \ -DBUILD_EXAMPLES=OFF \ -DBUILD_TESTS=OFF \ -DBUILD_PERF_TESTS=OFF \ -DWITH_FFMPEG=ON \ \ -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ ..
  • 开始编译:
    make -j (不限制资源,使用所有核心)
  • 安装:
    sudo make install

三、设置开机启动文件

这一步只做项目记录,与文章无关

1.Ubuntu16以上需要设置

  • 建立系统服务rc-local.service 创建服务文件:sudo vi /etc/systemd/system/rc-local.service 写入文件内容:
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target`
  • 创建文件rc.local:
sudo vi /etc/rc.local
  • 将下列内容复制进rc.local文件:
home/room_sample/auto_run.sh
  • 给rc.local加上权限:
sudo chmod 777 /etc/rc.local
  • 启用服务:
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

如果返回内容为绿色内容且重启过后/etc/local/test.log存在则成功

2.在项目目录对应位置放置脚本

  • 在当前的工程中项目位置为:home/room_sample/
  • 放置后脚本文件的位置为:home/room_sample/auto_run.sh
  • 脚本内容:
chmod 777 /home/room_sample/set_machine_sn.sh
nohup /home/room_sample/set_machine_sn.sh &
if [ $? -eq 0 ]
then
	echo "set_machine_sn run success"
	chmod 777 /home/room_sample/monitorRestart.sh
	nohup /home/room_sample/monitorRestart.sh &
fi

注意设置脚本的权限!