最近有个Android项目,需要使用opencv的TPS来矫正图像,找了一下没找到可以直接用的库,决定自己编译。
一、系统环境:
1) 系统:Ubuntu18.04
2) 安装依赖:
参考:https://blog.csdn.net/heiheiya/article/details/80998082
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libdc1394-22-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install ffmpeg
编译过程中,发现还有几个库需要安装:
sudo apt-get install ninja-build
sudo apt-get install libboostdesc-all-dev
二、编译环境:
参考:Ubuntu 上编译opencv_contrib模块for Android - Maddock - 博客园
1) JDK:java-8-openjdk-amd64
注:JAVA_HOME设为:/usr/lib/jvm/java-11-openjdk-amd64/
2) Android NDK:android-ndk-r18b(笔者放到了~/android/ndk目录下),下载地址:
NDK 修订历史记录 | Android NDK | Android Developers
3) Android SDK:
下载sdk-tools-linux-4333796.zip
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
解压到新建的sdk目录(笔者放到了~/android/sdk目录下),进入到tools/bin/目录,使用以下命令下载sdk套件:
./sdkmanager "build-tools;28.0.0"
./sdkmanager "ndk-bundle"
./sdkmanager "ndk;20.0.5594570"
./sdkmanager "platforms;android-28"
./sdkmanager "sources;android-28"
./sdkmanager "system-images;android-28;google_apis;x86_64"
4) make:4.1
5) cmake:3.10.2
6) python 2.7.17
三、clone代码
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
由于项目需要,都切换到tag: 4.2.0:
git checkout 4.2.0
四、编译
参考:基于android的opencv4.0.1+opencv_contrib4.0.1编译 - 掘金
1) 在opencv目录下创建build目录
cd ~/git/opencv/ & mkdir build
1) 选择硬件平台
cd ~/git/opencv/platforms/android
vim ndk-18-api-level-21.config.py
笔者只需要编译armeabi-v7a和arm64-v8a,故注释掉其他平台
ABIs = [
ABI("2", "armeabi-v7a", None, 21, cmake_vars=dict(ANDROID_ABI='armeabi-v7a with NEON')),
ABI("3", "arm64-v8a", None, 21),
#ABI("5", "x86_64", None, 21),
#ABI("4", "x86", None, 21),
]
注:不选也没问题,所有平台都会编译一遍,比较耗时罢了
2) 开始编译
./build_sdk.py --ndk_path ~/android/ndk/android-ndk-r18b --sdk_path ~/android/sdk --extra_modules_path ~/git/opencv_contrib/modules ~/git/opencv/build ~/git/opencv
编译结果在opecv/build目录下。
五、编译遇到的报错
1) 找不到ninja
解决方法:sudo apt-get install ninja-build
2) 找不到boostdesc
解决方法:sudo apt-get install libboost-all-dev
3) 找到boostdesc_bgm.i
解决方法:编译OpenCV提示opencv_contrib缺少boostdesc_bgm.i等文件 - wenglabs - 博客园
4) JAVA_HOME is set to an invalid directory
解决方法:将JAVA_HOME设为:/usr/lib/jvm/java-11-openjdk-amd64