ubuntu下ndk编译opencv-mobile

nihui的opencv-mobile要求Android sdk大于24,且编译时关闭了rtti,所以需要自己手动编译一个
一、编译环境
系统:Ubuntu
ndk版本:android-ndk-r21d
修改环境变量

sudo vim etc/profile
#etc/profile末尾
export NDKROOT=/path/to/android-ndk-r21d
export PATH=$NDKROOT:$PATH
#启用环境变量
source profile

cmake:cmake version 3.13.3
ant安装:

sudo apt-get install ant
#配置/etc/profile
export ANT_HOME=/path/to/ant
export PATH=$ANT_HOME/bin:$PATH
#启用环境变量
source profile

二、编译
下载opencv-mobile和opencv-4.5.4

git clone https://github.com/nihui/opencv-mobile.git
cd opencv-mobile
wget -q https://github.com/opencv/opencv/archive/4.5.4.zip -O opencv-4.5.4.zip
unzip -q opencv-4.5.4.zip
cd opencv-4.5.4
mkdir build
cd build

参考https://blog.csdn.net/yang332233/article/details/105503358,开始编译armeabi-v7a

cmake -DCMAKE_TOOLCHAIN_FILE=/home/image-pc/software/android-ndk/android-ndk-r21d/build/cmake/android.toolchain.cmake -DCMAKE_ANDROID_NDK=/home/image-pc/software/android-ndk/android-ndk-r21d -DANDROID_NATIVE_API_LEVEL=21 -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Release -DBUILD_JAVA=OFF -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a  `cat ../../opencv4_cmake_options.txt`  
-DBUILD_opencv_world=OFF -DCMAKE_INSTALL_PREFIX=../install ..
make -j8
make install

能够正常生成,结果在install文件夹下

但是设置了arm64-v8a,不能生效

cmake -DCMAKE_TOOLCHAIN_FILE=/home/image-pc/software/android-ndk/android-ndk-r21d/build/cmake/android.toolchain.cmake -DCMAKE_ANDROID_NDK=/home/image-pc/software/android-ndk/android-ndk-r21d -DANDROID_NATIVE_API_LEVEL=21 -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Release -DBUILD_JAVA=OFF -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a  `cat ../../opencv4_cmake_options.txt` 
llc-DBUILD_opencv_world=OFF  -DCMAKE_INSTALL_PREFIX=../install  ..

生成的还是armeabi-v7a框架下的
强行修改/home/image-pc/software/android-ndk/android-ndk-r21d/build/cmake/android.toolchain.cmake,增加SET(ANDROID_ABI “arm64-v8a”),报错。

-- Check for working CXX compiler: /home/image-pc/software/android-ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
-- Check for working CXX compiler: /home/image-pc/software/android-ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -- broken
CMake Error at /opt/cmake-3.13.3/share/cmake-3.13/Modules/CMakeTestCXXCompiler.cmake:45 (message):
  The C++ compiler

    "/home/image-pc/software/android-ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /home/image-pc/software/opencv/opencv-mobile/opencv-4.5.4/build/CMakeFiles/CMakeTmp

    Run Build Command:"/usr/bin/make" "cmTC_785c9/fast"
    /usr/bin/make -f CMakeFiles/cmTC_785c9.dir/build.make CMakeFiles/cmTC_785c9.dir/build
    make[1]: 进入目录“/home/image-pc/software/opencv/opencv-mobile/opencv-4.5.4/build/CMakeFiles/CMakeTmp”
    Building CXX object CMakeFiles/cmTC_785c9.dir/testCXXCompiler.cxx.o
    /home/image-pc/software/android-ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/home/image-pc/software/android-ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/image-pc/software/android-ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/sysroot    -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security    -fPIE   -o CMakeFiles/cmTC_785c9.dir/testCXXCompiler.cxx.o -c /home/image-pc/software/opencv/opencv-mobile/opencv-4.5.4/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
    clang++: error: the clang compiler does not support '-march=armv7-a'
    CMakeFiles/cmTC_785c9.dir/build.make:65: recipe for target 'CMakeFiles/cmTC_785c9.dir/testCXXCompiler.cxx.o' failed
    make[1]: *** [CMakeFiles/cmTC_785c9.dir/testCXXCompiler.cxx.o] Error 1
    make[1]: 离开目录“/home/image-pc/software/opencv/opencv-mobile/opencv-4.5.4/build/CMakeFiles/CMakeTmp”
    Makefile:121: recipe for target 'cmTC_785c9/fast' failed
    make: *** [cmTC_785c9/fast] Error 2




  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:107 (enable_language)


-- Configuring incomplete, errors occurred!
See also "/home/image-pc/software/opencv/opencv-mobile/opencv-4.5.4/build/CMakeFiles/CMakeOutput.log".
See also "/home/image-pc/software/opencv/opencv-mobile/opencv-4.5.4/build/CMakeFiles/CMakeError.log".

数次尝试,无果

参考https://www.jianshu.com/p/45fb1dc6a041,对比命令,发现DCMAKE_ANDROID_ARCH_ABI这个编译选项没有生效,修改为DANDROID_ABI,可以正常编译得到结果

成功命令

cmake -DCMAKE_TOOLCHAIN_FILE=/home/image-pc/software/android-ndk/android-ndk-r21d/build/cmake/android.toolchain.cmake -DCMAKE_ANDROID_NDK=/home/image-pc/software/android-ndk/android-ndk-r21d -DANDROID_NATIVE_API_LEVEL=21 -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Release -DBUILD_JAVA=OFF -DANDROID_ABI=armeabi-v7a  -DBUILD_opencv_world=OFF -DCMAKE_INSTALL_PREFIX=../install `cat ../../opencv4_cmake_options.txt`  ..
make -j8 
make install
cmake -DCMAKE_TOOLCHAIN_FILE=/home/image-pc/software/android-ndk/android-ndk-r21d/build/cmake/android.toolchain.cmake -DCMAKE_ANDROID_NDK=/home/image-pc/software/android-ndk/android-ndk-r21d -DANDROID_NATIVE_API_LEVEL=21 -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Release -DBUILD_JAVA=OFF -DANDROID_ABI=arm64-v8a  -DBUILD_opencv_world=OFF -DCMAKE_INSTALL_PREFIX=../install `cat ../../opencv4_cmake_options.txt`  ..
make -j8 
make install

yolox的Android demo是通过c++调用手机摄像头获取图像来实现实时的目标检测,我通过Android camera调用,发现速度异常,一直因为是ncnn关于cpu大小核设置问题,但是尝试了很久,一直没有什么进度。直到最后发现,是opencv-mobile的问题,在Ubuntu下使用上面的方法重新编译opencv后,速度还是慢。最后不使用cat …/…/opencv4_cmake_options.txt后,编译出的opencv-mobile,在手机上速度正常。

你可能感兴趣的:(Android,opencv,ubuntu,ndk)