在开始之前,关于raspberry 4b的安装推荐两个教程。其中之一是64bit os安装opencv:
64bits, 32bits 其中64bits的安装验证成功
1、源文件下载目录
cv: https://github.com/opencv/opencv/archive/4.4.0.tar.gz
cv_contrib:https://github.com/opencv/opencv_contrib/archive/4.4.0.tar.gz
2、编译参考文档
https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
# or more
sudo apt-get install build-essential cmake git unzip pkg-config libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libgtk2.0-dev libcanberra-gtk* libxvidcore-dev libx264-dev libgtk-3-dev python3-dev python3-numpy python3-pip python-dev python-numpy
sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev libv4l-dev v4l-utils libjasper-dev libopenblas-dev libatlas-base-dev libblas-dev liblapack-dev gfortran gcc-arm* protobuf-compiler
3、libjasper-dev找不到怎么办?
可以不用管libjasper,这个包已经废弃了
4、cmake怎么编译
下面是不带cuda时常用的cmake设置:
cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DPYTHON3_EXECUTABLE=/usr/bin/python3 \
-DPYTHON_INCLUDE_DIR=/usr/include/python3.8 \
-DPYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.8 \
-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.so \
-DPYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ \
-DINSTALL_C_EXAMPLES=OFF \
-DINSTALL_PYTHON_EXAMPLES=OFF \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DOPENCV_EXTRA_MODULES_PATH=~/Projects/opencv/opencv_contrib-4.4.0/modules \
-DBUILD_EXAMPLES=OFF ..
# or this on raspberry
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-DOPENCV_EXTRA_MODULES_PATH=~/Projects/opencv/opencv_contrib-4.4.0/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D WITH_OPENMP=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
5、make -j8出错
确认一下自己的核心,如果时嵌入式系统,尽量留出几个核心忙别的
安装openjdk:
sudo apt-get install openjdk-11-jdk
6、make中遇到的1301错误
点击上面的链接看问题原因。为了防止make很久之后,再出这个问题。一定要在cmake完毕之后,要先检查一下opencv-4.4.0/.cache/xfeatures2d里面的文件数目。尤其是看看有没有空文件。所谓空文件是用ls -al查看一下容量是不是0或者说很小。这些文件一般都是几K的。
目录应当如下:
.cache
│
└───xfeatures2d
├───boostdesc
│ 0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i
│ 0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i
│ 202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i
│ 232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i
│ 324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i
│ 98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i
│ e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i
│
└───vgg
151805e03568c9f490a5e3a872777b75-vgg_generated_120.i
7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i
7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i
e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i
一个文件夹7个文件,另一个4个。出现文件不全或者空文件的时候。可以尝试用打洞的技术用curl下载:
mkdir boostdesc && cd boostdesc
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i
和
cd ..
mkdir vgg && cd vgg
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i > 151805e03568c9f490a5e3a872777b75-vgg_generated_120.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i > 7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i > 7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i > e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i
然后将文件替换。如果下载不了可以尝试从附件下载。
7、make失败的主要问题
在make之前cmake时,会很详尽的检查编译环境。会告诉你编译时缺失的各种包。根据需求不同,make的最终以来也不同。但建议详细查看一下最后的编译汇总。大概如下:
--
-- General configuration for OpenCV 4.4.0 =====================================
-- Version control: unknown
--
-- Platform:
-- Timestamp: 2020-08-18T07:33:46Z
-- Host: Linux 5.4.58-v8+ aarch64
-- CMake: 3.13.4
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: RELEASE
--
-- CPU/HW features:
-- Baseline: NEON FP16
-- required: NEON
-- disabled: VFPV3
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /usr/bin/c++ (ver 8.3.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 -Winit-self -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 -Winit-self -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=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -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=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -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: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python2 python3 stitching ts video videoio
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: java js
-- Applications: tests perf_tests examples apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- GUI:
-- GTK+: YES (ver 2.24.32)
-- GThread : YES (ver 2.58.3)
-- GtkGlExt: 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 62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.36)
-- TIFF: /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
-- JPEG 2000: build Jasper (ver 1.900.1)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: YES (2.2.5)
-- FFMPEG: YES
-- avcodec: YES (58.35.100)
-- avformat: YES (58.20.100)
-- avutil: YES (56.22.100)
-- swscale: YES (5.3.100)
-- avresample: NO
-- GStreamer: NO
-- v4l/v4l2: YES (linux/videodev2.h)
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.5.1)
--
-- OpenCL: YES (no extra features)
-- Include path: /home/pi/Workspace/opencv/opencv-4.4.0/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.16)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.so
-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.2)
-- install path: lib/python2.7/dist-packages/cv2/python-2.7
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.7.3)
-- Libraries: /usr/lib/aarch64-linux-gnu/libpython3.7m.so (ver 3.7.3)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.16.2)
-- install path: lib/python3.7/dist-packages/cv2/python-3.7
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring incomplete, errors occurred!
See also "/home/pi/Workspace/opencv/opencv-4.4.0/build/CMakeFiles/CMakeOutput.log".
See also "/home/pi/Workspace/opencv/opencv-4.4.0/build/CMakeFiles/CMakeError.log".
这里最后会提示在build的CMakeFiles中会生成log文件。如果出错可以先检查一下。
8、提升编译速度
除了使用make -jn来提升之外。别忘了安装ccache。这个工具可以提高编译速度:
sudo apt-get install ccache
9、最好安装LAPACK和EIGEN库
前者能提升线性代数的算法效率
sudo apt-get install liblapacke-dev libeigen3-dev
以上安装并不能保证好用,建议还是从各自的网站下载编译安装。
解决方案:https://github.com/opencv/opencv/issues/12957