opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
78.39k stars 55.74k forks source link

Android OpenCV 3.4.1 - Cannot call stitching methods in JNI C++ methods #11595

Open KholofeloMoyaba opened 6 years ago

KholofeloMoyaba commented 6 years ago
System information (version)
Detailed description

Android OpenCV 3.4.1 libopencv_java3.so doesn't have stitching/stitcher support/symbols compiled in.

More specifically, calling the Stitcher class makes the project not to compile as references to cv::Stitcher cannot be found.

Steps to reproduce

This is my JNI

JNIEXPORT void JNICALL Java_za_co_palota_opencvtest_MainActivity_incrementalStitch(JNIEnv *env, jobject instance, jlong stitchingFrameAddr, jlong additionalFrameAddr) {

        Mat &srcM = *(Mat *) stitchingFrameAddr;
        Mat &dstM = *(Mat *) additionalFrameAddr;

        vector<Mat> frames;

        frames.push_back(srcM);
        frames.push_back(dstM);

        Mat panorama;

        Ptr<Stitcher> stitcher = Stitcher::create(mode, try_use_gpu);
        Stitcher::Status status = stitcher->stitch(frames, panorama);

        if (status != Stitcher::OK)
        {
            LOGD("CV: STITCHING FAILED");
        } else {
            LOGD("CV: STITCHING SUCCESSFUL");
        }

    }

When building my android app I get these errors

/Users/.../app/src/main/cpp/native-lib.cpp:61: error: undefined reference to 'cv::Stitcher::create(cv::Stitcher::Mode, bool)'
/Users/.../app/src/main/cpp/native-lib.cpp:62: error: undefined reference to 'cv::Stitcher::stitch(cv::_InputArray const&, cv::_OutputArray const&)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Upon further I looked at the symbols inside libopencv_java3.so (libopencv_java3.so comes straight from the latest package served from SourceForge - https://sourceforge.net/projects/opencvlibrary/files/opencv-android/3.4.1/) by running the following command:

nm -D libopencv_java3.so

When looking at the output I couldn't see references to any of the stitching methods.

alalek commented 6 years ago

"stitching" module is out of scope libopencv_java because Java bindings (Android or desktop) are disabled for stitching module (missing "WRAP Java" entry for some reason): https://github.com/opencv/opencv/blob/3.4.1/modules/stitching/CMakeLists.txt#L13

KholofeloMoyaba commented 6 years ago

@alalek thanks for the speedy response. Is there anyway to manually include the stitching library myself? E.g. using the staticlibs included in the package?

Or are my only options to either downgrade or implement stitching from scratch?

KholofeloMoyaba commented 6 years ago

Hi @alalek So I've been trying to implement the stitching algorithms myself and I'm having issues with DescriptorMatcher as well.

I can't call

DescriptorMatcher::match

or

DescriptorMatcher::knnMatch

Please see error below:

/Users/.../src/main/cpp/native-lib.cpp:62: undefined reference to `cv::DescriptorMatcher::match(cv::_InputArray const&, cv::_InputArray const&, std::__ndk1::vector<cv::DMatch, std::__ndk1::allocator<cv::DMatch> >&, cv::_InputArray const&) const'
victorv commented 6 years ago

I am also seeing this problem in 3.4.3 where we are unable to link from Android Studio (or command line) from OpenCV built with clang and embedded cmake file (see https://github.com/10imaging/opencv/tree/v3.4.3)

"/Users/XXX/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld" --sysroot=/Users/XXX/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64 --fix-cortex-a53-843419 --eh-frame-hdr -m aarch64linux -shared -o /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/libiris_native.so /Users/XXX/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64/usr/lib/crtbegin_so.o -L/Users/XXX/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64/usr/lib -L/Users/XXX/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/6.0.2/lib/linux/aarch64 -L/Users/XXX/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/lib/gcc/aarch64-linux-android/4.9.x -L/Users/XXX/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/lib/../lib64 -L/Users/XXX/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/lib -L/Users/XXX/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64/usr/lib -soname libiris_native.so /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/zzz_native/exif_jni.o /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/zzz_native/exif.o /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/zzz_native/PanoStitch_jni.o /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/zzz_native/PerfTracker_jni.o /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/zzz_native/DetectionBasedTracker_jni.o /Users/XXX/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_static.a /Users/XXX/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++abi/../llvm-libc++/libs/arm64-v8a/libc++abi.a -lgcc --exclude-libs libgcc.a -latomic --exclude-libs libatomic.a /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/libopencv_java3.so /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/libc++_shared.so /Users/YYY/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/libSNPE.so --build-id --no-undefined -z noexecstack -z relro -z now --warn-shared-textrel --fatal-warnings -llog -ldl -landroid -lc -lm -lm -lgcc -ldl -lc -lgcc -ldl /Users/XXX/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64/usr/lib/crtend_so.o

PanoStitchjni.o: In function `cv::Ptr cv::makePtr<cv::detail::OrbFeaturesFinder, cv::Size, int, float, int>(cv::Size const&, int const&, float const&, int const&)': /Users/XXX/src/main/jni/../../../opencv/src/main/jni/include/opencv2/core/ptr.inl.hpp:325: undefined reference to `cv::detail::OrbFeaturesFinder::OrbFeaturesFinder(cv::Size, int, float, int)' /Users/XXX/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/zzz_native/PanoStitch_jni.o: In function `Stitch(_JNIEnv, std::ndk1::basic_string<char, std::__ndk1::char_traits, std::ndk1::allocator >, cv::Mat, std::ndk1::vector<std::ndk1::basic_string<char, std::ndk1::char_traits, std::ndk1::allocator >, std::ndk1::allocator<std::__ndk1::basic_string<char, std::ndk1::char_traits, std::_ndk1::allocator > > >, int, int, int*, cv::Rect*)':

victorv commented 6 years ago

I fixed in our tree by adding java to wrappers -

set(the_description "Images stitching")

if(HAVE_CUDA)
  ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wmissing-declarations -Wshadow)
endif()

set(STITCHING_CONTRIB_DEPS "opencv_xfeatures2d")
if(BUILD_SHARED_LIBS AND BUILD_opencv_world AND OPENCV_WORLD_EXCLUDE_EXTRA_MODULES)
  set(STITCHING_CONTRIB_DEPS "")
endif()
ocv_define_module(stitching opencv_imgproc opencv_features2d opencv_calib3d opencv_flann opencv_objdetect
                  OPTIONAL opencv_cudaarithm opencv_cudawarping opencv_cudafeatures2d opencv_cudalegacy opencv_cudaimgproc ${STITCHING_CONTRIB_DEPS}
                  WRAP python java)
shawn817 commented 4 years ago

It can not be completely fixed by adding java to wrappers in opencv-3.4.x/modules/stitching/CMakelist.txt, since the script to generate Java wrapper for stitching module is missing in the source code (typically in $opencv-3.4.x/modules/$module_name/misc/java). E.g. feature2d module supports Java wrapper because it has generate script under opencv-3.4.x/modules/features2d/misc/java