Ubuntu-0009-编译安装 opencv

格瑞图

目录

收起

0、背景

(1)本系列文章

1、编译安装 opencv

(1)获取依赖

(2)创建目录克隆代码

(3)编译 OpenCV - 配置

(4)编译 OpenCV - 编译

(5)编译 OpenCV - 安装

(6)验证安装

N、后记

0、背景

最新的主板已经不带 VGA 接口了。

从优盘安装 CentOS 无法识别板载 HDMI 接口~

因此换了优盘安装 Ubuntu 系统。

(1)本系列文章

格瑞图:Ubuntu-0001-安装网卡驱动

格瑞图:Ubuntu-0002-根分区扩容

格瑞图:Ubuntu-0003-机械磁盘挂载

格瑞图:Ubuntu-0004-使用 KVM 虚拟化-01-环境安装

格瑞图:Ubuntu-0005-使用 KVM 虚拟化-02-创建桥接网络

格瑞图:Ubuntu-0006-使用 KVM 虚拟化-03-创建存储池

格瑞图:Ubuntu-0007-使用 KVM 虚拟化-04-创建虚拟机

格瑞图:Ubuntu-0008-编译安装 ffmpeg

1、编译安装 opencv

!!!参考本教程操作之前,看看完(6)里面的问题解决!!!

(1)获取依赖

sudo apt -y 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)创建目录克隆代码

01.克隆 opencv

demo@hm75:~$ mkdir ~/opencv_build && cd ~/opencv_build
demo@hm75:~/opencv_build$ git clone https://github.com/opencv/opencv.git
Cloning into 'opencv'...
remote: Enumerating objects: 323466, done.
remote: Counting objects: 100% (693/693), done.
remote: Compressing objects: 100% (382/382), done.
Receiving objects:  44% (144114/323466), 307.38 MiB | 1.78 MiB/s

Ubuntu-0009-编译安装 opencv_第1张图片

02.克隆 opencv contrib

demo@hm75:~/opencv_build$ git clone https://github.com/opencv/opencv_contrib.git
Cloning into 'opencv_contrib'...
remote: Enumerating objects: 39855, done.
remote: Counting objects: 100% (728/728), done.
remote: Compressing objects: 100% (423/423), done.
remote: Total 39855 (delta 342), reused 560 (delta 258), pack-reused 39127
Receiving objects: 100% (39855/39855), 134.83 MiB | 2.19 MiB/s, done.
Resolving deltas: 100% (24775/24775), done.
demo@hm75:~/opencv_build$

Ubuntu-0009-编译安装 opencv_第2张图片

(3)编译 OpenCV - 配置

demo@hm75:~/opencv_build$ cd ~/opencv_build/opencv
demo@hm75:~/opencv_build/opencv$ mkdir -p build && cd build
demo@hm75:~/opencv_build/opencv/build$ 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-0009-编译安装 opencv_第3张图片

配置完成

Ubuntu-0009-编译安装 opencv_第4张图片

(4)编译 OpenCV - 编译

demo@hm75:~/opencv_build/opencv/build$ make -j8

Ubuntu-0009-编译安装 opencv_第5张图片

编译完成

Ubuntu-0009-编译安装 opencv_第6张图片

(5)编译 OpenCV - 安装

demo@hm75:~/opencv_build/opencv/build$ sudo make install

Ubuntu-0009-编译安装 opencv_第7张图片

安装完成

Ubuntu-0009-编译安装 opencv_第8张图片

(6)验证安装

01.命令验证

demo@hm75:~/opencv_build/opencv/build$ pkg-config --modversion opencv4
4.8.0
demo@hm75:~/opencv_build/opencv/build$

Ubuntu-0009-编译安装 opencv_第9张图片

02.派森代码验证

demo@hm75:~/opencv_build/opencv/build$ python3 -c "import cv2; print(cv2.__version__)"
Traceback (most recent call last):
  File "", line 1, in 
ModuleNotFoundError: No module named 'cv2'
demo@hm75:~/opencv_build/opencv/build$

Ubuntu-0009-编译安装 opencv_第10张图片

找了参考资料:https://github.com/opencv/opencv/issues/21359#issuecomment-1003005474

编译配置加上:-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages

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-0009-编译安装 opencv_第11张图片

然后重新编译

demo@hm75:~/opencv_build/opencv/build$ date ; make -j8 ; date
Sun 26 Nov 2023 01:18:30 PM UTC
...
Sun 26 Nov 2023 01:19:01 PM UTC

Ubuntu-0009-编译安装 opencv_第12张图片

再次安装

demo@hm75:~/opencv_build/opencv/build$ sudo make install

再次验证

demo@hm75:~/opencv_build/opencv/build$ pkg-config --modversion opencv4
4.8.0
demo@hm75:~/opencv_build/opencv/build$ python3 -c "import cv2; print(cv2.__version__)"
4.8.0-dev
demo@hm75:~/opencv_build/opencv/build$

Ubuntu-0009-编译安装 opencv_第13张图片

完事儿~

N、后记

Ubuntu-0009-编译安装 opencv_第14张图片

Spain Toledo

西班牙 托莱多

Mostly popular for Arts & culture • Family friendly

~

你可能感兴趣的:(ubuntu,opencv,linux)