微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将Android上的错误与OpenCV 3.4.0和NDK相关联

我正在尝试将使用OpenCV用C编写的现有计算机视觉代码移植到Android NDK.我按照此处提供的信息成功地为Java和NDK导入了OpenCV库版本3.4.0(使用官方预构建的Android软件包):Satck Overflow Answer – CMake configuration of OpenCV on Android.

我能够使用Java和C中的OpenCV功能编译和运行一些代码.但是,我遇到了与某些OpenCV函数相关的2个“未定义引用”链接错误:持久性JSON读取器和2D描述符匹配器.

以下是我收到的错误消息:

Build command Failed.
Error while executing process D:\Librairies\Android_SDK\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\Dev\Android\PageDetector\app\.externalNativeBuild\cmake\debug\x86_64 --target page-recognition-lib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\recognition-lib.so
Failed: cmd.exe /C "cd . && D:\Librairies\Android_SDK\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=x86_64-none-linux-android --gcc-toolchain=D:/Librairies/Android_SDK/ndk-bundle/toolchains/x86_64-4.9/prebuilt/windows-x86_64 --sysroot=D:/Librairies/Android_SDK/ndk-bundle/sysroot -fPIC -isystem D:/Librairies/Android_SDK/ndk-bundle/sysroot/usr/include/x86_64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -std=c++11 -frtti -fexceptions -std=gnu++11 -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot D:/Librairies/Android_SDK/ndk-bundle/platforms/android-21/arch-x86_64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -LD:/Librairies/Android_SDK/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86_64 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,Now -shared -Wl,-soname,libpage-recognition-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\recognition-lib.so.so [...] -llog -llog ../../../../src/main/jniLibs/x86_64/libopencv_java3.so -latomic -lm "D:/Librairies/Android_SDK/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86_64/libc++_static.a" "D:/Librairies/Android_SDK/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86_64/libc++abi.a" && cd ."
D:/Librairies/OpenCV-android-sdk_340/sdk/native/jni/include\opencv2/core/persistence.hpp:1264: error: undefined reference to 'cv::read(cv::FileNode const&, std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> >&)'
D:\Dev\Android\PageDetector\app\src\main\cpp/PageMatcher.cpp:170: error: undefined reference to 'cv::DescriptorMatcher::radiusMatch(cv::_InputArray const&, cv::_InputArray const&, std::__ndk1::vector<std::__ndk1::vector<cv::DMatch, std::__ndk1::allocator<cv::DMatch> >, std::__ndk1::allocator<std::__ndk1::vector<cv::DMatch, std::__ndk1::allocator<cv::DMatch> > > >&, float, cv::_InputArray const&, bool) const'
clang++.exe: error: linker command Failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand Failed.

以下是编译器无法链接代码段:

//code that reads data from a JSON file
this->JSONFeatureFilePath = JSONFeatureFilePath;
cv::FileStorage reader(JSONFeatureFilePath, cv::FileStorage::READ);

this->bookTitle = (string) reader["book_title"];
this->pageNumber = (int) reader["page_num"];

string descType = (string)reader["desc_type"];
replace(descType.begin(), descType.end(), '_', '.');
this->descriptorType = descType;

reader["img_size"] >> this->imageSize;

//this instruction causes the linker error
reader["keypoints"] >> this->keyPoints;

reader["descriptors"] >> this->keyPointDescriptors;
reader["fsum2d"] >> this->fsum2DFeatureSummary;

reader.release();

//code performing key point descriptors matching
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::BRUTEFORCE_HAMMING);

vector<vector<cv::DMatch>> matchesTmp;

//instruction responsible for the link error
matcher->radiusMatch(this->sortedMatches.at(refSortedindex).refImage->getDescriptors(),
    this->testimage->getDescriptors(), matchesTmp, matchThreshold);

我已经清楚地确定了导致链接错误的行,如上面的代码示例中所述.如果我将它们评论出来,编译就会完成并且程序运行正常(当然没有我试图在NDK中实现的功能).

我的猜测是我调用的OpenCV函数在预构建的库中缺失,或者与我用于NDK开发的编译器不兼容.我已经尝试更改OpenCV版本(3.3.0和3.4.0).

有谁知道是什么原因造成这种情况以及如何解决这个问题?它是OpenCV中的已知错误还是我的配置不受支持或只是错误

我正在使用Windows 10计算机与Android Studio 3.1.2,NDK r17,构建工具27.0.3和OpenCV 3.4.0预构建的android包(我没有自己从源代码编译).以下是CMake和build.gradle文件

CMake的:

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(opencv_340_dir D:/Librairies/OpenCV-android-sdk_340/sdk/native/jni)
set(app_dir D:/Dev/Android/PageDetector/app)

# native recognition library API
add_library(recognition-lib
        SHARED
        src/main/cpp/recognition-lib.h
        src/main/cpp/recognition-lib.cpp
        # + my classes' h and cpp files
        )

# OpenCV lib linking and includes
include_directories(${opencv_340_dir}/include)
add_library(opencv-lib SHARED IMPORTED)
set_target_properties(opencv-lib PROPERTIES IMPORTED_LOCATION ${app_dir}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)


find_library(log-lib log)

target_link_libraries(
                   recognition-lib
                   opencv-lib
                   ${log-lib}
                   )

target_link_libraries(recognition-lib ${log-lib})

gradle这个:

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.companyname.detector"
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
        cmake {
            cppFlags "-std=c++11 -frtti -fexceptions"
        }
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}
sourceSets {
    main {
        jniLibs.srcDirs = ['src/main/jniLibs/']
    }
}
}

dependencies {
implementation filetree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':openCVLibrary340')
}

解决方法:

最近,NDK切换到libc作为认STL,但OpenCV是用gnustl构建的.

externalNativeBuild {
  cmake {
    arguments "-DANDROID_STL=gnustl_shared"
  }
}

为您的图书馆将解决这个问题.

或者,你可以rebuild OpenCV with c++_shared.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐