ORB_SLAM2在android studio上用cmake编译

最近公司要我做平面识别,完全没头绪,想到AR上有这种功能就去找,最初找到PTAM,可是代码太久远了,没搞好,然后发现ORB_SLAM2,虽然不知道可不可以实现平面识别,不过把ORB_SLAM2给搬到了android studio上了,分享一下
本来参考 ORB_SLAM2在Android上的移植过程 (Android Studio 2.2+OpenCV 3.2+Cmake) 这篇文章的,可是没成功,就用了笨办法,一个一个库来编译so文件,编译顺序是g2o->DBoW2->ORB_SLAM2生成3个so文件
用的代码在这里 ORB_SLAM2_Android下过来的,不过这个是eclipse项目,开始搞,先下载该项目,下载后新建项目ORB_SLAM2_Android,再创建个module,File->New->New Module选择Android Library名字取g2o,在g2o的build.gradle内添加代码
android {
...
    
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
                abiFilters 'arm64-v8a','armeabi', "armeabi-v7a","x86","x86_64"
            }
        }
    }
    ...
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

}
再把项目ORB_SLAM2_Android\app文件夹下的CMakeLists.txt复制到刚才新建的module内,把项目的ORB_SLAM2_Android\app\src\main\cpp文件夹复制到新建的module内的\src\main文件夹下,然后点中g2o,再点击Build->Make Module 'g2o',这样就可以编译g2o了
同样的顺序再做两遍,分别再创建DBoW2和ORB_SLAM2
现在开始编译g2o了
先把下载来的项目叫做a,把创建的module分别叫g2o,DBoW2和ORB_SLAM2
先把a的jni\Thirdparty文件夹下的g2o文件夹复制到g2o下的g2o\src\main\cpp\Thirdparty文件夹下,再把a的jni\Thirdparty\eigen3文件夹下的Eigen文件夹复制到g2o下的g2o\libs\include文件夹下,再修改g2o的CMakeLists.txt代码
cmake_minimum_required(VERSION 3.4.1)

include_directories(${CMAKE_SOURCE_DIR}/libs/include)

add_library(
             g2o

             SHARED

             src/main/cpp/native-lib.cpp

             src/main/cpp/Thirdparty/g2o/g2o/types/types_sba.cpp
             src/main/cpp/Thirdparty/g2o/g2o/types/types_six_dof_expmap.cpp
             src/main/cpp/Thirdparty/g2o/g2o/types/types_seven_dof_expmap.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/hyper_graph_action.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/hyper_graph.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/marginal_covariance_cholesky.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/matrix_structure.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/batch_stats.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/parameter.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/cache.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/optimizable_graph.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/solver.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/optimization_algorithm_factory.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/estimate_propagator.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/factory.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/sparse_optimizer.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/hyper_dijkstra.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/parameter_container.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/optimization_algorithm.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/optimization_algorithm_with_hessian.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/optimization_algorithm_levenberg.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/jacobian_workspace.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/robust_kernel.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/robust_kernel_factory.cpp
             src/main/cpp/Thirdparty/g2o/g2o/core/robust_kernel_impl.cpp
             src/main/cpp/Thirdparty/g2o/g2o/stuff/timeutil.cpp
             src/main/cpp/Thirdparty/g2o/g2o/stuff/os_specific.c
             src/main/cpp/Thirdparty/g2o/g2o/stuff/string_tools.cpp
             src/main/cpp/Thirdparty/g2o/g2o/stuff/property.cpp
              )


find_library(
              log-lib

              log )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(CMAKE_VERBOSE_MAKEFILE on)
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../app)
set_target_properties(g2o PROPERTIES
                      LIBRARY_OUTPUT_DIRECTORY
                      ${distribution_DIR}/libs/${ANDROID_ABI})


target_link_libraries(
                       g2o

                       ${log-lib} )
编译方法就是点中那个要编译的module然后点击Build->Make Module ''
先编译一下发现错误
Error:error: undefined reference to 'posix_memalign'
查了一下好像是版本问题,修改build.gradle内的minSdkVersion,把它改成16,再编译一下
编译成功的话在项目的app\libs内生成文件夹,打开就可以看到so文件了
接下来编译DBoW2了,DBoW2要opencv的支持,我前面的文章有配置opencv
android studio cmake配置opencv3.2.0
同样,将a\jni\Thirdparty下的DBoW2文件夹复制到DBoW2下的dbow2\src\main\cpp\Thirdparty文件夹下
进入DBoW2文件夹,将src文件夹重命名成DBoW2,再进入DLib\include文件夹,把DUtils文件夹复制到DLib\src文件夹下,再把DUtils文件夹复制到dbow2\src\main\cpp\DBoW2文件夹下,最后dbow2\src\main\cpp\DBoW2文件夹内是这样的

修改DBoW2的CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(libs_DIR ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI})


include_directories(${CMAKE_SOURCE_DIR}/libs/include)

add_library(OpenCV_java3 SHARED IMPORTED )
set_target_properties(  OpenCV_java3
                        PROPERTIES IMPORTED_LOCATION
                        ${libs_DIR}/libopencv_java3.so
                        )


add_library(
             DBoW2

             SHARED
             src/main/cpp/native-lib.cpp
             src/main/cpp/Thirdparty/DBoW2/DBoW2/BowVector.cpp
             src/main/cpp/Thirdparty/DBoW2/DBoW2/FORB.cpp
             src/main/cpp/Thirdparty/DBoW2/DBoW2/FeatureVector.cpp
             src/main/cpp/Thirdparty/DBoW2/DBoW2/ScoringObject.cpp
             src/main/cpp/Thirdparty/DBoW2/DUtils/Random.cpp
             src/main/cpp/Thirdparty/DBoW2/DUtils/Timestamp.cpp
              )



find_library(
              log-lib

              log )
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../app)
set_target_properties(DBoW2 PROPERTIES
                      LIBRARY_OUTPUT_DIRECTORY
                      ${distribution_DIR}/libs/${ANDROID_ABI})

target_link_libraries(
                       DBoW2
                       OpenCV_java3

                       ${log-lib} )

编译一下,报错

Error:(16, 10) fatal error: 'stdint-gcc.h' file not found
stdint-gcc.h这个文件我不知道是什么,所以就改成了stdint.h
#include 
编译一下,又报错
Error:(34, 10) fatal error: 'DUtils/Random.h' file not found
#include 
改成
#include "../DUtils/Random.h"
编译,没问题,主项目app\libs下生成了so文件
最后是ORB_SLAM2了
首先要在orb_slam2\libs文件夹下新建include文件夹,把g2o\libs\include文件夹下的Eigen复制到orb_slam2\libs\include文件夹,把g2o\src\main\cpp\Thirdparty\g2o文件夹内的.h和.hpp文件给挑出来给放到orb_slam2\libs\include文件夹下
dbow2\src\main\cpp\Thirdparty\DBoW2文件夹的.h和.hpp文件也移到orb_slam2\libs\include

ORB_SLAM2在android studio上用cmake编译_第1张图片
thirdparty文件夹下




把刚才生成的so文件拉到orb_slam2\libs内
把a\jni下的ORB_SLAM2文件夹复制到orb_slam2\src\main\cpp,把ORB_SLAM2\src内的文件都复制到orb_slam2\src\main\cpp\ORB_SLAM2,删除src和include文件夹
ORB_SLAM2也要集成opencv,贴ORB_SLAM2的CMakeLists.txt代码
cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(libs_DIR ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI})


include_directories( ${CMAKE_SOURCE_DIR}/libs/include )

add_library(OpenCV_java3 SHARED IMPORTED )
set_target_properties(  OpenCV_java3
                        PROPERTIES IMPORTED_LOCATION
                        ${libs_DIR}/libopencv_java3.so
                        )
add_library(g2o SHARED IMPORTED )
set_target_properties(  g2o
                        PROPERTIES IMPORTED_LOCATION
                        ${libs_DIR}/libg2o.so
                        )
add_library(DBoW2 SHARED IMPORTED )
set_target_properties(  DBoW2
                        PROPERTIES IMPORTED_LOCATION
                        ${libs_DIR}/libDBoW2.so
                        )


add_library(
             ORB_SLAM2

             SHARED

             src/main/cpp/native-lib.cpp
             src/main/cpp/ORB_SLAM2/System.cc
             src/main/cpp/ORB_SLAM2/Tracking.cc
             src/main/cpp/ORB_SLAM2/LocalMapping.cc
             src/main/cpp/ORB_SLAM2/LoopClosing.cc
             src/main/cpp/ORB_SLAM2/ORBextractor.cc
             src/main/cpp/ORB_SLAM2/ORBmatcher.cc
             src/main/cpp/ORB_SLAM2/FrameDrawer.cc
             src/main/cpp/ORB_SLAM2/Converter.cc
             src/main/cpp/ORB_SLAM2/MapPoint.cc
             src/main/cpp/ORB_SLAM2/KeyFrame.cc
             src/main/cpp/ORB_SLAM2/Map.cc
             src/main/cpp/ORB_SLAM2/Optimizer.cc
             src/main/cpp/ORB_SLAM2/PnPsolver.cc
             src/main/cpp/ORB_SLAM2/Frame.cc
             src/main/cpp/ORB_SLAM2/KeyFrameDatabase.cc
             src/main/cpp/ORB_SLAM2/Sim3Solver.cc
             src/main/cpp/ORB_SLAM2/Initializer.cc
             src/main/cpp/ORB_SLAM2/MapDrawer.cc
             src/main/cpp/ORB_SLAM2/Viewer.cc
              )


find_library(
              log-lib

              log )

set(distribution_DIR ${CMAKE_SOURCE_DIR}/../app)
set_target_properties(ORB_SLAM2 PROPERTIES
                      LIBRARY_OUTPUT_DIRECTORY
                      ${distribution_DIR}/libs/${ANDROID_ABI})
target_link_libraries(
                       ORB_SLAM2
                       OpenCV_java3
                       g2o
                       DBoW2

                       GLESv1_CM
                       EGL
                       android
                       ${log-lib} )

编译一下,报错

Error:error: static_assert failed "YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY"
这里解决 关于ORB_SLAM2在Ubuntu16.04下编译出错的解决办法之一
将linear_solver_eigen.h内的

typedef Eigen::PermutationMatrix, Eigen::Dynamic, SparseMatrix::Index> PermutationMatrix;

改成
typedef Eigen::PermutationMatrix, Eigen::Dynamic, int > PermutationMatrix;
Error:(30, 9) fatal error: 'stdint-gcc.h' file not found
这个错误上边有了,同样处理
再编译一下
ok没问题,在ORB_SLAM2_Android\app\libs下生成了so文件
ORB_SLAM2在android studio上用cmake编译_第2张图片

打开一个
ORB_SLAM2在android studio上用cmake编译_第3张图片
好了,可以用那个下过来的例子试试可用否

2018/2/26更新
把下载来的ORB_SLAM2_Android内的代码和资源复制到项目
运行效果

ORB_SLAM2在android studio上用cmake编译_第4张图片

.so文件下载
百度云

密码:xn9w
我的项目源码
百度云
密码:k4lk


你可能感兴趣的:(android)