一身转战三千里,一剑曾百万师。这篇文章主要讲述如何为Android平台编译 opencv3 和 opencv_contrib (Linux)相关的知识,希望能为你提供帮助。
p { margin-bottom: 0.1in;
direction: ltr;
color: rgba(0, 0, 10, 1);
line-height: 120%;
text-align: left }
p.western { font-family: "Liberation Serif", serif;
font-size: 12pt }
p.cjk { font-family: "Source Han Sans CN Regular";
font-size: 12pt }
p.ctl { font-family: "Lohit Devanagari";
font-size: 12pt }
a:link { }编译出来的opencv库有问题,正在调试中 ......
本文以编译opencv 3.3.0 和 opencv_contrib 3.3.0为例,系统为 Linux x64 (Fedora 21),具体步骤如下:
1. 下载 Source code zip 压缩包从下面网址,选择 opencv 3.3.0 Source code 下载
https://github.com/opencv/opencv/releases
从下面网址,选择下载与
opencv
3.3.0 对应的
opencv_contrib
3.3.0
https://github.com/opencv/opencv_contrib/releases
【如何为Android平台编译 opencv3 和 opencv_contrib (Linux)】注意:opencv和opencv_contrib
版本必须保持一致,否则可能会出现编译错误。
2.
下载
build-opencv-for-android
开源代码
$ cd /mnt/work $ git clone https://github.com/tzutalin/build-opencv-for-android.git $ cd build-opencv-for-android
3. 解压源码包并重命名将opencv-3.3.0.zip 和 opencv_contrib-3.3.0.zip 都解压到下载的build-opencv-for-android 开源代码 所在目录 /mnt/work/build-opencv-for-android 。
$ unzip opencv-3.3.0.zip -d /mnt/work/build-opencv-for-android $ unzip opencv_contrib-3.3.0.zip -d /mnt/work/build-opencv-for-android $ cd /mnt/work/build-opencv-for-android $ mv opencv-3.3.0 opencv $ mv opencv_contrib-3.3.0 opencv_contrib
4. 修改 build-opencv-for-android 开源代码中 build-android-opencv.sh 修改内容如下:
修改 第 6 行代码:
declare -a ANDROID_ABI_LIST=("x86" "x86_64" "armeabi" "arm64-v8a" "armeabi-v7a" "mips" "mips64")
为
declare -a ANDROID_ABI_LIST=("armeabi-v7a")
最终代码,如下:
文章图片
文章图片
1 #!/bin/bash 2 NDK_ROOT="${1:-${NDK_ROOT}}" 3 4 ### ABIs setup 5 #declare -a ANDROID_ABI_LIST=("x86" "x86_64" "armeabi-v7a with NEON" "arm64-v8a") 6 declare -a ANDROID_ABI_LIST=("armeabi-v7a") 7 8 ### path setup 9 SCRIPT=$(readlink -f $0) 10 WD=`dirname $SCRIPT` 11 OPENCV_ROOT="${WD}/opencv" 12 N_JOBS=${N_JOBS:-4} 13 14 ### Download android-cmake 15 if [ ! -d "${WD}/android-cmake" ]; then 16echo \'Cloning android-cmake\' 17git clone https://github.com/taka-no-me/android-cmake.git 18 fi 19 20 INSTALL_DIR="${WD}/android_opencv" 21 rm -rf "${INSTALL_DIR}/opencv" 22 23 ### Make each ABI target iteratly and sequentially 24 for i in "${ANDROID_ABI_LIST[@]}" 25 do 26ANDROID_ABI="${i}" 27echo "Start building ${ANDROID_ABI} version" 28 29if [ "${ANDROID_ABI}" = "armeabi" ]; then 30API_LEVEL=19 31else 32API_LEVEL=21 33fi 34 35temp_build_dir="${OPENCV_ROOT}/platforms/build_android_${ANDROID_ABI}" 36### Remove the build folder first, and create it 37rm -rf "${temp_build_dir}" 38mkdir -p "${temp_build_dir}" 39cd "${temp_build_dir}" 40 41cmake -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \\ 42-DCMAKE_TOOLCHAIN_FILE="${WD}/android-cmake/android.toolchain.cmake" \\ 43-DANDROID_NDK="${NDK_ROOT}" \\ 44-DANDROID_NATIVE_API_LEVEL=${API_LEVEL} \\ 45-DANDROID_ABI="${ANDROID_ABI}" \\ 46-D WITH_CUDA=OFF \\ 47-D WITH_MATLAB=OFF \\ 48-D BUILD_ANDROID_EXAMPLES=OFF \\ 49-D BUILD_DOCS=OFF \\ 50-D BUILD_PERF_TESTS=OFF \\ 51-D BUILD_TESTS=OFF \\ 52-DOPENCV_EXTRA_MODULES_PATH="${WD}/opencv_contrib/modules/"\\ 53-DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}/opencv" \\ 54../.. 55# Build it 56make -j${N_JOBS} 57# Install it 58make install/strip 59### Remove temp build folder 60cd "${WD}" 61rm -rf "${temp_build_dir}" 62echo "end building ${ANDROID_ABI} version" 63 done
View Code说明:
1). 由于目前 大多数 android 平台使用arm cpu, 采用armeabi-v7 指令集,为了减少编译时间,所有我只保留 "armeabi-v7a" , 其他删除,如果需要也可保留。
2). armeabi-v7 代表 arm EABI version 7 指令集, EABI 为 嵌入应用二进制接口 (Embedded Application Binary Interface) 。
5. 执行脚本 build-android-opencv.sh
$ ./build-android-opencv.sh /mnt/work/android/android-sdk-linux/ndk-bundle
注:build-android-opencv.sh脚本的通用的执行命令格式为“./build-android-opencv.sh [NDK_ROOT]”,其中,参数NDK_ROOT是可选的,表示Android Native Development Kit (NDK) 顶层目录,如果没有提供,则会试图寻找环境变量 ANDROID_NDK。
6. 查看编译结果编译成功后,会在 build-opencv-for-android 目录,即build-android-opencv.sh脚本所在目录,生成 android_opencv目录,其目录结构如下:
$ tree -d android_opencv android_opencv └── opencv ├── apk └── sdk ├── etc │├── haarcascades │└── lbpcascades └── native ├── 3rdparty │└── libs │└── armeabi-v7a ├── jni │├── abi-armeabi-v7a │└── include │├── opencv │└── opencv2 │├── aruco │├── bioinspired │├── calib3d │├── ccalib │├── core ││├── cuda │││└── detail ││├── hal ││└── utils │├── datasets │├── dnn │├── face │├── features2d │├── flann │├── fuzzy │├── highgui │├── imgcodecs │├── img_hash │├── imgproc ││├── detail ││└── hal │├── line_descriptor │├── ml │├── objdetect │├── optflow │├── phase_unwrapping │├── photo │├── reg │├── rgbd │├── saliency │├── shape │├── stereo │├── stitching ││└── detail │├── structured_light │├── superres │├── surface_matching │├── text │├── tracking │├── video │├── videoio │├── videostab │├── xfeatures2d │├── ximgproc │└── xphoto └── libs └── armeabi-v7a62 directories
问题与解决方法 问题一
$ ./build-android-opencv.sh Start building armeabi-v7a version CMake Error at /mnt/work/video/res/opencv/build-opencv-for-android-master/android-cmake/android.toolchain.cmake:446 (message): Could not find neither Android NDK nor Android standalone toolchain.You should either set an environment variable: export ANDROID_NDK=~/my-android-ndk or export ANDROID_STANDALONE_TOOLCHAIN=~/my-android-toolchain or put the toolchain or NDK in the default path: sudo ln -s ~/my-android-ndk /opt/android-ndk sudo ln -s ~/my-android-toolchain /opt/android-toolchain Call Stack (most recent call first): /usr/share/cmake/Modules/CMakeDetermineSystem.cmake:98 (include) CMakeLists.txt:127 (project)CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly. Missing variable is: CMAKE_CXX_COMPILER_ENV_VAR CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly. Missing variable is: CMAKE_CXX_COMPILER CMake Error: Could not find cmake module file: /mnt/work/video/res/opencv/build-opencv-for-android-master/opencv/platforms/build_android_armeabi-v7a/CMakeFiles/3.0.2/CMakeCXXCompiler.cmake CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly. Missing variable is: CMAKE_C_COMPILER_ENV_VAR CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly. Missing variable is: CMAKE_C_COMPILER CMake Error: Could not find cmake module file: /mnt/work/video/res/opencv/build-opencv-for-android-master/opencv/platforms/build_android_armeabi-v7a/CMakeFiles/3.0.2/CMakeCCompiler.cmake CMake Error at CMakeLists.txt:127 (project): No CMAKE_CXX_COMPILER could be found.Tell CMake where to find the compiler by setting the CMake cache entry CMAKE_CXX_COMPILER to the full path to the compiler, or to the compiler name if it is in the PATH.CMake Error at CMakeLists.txt:127 (project): No CMAKE_C_COMPILER could be found.Tell CMake where to find the compiler by setting the CMake cache entry CMAKE_C_COMPILER to the full path to the compiler, or to the compiler name if it is in the PATH.CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage -- Configuring incomplete, errors occurred! make: *** No targets specified and no makefile found.Stop. make: *** No rule to make target \'install/strip\'.Stop. end building armeabi-v7a version
解决方法
方法一(推荐方法)
p { margin-bottom: 0.1in; direction: ltr; color: rgba(0, 0, 10, 1); line-height: 120%; text-align: left } p.western { font-family: "Liberation Serif", serif; font-size: 12pt } p.cjk { font-family: "Source Han Sans CN Regular"; font-size: 12pt } p.ctl { font-family: "Lohit Devanagari"; font-size: 12pt } a:link { }从命令行传入 NDK_ROOT 目录,如下:
$ ./build-android-opencv.sh /mnt/work/android/android-sdk-linux/ndk-bundle/
方法二,
根据提示,设置环境变量 ANDROID_NDK,如下
p { margin-bottom: 0.1in; direction: ltr; color: rgba(0, 0, 10, 1); line-height: 120%; text-align: left } p.western { font-family: "Liberation Serif", serif; font-size: 12pt } p.cjk { font-family: "Source Han Sans CN Regular"; font-size: 12pt } p.ctl { font-family: "Lohit Devanagari"; font-size: 12pt } a:link { }
$ export ANDROID_NDK=/mnt/work/android/android-sdk-linux/ndk-bundle/ $ env | grep ANDROID_NDK ANDROID_NDK=/mnt/work/android/android-sdk-linux/ndk-bundle/ $ ./build-android-opencv.sh
参考资料[1] Build and install OpenCV and contrib lib on Ubuntu / Android 链接一 链接二(转载)
[2] 如何编译 opencv3 和 opencv_contrib(Linux)
推荐阅读
- Xamarin.Android Binding 源自github第三方库的绑定(中级教学)----aar文件
- 使用MyBatis Generator自动生成实体mapper和dao层
- Android——调用高德地图API前期准备
- NetApp移除ADP分区后的disk的owner
- Jboss启动报错——DailyRollingFileAppender无法转换异常
- Android App退出检测
- 解决安卓手机键盘弹出时会把背景或百分比定位的布局压缩的问题
- androidActivity 的生命周期 以及横屏竖屏切换时 Activity 的状态变化
- android——使用Ijkplayer打造自己的超级电视台播放软件