1.准备opencv4.5.4源码
官网下载地址:https://opencv.org/releases/
2.安装cmake工具
sudo apt update
sudo apt install cmake
3.安装所需依赖
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev
4.生成
1.解压、进入opencv文件中源码文件夹内打开终端
2.创建编译目录并进入
mkdir build&& cd build
3.执行cmake,CMAKE_INSTALL_PREFIX后跟库的存放目录
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/fy/opencv4.5.4_gnu64 ..
4.如果卡在ippicv需要手动下载,下载ippicv_2020_lnx_intel64_20191018_general.tgz
2017版本的在这https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20170822/ippicv
2020的在这链接: https://pan.baidu.com/s/1C05qBz-eSAUIxdwljce0kw 密码: 4q33(好人一生平安^ _ ^)
下载相关库。然后进入opencv源文件中,找到/3rdparty/ippicv文件夹下的 ippicv.cmake,打开。
把"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/" 改成下载的ippicv文件路径"file:///home/fy/"
5.编译
需要sudo自己加
make -j$(nproc)
6.安装
需要sudo自己加
make install
补充一下,如果要清除用make uninstall
解决编译opencv时,卡在IPPICV: Download: ippicv_2020_lnx_intel64_20191018_general.tgz
ARM交叉编译(需要的看这里)
执行cmake这一步改为下面命令行,注意修改相应路径
cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/home/fy/WS_OPENCV/opencv3.3.1_imx8 \
-D CMAKE_CXX_COMPILER=/home/fy/gcc-linaro-5.5.0-2017.10-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ \
-D CMAKE_C_COMPILER=/home/fy/gcc-linaro-5.5.0-2017.10-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc \
-D CMAKE_SYSTEM_NAME=Linux \
-D CMAKE_SYSTEM_PROCESSOR=arm \
-D CMAKE_CROSSCOMPILING=true \
-D BUILD_opencv_highgui=OFF \
..
之所以有BUILD_opencv_highgui=OFF这一项,因为opencv_highgui需要链接一大堆库,由于没图形界面所以这里没搞。
交叉编译可能遇到的问题
(1)编译过程中可能报错“fatal error: zlib.h: No such file or directory #include “zlib.h”
在opencv的source目录下,修改CMakeList.txt,找到ocv_include_directories(${OPENCV_CONFIG_FILE_INCLUDE_DIR}),并在下面添加两行:ocv_include_directories(./3rdparty/zlib)
ocv_include_directories(./build/3rdparty/zlib)