編譯說明
opencv 下包含很多模塊,各個模塊的作用可以參考Opencv—模塊概覽. 嵌入式考慮有限存儲等因素會對模塊進行裁剪,我這里主要保留圖像拼接(stitching)圖片編解碼(imgcodecs)與特征點匹配(feature2d)及其依賴模塊,下面是編譯過程。
編譯過程
- 在 github下載opencv庫.
- 配置交叉編譯工具,我這里是 hi3159dv500 的交叉編譯工具鏈,大家根據自己的進行cmake配置。
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)# 環境庫
set(CMAKE_FIND_ROOT_PATH /home/hisi/workspace/hocker/pack/3ds/buildroot/output/hisi_3519/host/aarch64-buildroot-linux-gnu/sysroot)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_SYSROOT ${CMAKE_FIND_ROOT_PATH})# 安裝路徑配置,這個他們需要絕對路徑
set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/lib-aarch64" CACHE PATH "Installation Directory") set(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH FALSE)
# set(CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH FALSE)
set(CMAKE_FIND_USE_CMAKE_PATH FALS)set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_FIND_ROOT_PATH})
set(ENV{PKG_CONFIG_LIBDIR} ${CMAKE_FIND_ROOT_PATH}/usr/lib/pkgconfig)
set(ENV{PKG_CONFIG_PATH} "")set(TARGET_ARCH arm64)
cd build
# rm ../build/* -rf
# 這里的命令一定要加 TARGET_ARCH=arm64, 不然 編譯 png 的時候 不支持 neno, 導致 imgcodecs 編譯失敗
cmake .. \-DBUILD_LIST=core,imgproc,features2d,stitching,calib3d,flann,imgcodecs \-DBUILD_opencv_dnn=OFF \-DBUILD_opencv_highgui=OFF \-DBUILD_opencv_videoio=OFF \-DBUILD_opencv_calib3d=ON \-DWITH_PYTHON=OFF \-DWITH_opencv_stitching=ON \-DBUILD_opencv_world=OFF \-DBUILD_TESTS=OFF \-DBUILD_EXAMPLES=OFF \-DWITH_GTK=OFF \-DWITH_JPEG=ON \-DWITH_PNG=ON \-DTARGET_ARCH=arm64 \-DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake
make -j8
編譯結果
- 編譯過程輸出(這里主要看庫編譯過程,僅供參考)
-- Build files have been written to: /home/hisi/workspace/hocker/3ds/opencv-4.10.0/build
Consolidate compiler generated dependencies of target libopenjp2
Consolidate compiler generated dependencies of target jpeg16-static
Consolidate compiler generated dependencies of target zlib
[ 0%] Built target opencv_dnn_plugins
Consolidate compiler generated dependencies of target jpeg12-static
[ 0%] Built target opencv_highgui_plugins
[ 0%] Built target opencv_videoio_plugins
Consolidate compiler generated dependencies of target libwebp
[ 3%] Built target zlib
[ 7%] Built target libopenjp2
[ 10%] Built target jpeg16-static
Scanning dependencies of target libpng
Consolidate compiler generated dependencies of target libpng
Consolidate compiler generated dependencies of target libtiff
[ 15%] Built target jpeg12-static
[ 18%] Built target libpng
Consolidate compiler generated dependencies of target libjpeg-turbo
[ 26%] Built target libtiff
Consolidate compiler generated dependencies of target opencv_core
[ 45%] Built target libwebp
[ 54%] Built target libjpeg-turbo
[ 69%] Built target opencv_core
Consolidate compiler generated dependencies of target opencv_version
Consolidate compiler generated dependencies of target opencv_flann
[ 69%] Built target opencv_version
[ 70%] Built target opencv_flann
Consolidate compiler generated dependencies of target opencv_imgproc
[ 82%] Built target opencv_imgproc
Consolidate compiler generated dependencies of target opencv_imgcodecs
Consolidate compiler generated dependencies of target opencv_features2d
[ 90%] Built target opencv_imgcodecs
[ 90%] Built target opencv_features2d
Consolidate compiler generated dependencies of target opencv_calib3d
[ 97%] Built target opencv_calib3d
Consolidate compiler generated dependencies of target opencv_stitching
[100%] Built target opencv_stitching
- 編譯出來的庫文件如下
$ ls ../build/lib/
libopencv_calib3d.so libopencv_features2d.so.410 libopencv_imgcodecs.so.4.10.0
libopencv_calib3d.so.410 libopencv_features2d.so.4.10.0 libopencv_imgproc.so
libopencv_calib3d.so.4.10.0 libopencv_flann.so libopencv_imgproc.so.410
libopencv_core.so libopencv_flann.so.410 libopencv_imgproc.so.4.10.0
libopencv_core.so.410 libopencv_flann.so.4.10.0 libopencv_stitching.so
libopencv_core.so.4.10.0 libopencv_imgcodecs.so libopencv_stitching.so.410
libopencv_features2d.so libopencv_imgcodecs.so.410 libopencv_stitching.so.4.10.0
- 編譯出來的庫大小
$ du --max-depth=0 -h ../build/lib/
16M ../build/lib/
- 編譯出來的庫結果
$ file ../build/lib/libopencv_*.4.10.0
../build/lib/libopencv_calib3d.so.4.10.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (GNU/Linux), dynamically linked, with debug_info, not stripped
../build/lib/libopencv_core.so.4.10.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (GNU/Linux), dynamically linked, with debug_info, not stripped
../build/lib/libopencv_features2d.so.4.10.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (GNU/Linux), dynamically linked, with debug_info, not stripped
../build/lib/libopencv_flann.so.4.10.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (GNU/Linux), dynamically linked, with debug_info, not stripped
../build/lib/libopencv_imgcodecs.so.4.10.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (GNU/Linux), dynamically linked, with debug_info, not stripped
../build/lib/libopencv_imgproc.so.4.10.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (GNU/Linux), dynamically linked, with debug_info, not stripped
../build/lib/libopencv_stitching.so.4.10.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (GNU/Linux), dynamically linked, with debug_info, not stripped