1. 程式人生 > >如何為Android平臺編譯 opencv3 和 opencv_contrib (Linux)

如何為Android平臺編譯 opencv3 和 opencv_contrib (Linux)

gre inter jni fuzzy dnn letter ott esc lis

本文以編譯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

註意:opencvopencv_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
16     echo ‘Cloning android-cmake‘
17     git 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
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     fi
34 
35     temp_build_dir="${OPENCV_ROOT}/platforms/build_android_${ANDROID_ABI}"
36     ### Remove the build folder first, and create it
37     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 it
56     make -j${N_JOBS}
57     # Install it
58     make install/strip
59     ### Remove temp build folder
60     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-v7a

62 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 

解決方法

方法一(推薦方法)

從命令行傳入 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 | 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)

如何為Android平臺編譯 opencv3 和 opencv_contrib (Linux)