安装这个opencv真的是一把心酸,一把泪啊。查了各种资料,终于能够顺利的将opencv给安装成功了。在此记录一下安装的过程及遇到过的问题。希望能够帮助到其他人。
1.#备份原始的
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
2.#编辑文件(vim使用:wq
命令保存)
sudo vim /etc/apt/sources.list
清华源
根据Ubuntu的架构选择镜像!(这里采用清华源,如需选择其他镜像源可自行百度)(选择其中一个复制就可以
)(一定要注意架构,否则在sudo apt-get update时会报错)
镜像一:本镜像仅包含 32/64 位 x86 架构处理器的软件包,在 ARM(arm64, armhf)、PowerPC(ppc64el)、RISC-V(riscv64) 和 S390x 等架构的设备上(对应官方源为ports.ubuntu.com)请使用 ubuntu-ports 镜像。
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
镜像二:本镜像仅包含 arm64 armhf ppc64el riscv64 s390x 架构的软件包。
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse
3.#更新源
sudo apt-get update
出现类似错误时:
忽略:12 http://security.ubuntu.com/ubuntu xenial-security/main arm64
Packages 忽略:12 http://security.ubuntu.com/ubuntu xenial-security/main
arm64 Packages 忽略:12 http://security.ubuntu.com/ubuntu
xenial-security/main arm64 Packages 忽略:12
http://security.ubuntu.com/ubuntu xenial-security/main arm64 Packages
错误:12 http://security.ubuntu.com/ubuntu xenial-security/main arm64
Packages 404 Not Found [IP: 185.125.190.39 80] 已下载 460 kB,耗时 6秒 (71.7
kB/s) 正在读取软件包列表… 完成 E: 无法下载
http://security.ubuntu.com/ubuntu/dists/xenial-security/main/binary-arm64/Packages
404 Not Found [IP: 185.125.190.39 80] E:
部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。
可以手动打开软件和更新,取消http://security.ubuntu.com/ubuntu xenial-security main
1.安装构建工具和所有的依赖软件包:
sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
2.克隆所有的OpenCV 和 OpenCV contrib 源:(可能会花几分钟下载,这取决于你的网速)
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
3.一旦下载完成,创建一个临时构建目录,并且切换到这个目录:
cd ~/opencv_build/opencv
mkdir -p build && cd build
使用 CMake 命令配置 OpenCV 构建:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
输出将会如下:(ubuntu为你的用户名)
- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/opencv_build/opencv/build
4.开始编译过程:
make -j8
根据你的处理器修改-f
值。(当然,如果内存足够大的话,可以直接选择make -j
)如果你不知道你的处理器核心数,你可以输入nproc
找到。
编译将会花费几分钟,或者更多,这依赖于你的系统配置。
-编译时可能会出现的问题
:
C++: fatal error: Killed signal terminated program cc1plus
这是由于make -j时选用的参数太大,没有足够的内存。可以采用交换内存的方法,但更推荐将make -j选择小一点的参数,比如选择make -j1或者make -j2或者make -j4(同时,这也会导致编译的时候时间很长很长,可能会几个小时)
5.安装 OpenCV:
sudo make install
6.验证安装结果,输入下面的命令,那你将会看到 OpenCV 版本:
C++ bindings:
pkg-config --modversion opencv4
输出:
4.x.x
Python bindings:
python3 -c "import cv2; print(cv2.__version__)"
输出:
4.x.x-dev
https://cloud.tencent.com/developer/article/1657529
https://youtu.be/LzAqP94bwCk
https://blog.csdn.net/ouyangandy/article/details/83820817
如有问题请各位指教,祝愿各位能够顺利地安装opencv库