本文以编译opencv3.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
注意: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.0opencv
$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/bash2 NDK_ROOT="${1:-${NDK_ROOT}}"
3
4 ###ABIs setup5 #declare -a ANDROID_ABI_LIST=("x86" "x86_64" "armeabi-v7a with NEON" "arm64-v8a")
6 declare -a ANDROID_ABI_LIST=("armeabi-v7a")
7
8 ### pathsetup9 SCRIPT=$(readlink -f $0)
10 WD=`dirname $SCRIPT`11 OPENCV_ROOT="${WD}/opencv"
12 N_JOBS=${N_JOBS:-4}13
14 ###Download android-cmake15 if [ ! -d "${WD}/android-cmake" ]; then
16 echo‘Cloning android-cmake‘17 git clone https://github.com/taka-no-me/android-cmake.git18 fi19
20 INSTALL_DIR="${WD}/android_opencv"
21 rm -rf "${INSTALL_DIR}/opencv"
22
23 ###Make each ABI target iteratly and sequentially24 for i in "${ANDROID_ABI_LIST[@]}"
25 do
26 ANDROID_ABI="${i}"
27 echo "Start building ${ANDROID_ABI} version"
28
29 if [ "${ANDROID_ABI}" = "armeabi" ]; then
30 API_LEVEL=19
31 else
32 API_LEVEL=21
33 fi34
35 temp_build_dir="${OPENCV_ROOT}/platforms/build_android_${ANDROID_ABI}"
36 ### Remove the build folder first,and create it37 rm -rf "${temp_build_dir}"
38 mkdir -p "${temp_build_dir}"
39 cd "${temp_build_dir}"
40
41 cmake -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 it56 make -j${N_JOBS}57 #Install it58 make install/strip59 ###Remove temp build folder60 cd "${WD}"
61 rm -rf "${temp_build_dir}"
62 echo "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.shStart 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 notfindneither 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 NDKinthe default path:sudo ln -s ~/my-android-ndk /opt/android-ndksudo 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 notfind 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 notfind 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 tofindthe compiler by setting the CMake cache entry
CMAKE_CXX_COMPILER to the full path to the compiler, or to the compiler
nameif it is inthe PATH.
CMake Error at CMakeLists.txt:127(project):
No CMAKE_C_COMPILER could be found.
Tell CMake where tofindthe compiler by setting the CMake cache entry
CMAKE_C_COMPILER to the full path to the compiler, or to the compiler nameif it is inthe 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
解决方法
方法一(推荐方法)
从命令行传入NDK_ROOT 目录,如下:
$ ./build-android-opencv.sh /mnt/work/android/android-sdk-linux/ndk-bundle/
方法二,
根据提示,设置环境变量 ANDROID_NDK,如下
$ export ANDROID_NDK=/mnt/work/android/android-sdk-linux/ndk-bundle/$env | grepANDROID_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 链接一 链接二(转载)