OpenCV - 在NDK更新后未定义对' cv :: CascadeClassifier :: detectMultiScale()的引用

时间:2018-05-13 11:02:07

标签: android c++ android-studio opencv android-ndk

昨天我将包含NDK的Android Studio更新为版本17.0.4754217,从那时起我就再也无法运行我的应用了。当我尝试在更新后重新运行代码时,它给了我错误ABIs [mips64, armeabi, mips] are not supported for platform. Supported ABIs are [armeabi-v7a, arm64-v8a, x86, x86_64],因此我通过以下方式将其排除在app.gradle文件中的项目中:abiFilters 'x86', 'x86_64', /*'armeabi',*/ 'armeabi-v7a', 'arm64-v8a'/*, 'mips', 'mips64'*/

然而,从那以后我遇到了使用OpenCV函数cv::CascadeClassifier::detectMultiScale的C ++文件的问题。

始终显示错误:CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function detectAndDisplay(cv::Mat, double, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, cv::CascadeClassifier&)': D:\Schule\OpenCV\ARcpp\app\src\main\cpp/native-lib.cpp:158: undefined reference to cv::CascadeClassifier::detectMultiScale(cv::_InputArray const&, std::__ndk1::vector<cv::Rect_<int>, std::__ndk1::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)'.

我将这个函数称为:cascadeClassifier.detectMultiScale(frame_gray, sights, scaleFactor, minNeighbours, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));。其余代码非常类似于OpenCV-tutorial https://docs.opencv.org/2.4/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

除NDK外,我使用CMake和LLDB,我的OpenCV库是openCVLibrary320。同样,所有这一切都有效,直到我下载了上述NDK更新。

构建或执行应用程序时始终出现的其余错误是:Build command failed. Error while executing process C:\Users\chris\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\Schule\OpenCV\ARcpp\app\.externalNativeBuild\cmake\debug\arm64-v8a --target native-lib} [1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so FAILED: cmd.exe /C "cd . && C:\Users\chris\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android --gcc-toolchain=C:/Users/chris/AppData/Local/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/chris/AppData/Local/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem C:/Users/chris/AppData/Local/Android/Sdk/ndk-bundle/sysroot/usr/include/aarch64-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 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot C:/Users/chris/AppData/Local/Android/Sdk/ndk-bundle/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -LC:/Users/chris/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog ../../../../src/main/jniLibs/arm64-v8a/libopencv_java3.so -latomic -lm "C:/Users/chris/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_static.a" "C:/Users/chris/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++abi.a" && cd ." clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.

我真的输了,希望有人知道答案。

5 个答案:

答案 0 :(得分:4)

OpenCV使用ANDROID_STL=gnustl_static构建。升级后,NDK使用默认的 libc ++ 。您可以在 app / build.gradle 中明确设置ANDROID_STL:

function playthis

(请参阅示例here

答案 1 :(得分:1)

将opencv链接到项目可执行文件时,您始终必须链接到常规库 -lopencv_core 。但是一些包需要额外的链接标志。例如, 如果你在

中使用highgui
#include <opencv2/highgui/highgui.hpp>

您必须为: -lopencv_highgui 添加opencv_highgui链接标记。

在您的情况下,CascadeClassifiers在

中定义
#include "opencv2/objdetect.hpp"

因此需要与opencv_objdetect -lopencv_objdetect 的链接。

解决方案是在编译时添加链接标志 -lopencv_objdetect

答案 2 :(得分:1)

为所有寻求者提供更完整的答案。在项目中使用OpenCV预构建的 静态库 和本机组件。

这就是我对#include <boost/filesystem.hpp> void create_data_file(std::string file_path) { boost::filesystem::path p(file_path); boost::filesystem::create_directory(p); }

的看法
build.gradle

这是CMakeLists.txt:

defaultConfig {
    minSdkVersion 21
    targetSdkVersion 21
    externalNativeBuild {
        cmake {
            cppFlags "-fexceptions -std=gnu++11"
            arguments '-DANDROID_STL=gnustl_static'
        }
    }
    ndk {
        // Specifies the ABI configurations of your native
        // libraries Gradle should build and package with your APK.
        abiFilters 'armeabi-v7a', 'x86'
        stl = "gnustl_shared"
    }
}

(然后使用set(OPENCV_LIBRARIES ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libopencv_video.a ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libopencv_imgproc.a ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libopencv_core.a ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libopencv_highgui.a ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libtbb.a ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libcpufeatures.a ) if (${ANDROID_ABI} STREQUAL "x86") set(OPENCV_LIBRARIES ${OPENCV_LIBRARIES} ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libippiw.a ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libippicv.a ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libittnotify.a ) set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a") endif() if (${ANDROID_ABI} STREQUAL "armeabi-v7a") set(OPENCV_LIBRARIES ${OPENCV_LIBRARIES} ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_NDK_ABI_NAME}/libtegra_hal.a ) endif() 作为${OPENCV_LIBRARIES}

答案 3 :(得分:0)

对此link的第一条评论可以为您节省很多时间,我在下面复制了这些内容:

“我遇到了类似的问题。结果证明,您必须使用与用于构建OpenCV Android SDK的Android NDK类似的Android NDK,在版本〜3.4的情况下,它是Android NDK10。最近是OpenCV Android SDK 4.0。 1已发布,并且可以与最新的NDK版本(当时为19)一起使用。”

在撰写本文时,您可以找到版本4.1.2。您可以检查此link上是否有最新版本。

在选择最新版本(在本例中为4.1.2)之后,下载文件opencv-4.1.2-android-sdk.zip,解压缩并将sdk / native文件夹复制到yourprojectfolder / your-app / src / sdk /。

您已经准备好开始滚动。

答案 4 :(得分:0)

我通过与this OpenCV + contrib lib(opencv4_1_0_contrib)和official Java OpenCV类(4.3)混合使用解决了这个问题。在Android 9上成功运行官方OpenCV FaceDetection示例。


一些有用的代码:

应用程序级别build.gradle:

android{defaultConfig{
//...
externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
                abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
                //I import OpenCV as module and provide it path to CMake
                arguments "-DOpenCV_DIR=/" + rootProject.projectDir.path + "/sdk/native"
             }
        }
}}

externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }

我的CMakeLists.txt:

# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)

#Add OpenCV 4 lib
include_directories(${OpenCV_DIR}/jni/include) #Path from gradle to OpenCV Cmake
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${OpenCV_DIR}/libs/${ANDROID_ABI}/libopencv_java4.so)

#Add Your Native Lib
add_library(native-lib SHARED native-lib.cpp)
add_library(detection_based_tracker SHARED detectionbasedtracker_jni.cpp)

#Add&Link Android Native Log lib with others libs
find_library(log-lib log)
target_link_libraries(detection_based_tracker lib_opencv ${log-lib})

NDK版本为21.0.611669。 另外,我不使用-DANDROID_STL=gnustl_static这样的CMake参数。