RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本

RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本

    • 1、卸载之前安装的OpenCV
    • 2、安装Eigen3
    • 3、安装OpenCV3.4.16
      • 3.1、对应的工具要求
        • 3.1.1、配置系统默认的python版本
          • 3.1.1.1、系统级的配置
      • 3.2、安装对应的依赖
        • 3.2.1、安装编译依赖
      • 3.3、开始安装OpenCV
      • 3.4、配置OpenCV
      • 3.5、测试OpenCV
        • 3.5.1、查看版本
        • 3.5.2、Python版本测试
        • 3.5.3、C++demo测试
        • 3.5.3、Python demo测试
        • 3.5.4、Python调用摄像头测试

2023-04-20记录一下,来自我的博文RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本

Python版本可以了,调用摄像头也ok,C++的调用摄像头还存在一些问题。

1、卸载之前安装的OpenCV

就是之前记录的:RK3588卸载OpenCV3.4.16

2、安装Eigen3

就是之前记录的文章:RK3588安装Eigen3

3、安装OpenCV3.4.16

  • 参考资料:
    1、RK3588安装OpenCV3.4.16【第一次记录的,不过似乎没有装好,这次再来
    2、ubuntu系统编译安装OpenCV 4.4并配置C++和Python开发环境
    3、【CV实战】Ubuntu18.04源码编译安装opencv-3.4.X+测试demo
    4、Install OpenCV-Python in Ubuntu
    5、Installation in Linux

3.1、对应的工具要求

在官网Installation in Linux上可以看到对工具的要求:

  • Required Packages :
  • 1、 GCC 4.4.x or later
  • 2、CMake 2.8.7 or higher
  • 3、Git
  • 4、GTK+2.x or higher, including headers (libgtk2.0-dev)
  • 5、 pkg-config
  • 6、Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • 7、ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • 8、 [optional] libtbb2 libtbb-dev
  • 9、 [optional] libdc1394 2.x
  • 10、[optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
  • 11、[optional] CUDA Toolkit6.5 or higher

需要Python2.6版本以上,这里采用了3.8。
RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第1张图片
RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第2张图片
python3.8不是系统默认的Python版本,需要进行替换。

3.1.1、配置系统默认的python版本
  • 参考:
    1、【Python】Ubuntu修改默认Python版本
    2、ubuntu 16.04下切换python版本小结
3.1.1.1、系统级的配置
  • 1、从上面的信息可以看到,没有python的软连接,如果是有,在需要删除:sudo rm /usr/bin/python。这里没有这个,就不用删除了,直接跳过。
    在这里插入图片描述
  • 2、创建一个python的软连接到指定的Python版本:
sudo ln -s /usr/bin/python3.8 /usr/bin/python

RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第3张图片
这样就行了,因为没有其他的python版本造成干扰。

  • 3、pip错误
    RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第4张图片
    解决方法是将pip版本更改为符合当前的Python版本。对于Python2或者Python3:
sudo apt-get update
sudo apt-get install python-pip #python2
sudo apt-get install python3-pip #python3

在这里插入图片描述

3.2、安装对应的依赖

本次的目标是要编译成C++、Python版本,两者都是需要的,因此依赖需要安装C++的依赖、Python的依赖。

执行命令:

sudo apt-get update
3.2.1、安装编译依赖
  • 1、根据参考资料,安装编译所需的依赖【C++版本所需】:
sudo apt-get install build-essential 
  • 2、安装cmake 【都需要】、git、pkg-config、gcc、g++ :
# 都需要
sudo apt-get install cmake git pkg-config 
# 一般都自带有,不需要安装,没有的话就需要装上:
sudo apt-get install gcc g++
  • 3、GTK支持(图形界面库)
# 安装gtk2支持
sudo apt-get install libgtk2.0-dev
# 安装gtk3支持
sudo apt-get install libgtk-3-dev
  • 4、安装摄像头库支持(v4l)、流媒体支持(fmpeg, gstreamer)
    这个很重要,不然可能没法打开摄像头,这次重装就是因为没法打开摄像头调用数据,我感觉就是因为上次没有安装这个!!!!
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

返回的信息:

topeet@iTOP-RK3588:~ $ sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libgles2-mesa-dev liborc-0.4-dev liborc-0.4-dev-bin
  libx11-xcb-dev
Suggested packages:
  gstreamer1.0-doc liborc-0.4-doc
The following NEW packages will be installed:
  libgles2-mesa-dev libgstreamer-plugins-base1.0-dev
  libgstreamer1.0-dev liborc-0.4-dev liborc-0.4-dev-bin
  libx11-xcb-dev
0 upgraded, 6 newly installed, 0 to remove and 35 not upgraded.
Need to get 1,233 kB of archives.
After this operation, 14.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://repo.huaweicloud.com/ubuntu-ports focal-security/main arm64 libgles2-mesa-dev arm64 21.2.6-0ubuntu0.1~20.04.2 [6,424 B]
Get:2 https://repo.huaweicloud.com/ubuntu-ports focal-security/main arm64 libgstreamer1.0-dev arm64 1.16.3-0ubuntu1.1 [484 kB]
Get:3 https://repo.huaweicloud.com/ubuntu-ports focal/main arm64 liborc-0.4-dev-bin arm64 1:0.4.31-1 [171 kB]
Get:4 https://repo.huaweicloud.com/ubuntu-ports focal/main arm64 liborc-0.4-dev arm64 1:0.4.31-1 [178 kB]
Get:5 https://repo.huaweicloud.com/ubuntu-ports focal-security/main arm64 libx11-xcb-dev arm64 2:1.6.9-2ubuntu1.2 [9,776 B]
Get:6 https://repo.huaweicloud.com/ubuntu-ports focal-security/main arm64 libgstreamer-plugins-base1.0-dev arm64 1.16.3-0ubuntu1.1 [383 kB]
Fetched 1,233 kB in 6s (223 kB/s)
Selecting previously unselected package libgles2-mesa-dev:arm64.
(Reading database ... 121622 files and directories currently installed.)
Preparing to unpack .../0-libgles2-mesa-dev_21.2.6-0ubuntu0.1~20.04.2_arm64.deb ...
Unpacking libgles2-mesa-dev:arm64 (21.2.6-0ubuntu0.1~20.04.2) ...
Selecting previously unselected package libgstreamer1.0-dev:arm64.
Preparing to unpack .../1-libgstreamer1.0-dev_1.16.3-0ubuntu1.1_arm64.deb ...
Adding 'diversion of /usr/bin/dh_gstscancodecs to /usr/bin/dh_gstscancodecs-gst0.10 by libgstreamer1.0-dev'
Adding 'diversion of /usr/share/man/man1/dh_gstscancodecs.1.gz to /usr/share/man/man1/dh_gstscancodecs.1.gz-gst0.10 by libgstreamer1.0-dev'
Unpacking libgstreamer1.0-dev:arm64 (1.16.3-0ubuntu1.1) ...
Selecting previously unselected package liborc-0.4-dev-bin.
Preparing to unpack .../2-liborc-0.4-dev-bin_1%3a0.4.31-1_arm64.deb ...
Unpacking liborc-0.4-dev-bin (1:0.4.31-1) ...
Selecting previously unselected package liborc-0.4-dev:arm64.
Preparing to unpack .../3-liborc-0.4-dev_1%3a0.4.31-1_arm64.deb ...
Unpacking liborc-0.4-dev:arm64 (1:0.4.31-1) ...
Selecting previously unselected package libx11-xcb-dev:arm64.
Preparing to unpack .../4-libx11-xcb-dev_2%3a1.6.9-2ubuntu1.2_arm64.deb ...
Unpacking libx11-xcb-dev:arm64 (2:1.6.9-2ubuntu1.2) ...
Selecting previously unselected package libgstreamer-plugins-base1.0-dev:arm64.
Preparing to unpack .../5-libgstreamer-plugins-base1.0-dev_1.16.3-0ubuntu1.1_arm64.deb ...
Unpacking libgstreamer-plugins-base1.0-dev:arm64 (1.16.3-0ubuntu1.1) ...
Setting up libx11-xcb-dev:arm64 (2:1.6.9-2ubuntu1.2) ...
Setting up libgles2-mesa-dev:arm64 (21.2.6-0ubuntu0.1~20.04.2) ...
Setting up libgstreamer1.0-dev:arm64 (1.16.3-0ubuntu1.1) ...
Setting up liborc-0.4-dev-bin (1:0.4.31-1) ...
Setting up liborc-0.4-dev:arm64 (1:0.4.31-1) ...
Setting up libgstreamer-plugins-base1.0-dev:arm64 (1.16.3-0ubuntu1.1) ...
Processing triggers for man-db (2.9.1-1) ...
  • 5、安装libtbb2、libtbb-dev【可选】
sudo apt-get install libtbb2 libtbb-dev
  • 6、安装python2、python3支持模块【可选】
    可选,譬如你可以只打算在python3里用opencv,那么就忽略python2的模块安装。
# 安装python2支持
sudo apt-get install python-dev python-numpy
# 安装python3支持
sudo apt-get install python3-dev python3-numpy
  • 7、可选图片支持库【可选】
    用于处理多种图片格式(可选)
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev libopenexr-dev libwebp-dev
#后面两种 libopenexr-dev libwebp-dev 加进来的,opencv官方给的安装指导没有

如果有的话,就不用安装了。

# 总的安装
sudo apt-get install build-essential cmake git pkg-config gcc g++ libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libtbb2 libtbb-dev python3-dev python3-numpy libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev libopenexr-dev libwebp-d

# 查看
apt-show-versions build-essential cmake git pkg-config gcc g++ libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libtbb2 libtbb-dev python3-dev python3-numpy libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev libopenexr-dev libwebp-d

结果:

build-essential:arm64/focal-updates 12.8ubuntu1.1 uptodate
cmake:arm64/focal-updates 3.16.3-1ubuntu1.20.04.1 uptodate
g++:arm64/focal 4:9.3.0-1ubuntu2 uptodate
gcc:arm64/focal 4:9.3.0-1ubuntu2 uptodate
git:arm64/focal-security 1:2.25.1-1ubuntu3.10 uptodate
libavcodec-dev:arm64/focal-security 7:4.2.7-0ubuntu0.1 uptodate
libavformat-dev:arm64/focal-security 7:4.2.7-0ubuntu0.1 uptodate
libdc1394-22-dev:arm64/focal 2.2.5-2.1 uptodate
libgstreamer-plugins-base1.0-dev:arm64/focal-security 1.16.3-0ubuntu1.1 uptodate
libgstreamer1.0-dev:arm64/focal-security 1.16.3-0ubuntu1.1 uptodate
libgtk-3-dev:arm64/focal-updates 3.24.20-0ubuntu1.1 uptodate
libgtk2.0-dev:arm64/focal 2.24.32-4ubuntu4 uptodate
Use of uninitialized value $arch in concatenation (.) or string at /usr/bin/apt-show-versions line 371.
Use of uninitialized value $arch in hash element at /usr/bin/apt-show-versions line 374.
Use of uninitialized value $arch in hash element at /usr/bin/apt-show-versions line 386.
libjasper-dev: not installed
libjpeg-dev:arm64/focal 8c-2ubuntu8 uptodate
libopenexr-dev:arm64/focal-security 2.3.0-6ubuntu0.5 uptodate
libpng-dev:arm64/focal 1.6.37-2 uptodate
libswscale-dev:arm64/focal-security 7:4.2.7-0ubuntu0.1 uptodate
libtbb-dev:arm64/focal 2020.1-2 uptodate
libtbb2:arm64/focal 2020.1-2 uptodate
libtiff-dev:arm64/focal-security 4.1.0+git191117-2ubuntu0.20.04.8 uptodate
Use of uninitialized value $arch in concatenation (.) or string at /usr/bin/apt-show-versions line 371.
Use of uninitialized value $arch in hash element at /usr/bin/apt-show-versions line 374.
Use of uninitialized value $arch in hash element at /usr/bin/apt-show-versions line 386.
libwebp-d: not installed
pkg-config:arm64/focal 0.29.1-0ubuntu4 uptodate
python3-dev:arm64/focal 3.8.2-0ubuntu2 uptodate
python3-numpy:arm64/focal-security 1:1.17.4-5ubuntu3.1 uptodate

看到,有两个库没有安装成功:libjasper-devlibwebp-dev

tips:
在ubuntu系统编译安装OpenCV 4.4并配置C++和Python开发环境给出了好多依赖,不太认识,以后有机会好好研究一下。

  • 可能的报错:
topeet@iTOP-RK3588:~ $ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev libopenexr-dev libwebp-d
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libjasper-dev
E: Unable to locate package libwebp-d

参考:
1、用 add-apt-repository 管理 apt-get 源
2、OpenCV安装libjasper-dev依赖包错误:E: Unable to locate package libjasper-dev

  • 解决【没解决】:
# 因为系统是Ubuntu20.04的,把 xenial-security 改为 focal-security
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu focal-security main"
sudo apt install libjasper-dev libwebp-d

# 删除刚刚加入的源
sudo add-apt-repository --remove "deb http://security.ubuntu.com/ubuntu focal-security main"
sudo apt-get update
# 修复可能安装出错的依赖, -f 参数的意思是 fix broken(修复依赖包)
sudo apt-get install -f

解决不了,算了,也不是什么很重要的库。

3.3、开始安装OpenCV

  • 1、在OpenCV文件下创建build文件夹:
sudo mkdir build && cd build

RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第5张图片

  • 2、配置编译信息
    参考:
    1、ubuntu系统编译安装OpenCV 4.4并配置C++和Python开发环境
    2、Installation in Linux
    3、OpenCV 配置选项参考文档
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_ENABLE_NONFREE=ON \
      -D OPENCV_EXTRA_MODULES_PATH=/home/topeet/Downloads/opencv-3.4.16/opencv_contrib-3.4.16/modules \
      -D OPENCV_GENERATE_PKGCONFIG=YES \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D WITH_CUDA=OFF\
      -D BUILD_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D INSTALL_C_EXAMPLES=ON \
      -D PYTHON3_EXECUTABLE=/usr/bin/python \
	  -D PYTHON_INCLUDE_DIR=/usr/include/python3.8 \
      -D PYTHON_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python3.8 \
	  -D PYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.8.so \
	  -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3.8/dist-packages/numpy/core ..

参数解释:

  • CMAKE_BUILD_TYPE 安装的版本,Release\Debug,选择发行版Release,默认安装Release
  • CMAKE_INSTALL_PREFIX 是最终OpenCV的安装位置,默认安装在usr/local
  • OPENCV_ENABLE_NONFREE 是否使用部分被申请了专利的算方法 这里选True的话就可以使用了
  • OPENCV_EXTRA_MODULES_PATH 指示扩展算法的源码文件夹
  • OPENCV_GENERATE_PKGCONFIG 强烈建议开启这个 设置为ON。因为opencv4默认不生成.pc文件,所以加上这句用于生成opencv4.pc文件,支持pkg-config功能。opencv4版本及以上 这里用ON
  • PYTHON3_NUMPY_INCLUDE_DIRS跟官方给的不太一样,没有include文件夹
  • WITH_CUDA 该选项需要确保自己已安装显卡驱动和cuda

没有空格的版本:

sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=/home/topeet/Downloads/opencv-3.4.16/opencv_contrib-3.4.16/modules -D OPENCV_GENERATE_PKGCONFIG=YES -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_CUDA=OFF -D BUILD_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D PYTHON3_EXECUTABLE=/usr/bin/python -D PYTHON_INCLUDE_DIR=/usr/include/python3.8 -D PYTHON_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python3.8  -D PYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.8.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3.8/dist-packages/numpy/core ..

返回信息:
完整的信息记录在:RK3588编译OpenCV时cmake生成的信息

.......
-- General configuration for OpenCV 3.4.16 =====================================
--   Version control:               unknown
--
--   Extra modules:
--     Location (extra):            /home/topeet/Downloads/opencv-3.4.16/opencv_contrib-3.4.16/modules
--     Version control (extra):     unknown
--
--   Platform:
--     Timestamp:                   2023-04-20T02:51:39Z
--     Host:                        Linux 5.10.66 aarch64
--     CMake:                       3.16.3
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
--
--   CPU/HW features:
--     Baseline:                    NEON FP16
--
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++11:                       YES
--     C++ Compiler:                /usr/bin/c++  (ver 9.4.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed
--     Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt
--     3rdparty dependencies:
--
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv hdf java matlab ovis python2 sfm viz
--     Applications:                tests perf_tests examples apps
--     Documentation:               NO
--     Non-free algorithms:         YES
--
--   GUI:
--     QT:                          NO
--     GTK+:                        YES (ver 3.24.20)
--       GThread :                  YES (ver 2.64.6)
--       GtkGlExt:                  NO
--     OpenGL support:              NO
--     VTK support:                 NO
--
--   Media I/O:
--     ZLib:                        /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     /usr/lib/aarch64-linux-gnu/libImath.so /usr/lib/aarch64-linux-gnu/libIlmImf.so /usr/lib/aarch64-linux-gnu/libIex.so /usr/lib/aarch64-linux-gnu/libHalf.so /usr/lib/aarch64-linux-gnu/libIlmThread.so (ver 2_3)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--
--   Video I/O:
--     DC1394:                      YES (ver 2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 58.54.100)
--       avformat:                  YES (ver 58.29.100)
--       avutil:                    YES (ver 56.31.100)
--       swscale:                   YES (ver 5.5.100)
--       avresample:                NO
--     GStreamer:                   YES
--       base:                      YES (ver 1.16.3)
--       video:                     YES (ver 1.16.3)
--       app:                       YES (ver 1.16.3)
--       riff:                      YES (ver 1.16.3)
--       pbutils:                   YES (ver 1.16.3)
--     libv4l/libv4l2:              NO
--     v4l/v4l2:                    linux/videodev2.h
--
--   Parallel framework:            pthreads
--
--   Trace:                         YES (with Intel ITT)
--
--   Other third-party libraries:
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.7)
--     Custom HAL:                  YES (carotene (ver 0.0.1))
--     Protobuf:                    build (3.5.1)
--
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/topeet/Downloads/opencv-3.4.16/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--
--   Python 3:
--     Interpreter:                 /usr/bin/python (ver 3.8.10)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.8.so (ver 3.8.10)
--     numpy:                       /usr/lib/python3.8/dist-packages/numpy/core (ver 1.17.4)
--     install path:                lib/python3.8/dist-packages/cv2/python-3.8
--
--   Python (for build):            /usr/bin/python
--
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/topeet/Downloads/opencv-3.4.16/build
  • 3、接下来是编译:
sudo make -j8

返回信息:
完整的信息在:

  • 1、RK3588编译OpenCV3.4.16产生的信息-1
  • 2、RK3588编译OpenCV3.4.16产生的信息-2
...
[100%] Linking CXX executable ../../bin/example_tutorial_AddingImagesTrackbar
[100%] Linking CXX executable ../../bin/example_tutorial_copyMakeBorder_demo
[100%] Linking CXX executable ../../bin/example_cpp_dft
[100%] Built target example_tutorial_AddingImagesTrackbar
Scanning dependencies of target example_tutorial_periodic_noise_removing_filter
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_periodic_noise_removing_filter.dir/tutorial_code/ImgProc/periodic_noise_removing_filter/periodic_noise_removing_filter.cpp.o
[100%] Built target example_tutorial_generalContours_demo2
Scanning dependencies of target example_tutorial_cornerSubPix_Demo
[100%] Built target example_tutorial_copyMakeBorder_demo
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_cornerSubPix_Demo.dir/tutorial_code/TrackingMotion/cornerSubPix_Demo.cpp.o
[100%] Linking CXX executable ../../bin/example_cpp_imagelist_reader
[100%] Linking CXX executable ../../bin/example_tutorial_pointPolygonTest_demo
[100%] Built target example_cpp_dft
[100%] Built target example_cpp_imagelist_reader
[100%] Built target example_tutorial_pointPolygonTest_demo
[100%] Linking CXX executable ../../bin/example_tutorial_cornerDetector_Demo
[100%] Linking CXX executable ../../bin/example_tutorial_periodic_noise_removing_filter
[100%] Built target example_tutorial_cornerDetector_Demo
[100%] Built target example_tutorial_periodic_noise_removing_filter
[100%] Linking CXX executable ../../bin/example_tutorial_cornerSubPix_Demo
[100%] Built target example_tutorial_cornerSubPix_Demo
[100%] Linking CXX shared module ../../lib/python3/cv2.cpython-38-aarch64-linux-gnu.so
[100%] Built target opencv_python3
  • 4、安装
sudo make install

只展示粗略的信息,详细的记录在:RK3588编译安装OpenCV3.4.16返回的信息

topeet@iTOP-RK3588:~/Downloads/opencv-3.4.16/build $ sudo make install
[sudo] password for topeet:
[  0%] Built target gen-pkgconfig
...............................
-- Installing: /usr/local/share/OpenCV/samples/tapi/squares.cpp
-- Installing: /usr/local/share/OpenCV/samples/tapi/ufacedetect.cpp
........................
-- Installing: /usr/local/share/OpenCV/samples/python/watershed.py

3.4、配置OpenCV

暂无,直接就用了

3.5、测试OpenCV

在RK3588上测试

3.5.1、查看版本
topeet@iTOP-RK3588:~/Downloads/opencv-3.4.16/build $ pkg-config opencv --modversion
3.4.16
3.5.2、Python版本测试
topeet@iTOP-RK3588:~/Downloads/opencv-3.4.16/build $ python
Python 3.8.10 (default, Mar 13 2023, 10:26:41)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.16'
>>>

RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第6张图片

3.5.3、C++demo测试

在源码包路径中找到官方给的demo:/home/topeet/Downloads/opencv-3.4.16/samples/cpp/example_cmake/,直接编译运行:

sudo mkdir build && cd build
sudo cmake ..
sudo make
sudo ./opencv_example

能够编译,但是不能很好运行:

topeet@iTOP-RK3588:~/Downloads/opencv-3.4.16/samples/cpp/example_cmake/build $ sudo ./opencv_example
Built with OpenCV 3.4.16
[ WARN:0] OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Capture is opened
MoTTY X11 proxy: Authorisation not recognised

In case you are trying to start a graphical application with "sudo", read this article in order to avoid this issue:
https://blog.mobatek.net/post/how-to-keep-X11-display-after-su-or-sudo/

Unable to init server: Could not connect: Connection refused
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(3.4.16) /home/topeet/Downloads/opencv-3.4.16/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

Aborted

RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第7张图片
在本地串口中运行结果如下:
RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第8张图片
起码能够打开摄像头了,虽然还存在一定问题,到时候再看看!!

3.5.3、Python demo测试

测试程序
RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第9张图片
结果:
RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第10张图片
可以正常显示

3.5.4、Python调用摄像头测试
import cv2
import time

def main():
    # 使用GStreamer管道从MIPI摄像头捕获视频,添加视频帧率
    gst_pipeline = 'v4l2src device=/dev/video11 ! video/x-raw,format=NV12,width=640,height=480,framerate=30/1 ! appsink'
    # 从GStreamer管道创建OpenCV VideoCapture对象
    cap = cv2.VideoCapture(gst_pipeline, cv2.CAP_GSTREAMER)

    if not cap.isOpened():
        # 如果无法打开摄像头,则输出提示信息
        print("无法打开摄像头")
        return
    time0 = time.time() # 获取开始时间
    while True:
        # 从摄像头捕获帧
        ret, frame = cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_YUV2BGR_NV12)
        # 如果捕获到帧,则显示它
        if ret:
            time1 = time.time() # 获取结束时间
            fps = 1 / (time1 - time0) # 计算实时帧率
            time0 = time1 # 更新开始时间
            cv2.putText(frame, "FPS: {:.2f}".format(fps), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # 在图像上显示帧率
            cv2.imshow("MIPI Camera", frame)
        # 按下'q'键退出循环
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    # 释放资源并关闭窗口
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

结果:

RK3588在Ubuntu20.04上源码编译OpenCV3.4.16的C++、Python版本_第11张图片

topeet@iTOP-RK3588:~/Documents $ python testOV6595.py
Using mplane plugin for capture
[ WARN:0] OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

在这里插入图片描述

你可能感兴趣的:(RK3588,python,c++,opencv)