Ubuntu下搭建Android PCL、OPENCV开发环境

搭建步骤

  • PCL
    • 下载 PCL for android
    • Android studio中配置
  • java 、 c++ 混合开发opencv
    • C++部分
      • 1、首先下载 opencv-android-sdk
      • 2、解压
      • 3、修改 CMakeLists.txt 链接 libopencv_java4.so
      • 4、在C++代码中使用opencv
    • JAVA部分
    • Android studio 中配置
      • 1、导入OpenCV module:
      • 2、配置opencv410 的build.gradle
      • 3、将opencv添加为app的 module dependences

PCL

PCL(Point Cloud Library)是在吸收了前人点云相关研究基础上建立起来的大型跨平台开源C++编程库,它实现了大量点云相关的通用算法和高效数据结构,涉及到点云获取、滤波、分割、配准、检索、特征提取、识别、追踪、曲面重建、可视化等。支持多种操作系统平台,可在Windows、Linux、Android、Mac OS X、部分嵌入式实时系统上运行。如果说OpenCV是2D信息获取与处理的结晶,那么PCL就在3D信息获取与处理上具有同等地位,PCL是BSD授权方式,可以免费进行商业和学术应用。

下载 PCL for android

PCL一般需要我们自己编译,过程相当麻烦,这里提供已经编译好的
链接:pcl download

Android studio中配置

将下载好的压缩包解压,里面包含了lib和include头文件。打开android studio,打开CmakeLists,按如下进行配置:

cmake_minimum_required(VERSION 3.4.1)

set(PCL_BUILD "/home/xxx/PCL_android/build")

set(PCL_INCLUDE "${PCL_BUILD}/pcl-android")
set(BOOST_INCLUDE "${PCL_BUILD}/boost-android")
set(FLANN_INCLUDE "${PCL_BUILD}/flann-android")
set(EIGEN_INCLUDE "${PCL_BUILD}/eigen")
set(QHULL_INCLUDE "${PCL_BUILD}/qhull-android")

set(PCL_STATIC_LIB_DIR "${PCL_INCLUDE}/lib")
set(BOOST_STATIC_LIB_DIR "${BOOST_INCLUDE}/lib")
set(FLANN_STATIC_LIB_DIR "${FLANN_INCLUDE}/lib")
set(QHULL_STATIC_LIB_DIR "${QHULL_INCLUDE}/lib")

include_directories(${PCL_INCLUDE}/include)
include_directories(${BOOST_INCLUDE}/include)
include_directories(${FLANN_INCLUDE}/include)
include_directories(${QHULL_INCLUDE}/include)
include_directories(${EIGEN_INCLUDE})

function(importLib dirName libName)
    add_library(${libName} SHARED IMPORTED)
    set_target_properties(${libName}
            PROPERTIES
            IMPORTED_LOCATION
            "${dirName}/${libName}.a")
endfunction(importLib)

set(libFlannNames
        libflann_cpp_s
        libflann_s
        )

set(libBoostNames
        libboost_thread
        libboost_signals
        libboost_program_options
        libboost_filesystem
        libboost_system
        libboost_regex
        libboost_iostreams
        libboost_date_time
        )

set(libQhullNames
        libqhullstatic
        )

set(libPclNames
        libpcl_stereo
        libpcl_2d
        libpcl_segmentation
        libpcl_recognition
        libpcl_ml
        libpcl_io
        libpcl_io_ply
        libpcl_filters
        libpcl_tracking
        libpcl_search
        libpcl_registration
        libpcl_keypoints
        libpcl_features
        libpcl_surface
        libpcl_sample_consensus
        libpcl_octree
        libpcl_kdtree
        libpcl_common
        )

foreach(lib ${libPclNames})
    importLib(${PCL_STATIC_LIB_DIR} ${lib})
endforeach(lib)

foreach(lib ${libFlannNames})
    importLib(${FLANN_STATIC_LIB_DIR} ${lib})
endforeach(lib)

foreach(lib ${libBoostNames})
    importLib(${BOOST_STATIC_LIB_DIR} ${lib})
endforeach(lib)

foreach(lib ${libQhullNames})
    importLib(${QHULL_STATIC_LIB_DIR} ${lib})
endforeach(lib)

add_library(native-jni SHARED
        native-jni.cpp
        )

target_link_libraries(
        native-jni

        ${libPclNames}
        ${libFlannNames}
        ${libBoostNames}
        ${libQhullNames}

        android
        log)

build.gradle中添加编译设置

app--> build.gradle: 
			android {
    					..../
						externalNativeBuild {
							cmake {
								cppFlags "-DBOOST_SYSTEM_NO_DEPRECATED"
								//arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static', '-DCMAKE_BUILD_TYPE=Release'
								abiFilters 'armeabi-v7a','x86', 'arm64-v8a', 'x86_64'
							}
						}
   			 }

OK, 就是这么简单的配置,难点主要是自己编译android版的pcl sdk。有兴趣的朋友可以自己尝试用ndk去编译自己想要的pcl这里不详说了。

java 、 c++ 混合开发opencv

Opencv做图像处理的都不陌生吧,OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。接下来介绍如何在Android上搭建Opencv Java && c++混合开发环境。

C++部分

1、首先下载 opencv-android-sdk

直接去官网下就OK。
链接:opencv download
选择自己想要的版本下载。

2、解压

将下载的压缩包解压,把 Opencv/OpenCV-android-sdk/sdk/native/libs 下的so库文件到项目的Libs下面(没有Libs就自己新建一个,跟src同级目录),
build.gradle中加一段:

    sourceSets{
        main{
            jniLibs.srcDirs = ['libs']
        }
    }

3、修改 CMakeLists.txt 链接 libopencv_java4.so

NDK开发基本使用CmakeLists来构建项目,因此这里只说明CmakeLists的配置,我的配置如下:

# opencv 公共路径
set(pathToOpenCv /home/xxx/Opencv/OpenCV-android-sdk)
# 配置native依赖,引入 opencv 头文件
include_directories(${pathToOpenCv}/sdk/native/jni/include)
add_library(
        opencv_java4

        SHARED

        IMPORTED)
set_target_properties(

        opencv_java4

        PROPERTIES

        IMPORTED_LOCATION

        ../../../../libs/${ANDROID_ABI}/libopencv_java4.so
)

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        #native-lib.cpp
        fusionmeasure.cpp
        )

find_library( # Sets the name of the path variable.
        log-lib
        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)
        
target_link_libraries( # Specifies the target library.
        native-lib
        opencv_java4
        # Links the target library to the log library
        # included in the NDK.
        android
        GLESv3
        ${log-lib})

4、在C++代码中使用opencv

#include 
...
cv::Mat distanceImage;
distanceImage.convertTo(distanceImage, CV_32F);
...

上面的配置顺利的话。纯C++开发已经足够,但是opencv还提供了java api,所以接下来再介绍java开发opencv如何配置。

JAVA部分

opencv也提供了java层的api,打开我们解压好的文件夹,会看到有 java 文件夹(OpenCV-android-sdk/sdk/java)。接下来进行配置。

Android studio 中配置

1、导入OpenCV module:

–> 菜单栏选择File
–> new
–> imported module
–> 选择Opencv/OpenCV-android-sdk/sdk/java
–> module名字随便取,这里假定为:opencv410

2、配置opencv410 的build.gradle

直接复制一份项目的 build.gradle(app),去掉ndk编译的配置,这里举例我的配置,注意里面有两个地方要做修改,一个是第一行的 “apply plugin: ‘com.android.application’” 变为 **apply plugin: ‘com.android.library’ **, 还有就是删除 applicationId

// 这里要修改为 com.android.library
apply plugin: 'com.android.library' 

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
   	 	//applicationId "com.example.myopencvprj" 删除
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                //cppFlags ""
                //abiFilters 'armeabi-v7a', 'arm64-v8a'
            }
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets{
        main{
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:design:28.1.1'
    implementation 'com.github.chrisbanes.photoview:library:1.2.4'
    implementation 'com.afollestad.material-dialogs:core:0.9.1.0'
    implementation 'com.afollestad.material-dialogs:commons:0.9.1.0'
    testImplementation 'junit:junit:4.12'
}

3、将opencv添加为app的 module dependences

build.gradle中添加如下:
implementation project(path: ‘:openCV410’).
OK 大功告成。

你可能感兴趣的:(图像处理,ndk,android,opencv)