ORB_SLAM2 compiled with cmake on android studio

Recently, the company asked me to do plane recognition, but I had no clue. I thought that there was such a function in AR. I found PTAM at first, but the code was too long and I didn’t get it right. Then I found ORB_SLAM2, although I don’t know if plane recognition can be realized. But moved ORB_SLAM2 to android studio, share
Originally, I referred to this article on the porting process of ORB_SLAM2 on Android (Android Studio 2.2+OpenCV 3.2+Cmake) , but it was unsuccessful, so I used a stupid method to compile the so files one by one. The compilation order is g2o->DBoW2- >ORB_SLAM2 generates 3 so files
The code used is here from ORB_SLAM2_Android , but this is an eclipse project. To start, download the project first, then create a new project ORB_SLAM2_Android after downloading, and then create a module, File->New->New Module Select the Android Library name and take g2o, Add code in g2o's 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"
        }
    }

}
Then copy the CMakeLists.txt in the ORB_SLAM2_Android\app folder of the project to the newly created module, and copy the ORB_SLAM2_Android\app\src\main\cpp folder of the project to the \src\main folder in the newly created module, Then click on g2o, then click Build->Make Module 'g2o', so that you can compile g2o
Do the same sequence twice, create DBoW2 and ORB_SLAM2 respectively
Now start compiling g2o
First, call the downloaded project a, and call the created modules g2o, DBoW2 and ORB_SLAM2 respectively.
First copy the g2o folder under the jni\Thirdparty folder of a to the g2o\src\main\cpp\Thirdparty folder under g2o, and then copy the Eigen folder under the jni\Thirdparty\eigen3 folder of a to Under the g2o\libs\include folder under g2o, modify the CMakeLists.txt code of g2o
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} )
The compilation method is to click on the module to be compiled and then click Build->Make Module ''
Compile first to find errors
Error:error: undefined reference to 'posix_memalign'
I checked it and it seems to be a version problem, modify the minSdkVersion in build.gradle, change it to 16, and compile it again
If the compilation is successful, a folder will be generated in the app\libs of the project, and you can see the so file when you open it.
Next, compile DBoW2. DBoW2 needs opencv support. My previous article has configured opencv
Similarly, copy the DBoW2 folder under a\jni\Thirdparty to the dbow2\src\main\cpp\Thirdparty folder under DBoW2
进入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 <stdint.h>
编译一下,又报错
Error:(34, 10) fatal error: 'DUtils/Random.h' file not found
Bundle
#include <DUtils/Random.h>
change to
#include "../DUtils/Random.h"
Compile, no problem, the so file is generated under the main project app\libs
The last is ORB_SLAM2
First, create a new include folder in the orb_slam2\libs folder, copy the Eigen in the g2o\libs\include folder to the orb_slam2\libs\include folder, and put the g2o\src\main\cpp\Thirdparty\g2o folder in the folder The .h and .hpp files are picked out and placed in the orb_slam2\libs\include folder
The .h and .hpp files of the dbow2\src\main\cpp\Thirdparty\DBoW2 folder are also moved to orb_slam2\libs\include

under the thirdparty folder




Pull the so file just generated into orb_slam2\libs
Copy the ORB_SLAM2 folder under a\jni to orb_slam2\src\main\cpp, copy the files in ORB_SLAM2\src to orb_slam2\src\main\cpp\ORB_SLAM2, delete the src and include folders
ORB_SLAM2 should also integrate opencv, paste the CMakeLists.txt code of ORB_SLAM2
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"
将linear_solver_eigen.h内的

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

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

打开一个
好了,可以用那个下过来的例子试试可用否

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

.so文件下载
百度云

密码:xn9w








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325676035&siteId=291194637
Recommended