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

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); 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 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

注意: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
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

说明:

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 

解决方法

方法一(推荐方法)

p { margin-bottom: 0.1in; direction: ltr; color: rgb(0, 0, 10); 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: rgb(0, 0, 10); 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)

时间: 2024-08-07 22:37:06

如何为Android平台编译 opencv3 和 opencv_contrib (Linux)的相关文章

Cocos2d-x移植到Android平台编译的两个文件Android.mk和Application.mk

背景 首先,说说文章的背景.最近手中的一个项目,由于需求中要求提供Web界面的打印功能,当然如果没有打印机,还可以提供保存到本地.项目组长把这个"小任务"分给了我.本着努力为组长分忧解难的思想,领了任务之后,就马上开始了工作. 问题 刚开始的时候,组长给了一个工具(jatoolsprinter)让我研究,我用了一个多小时的时间,做出了一个简单的Demo,然后就是各种的测试,由于 web 打印需要浏览器安装 ActiveX 组件,在随后的测试中,我用了几款浏览器,甚至把安全级别都调到了最

Ogre3d Android平台编译教程

上一篇我们讲了Ogre3d 在 Window平台的编译流程方法 点击跳转 这一篇我们介绍 Ogre3d 编译到Android 平台的方法.可以和官方英文教程对照学习. 转载自博客 http://blog.csdn.net/huutu QQ:790621656 首先下载Ogre3d 依赖库源代码 和Ogre3d 源代码,下载方法在上一篇中介绍了,这里不再重复. 转载自博客 http://blog.csdn.net/huutu QQ:790621656 编译工具:NDK.Visual Studio.

Webrtc入门——基于阿里云ubuntu 最新webrtc Android平台编译详细说明

折腾了好多天,终于将webrtc Android平台的demo编译出来了,发现即使完全按照webrtc官网的编译指南,还是会碰到各种问题.而网上又没有更全面的说明,最后是通过查看webrtc 批处理脚本才最终一步步解决问题.写下这篇文章,希望对大家有帮助. 在编译的过程中,感觉webrtc编译实在是太难了, 但是完成了编译之后,感觉webrtc编译其实是很简单的,造成这个强烈反差的主要原因是,google工程师将几乎所有编译过程中需要使用的软件.依赖配置.环境变量,都已经写成了脚本,你只要运行就

在cocos2d-x-3.0 android 平台编译时提示CocosGUI.h: No such file or directory

分类是个让人蛋疼的事情,所幸自己的博客自己做主.这是个高兴的开始. 每天抽空玩2048,终于忍受不住,于是决定自己从网上download下源码,自己编译一个出来.所有的事情都很容易,除了操蛋的中文注释,在Unicode(UTF-8无签名)-代码页 65001的编码格式下,会产生很操蛋的错误提示. 当vs2012提示的错误让人摸不着头脑如坠毒海祸水时,首先应该怀疑编码格式,以及是否有中文注释. 然后,给vs添加了CocoStudio所应引用的三个库: HelloCocos\cocos2d\coco

android平台编译IW

不知道怎么编译IW,所以就搜索网络上的博客http://blog.csdn.net/jksl007/article/details/16862435 首先,iw依赖于libnl库,而目前android自身没有携带libnl库,所以要移植iw就要把libnl一起移植上去.好在Dominik Schurmann这哥们已经把一个移植好的libnl库挂在了github上,https://github.com/dschuermann/libnl-3-android,git clone下来. 在安装了and

Win7+VS2010+cocos2d-x 2.1 Beta3+Sqlite工程向android平台移植

Win7+VS2010+cocos2d-x 2.1 Beta3+Sqlite工程向android平台移植 题外话: 有时打开博客想写点什么时,心中感到有点酸楚,尽管语言不那么精简,或者说是准确,而且很可能文中的总结极不成熟,甚至还夹带些错误:但是,它们毕竟是自 己经过"痛苦"的挣扎后取得的一点成绩,于是,还是下决心记下来,一方面充实了自己尘封的笔记,另一方面也不敢独享-因为我看到有那么多的朋友把自己的心 血也无保留地奉献出来.或许,我的这一点一滴也能成为他们决战BUGS中的LITTLE

boost全平台编译方法

0.通用规则 boost自带一套编译工具bjam,bjam本身是跨平台的,并且也要自行编译出来.在boost目录下有bootstrap.sh和bootstrap.bat两个脚本分别用来编译*nix和windows下的bjam.bootstrap脚本可以传入参数,以在编译bjam过程中生成特定的编译boost的配置.编译bjam过程中生成的project-config.jam就是默认的配置,但还可以在运行bjam的时候再传入参数来覆盖.同时生成的b2是bjam的代理,运行哪个的效果差不多. 在终端

Windows环境中编译opencv3.0同时加入OPENCV_contrib库及解决遇到相关问题

因为opencv3.0默认安装中没有加入SIFT.SURF等点特征检测,一百度下一跳,原来这玩意还弄了个其他的库,还只能在GitHub上才能弄到,这两天弄opencv3的contrib库,遇到了一些问题,为了让大家更好.更快用上contrib库,根据谷歌百度的几个问题,现分享给大家完整流程,也欢迎大家与我讨论其他opencv方面的问题. 一.下载安装准备 github  desktop 下载      windows 7or Later : https://desktop.github.com

直播技术总结(二)ijkplayer的编译到Android平台并测试解码库

转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/55670380 前言:ijkplayer,是b站工程师开源的播放器框架,基于FFmpeg及MediaCodec,内部实现软解及硬解的功能,对于没有自研底层播放器的公司,用它确实是比较合适了.关于介绍可以直接看:https://github.com/Bilibili/ijkplayer,今天主要是对ijkplayer进行编译在Andr