码迷,mamicode.com
首页 > 移动开发 > 详细

如何为Android平台编译 opencv3 和 opencv_contrib (Linux)

时间:2017-09-20 21:58:49      阅读:724      评论:0      收藏:0      [点我收藏+]

标签:删除   hash   编译错误   matlab   ott   done   logs   格式   core   

本文以编译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)

标签:删除   hash   编译错误   matlab   ott   done   logs   格式   core   

原文地址:http://www.cnblogs.com/klchang/p/7563494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!