How to use opencv_xfeature2d in opencv 3.1 in Android studio?

asked 2017-03-21 05:08:11 -0600

Some One gravatar image

Based on this solution i was able to build the xfeature2d module of opencv 3.1. And now i want to use the xfeature2d in android NDK so added the libopencv_java3.so and libnonfree.so into the jniLibs folder of android studio. In many question like this and this they suggest "don't forget to link opencv_xfeatures2d" How do you link opencv_xfeature2d? I used cmake build and here is the cmake file

cmake_minimum_required(VERSION 3.4.1)
    add_library(native-lib
               SHARED
               src/main/cpp/native-lib.cpp )
    add_library( # Sets the name of the library
             nonfree
             # Sets the library as shared library.
             SHARED
             # indicate the library is prebuilt.
             IMPORTED )
    set_target_properties( # Specifies the targeted library.
                         nonfree
                       # Specifies the parameter you want to define.
                          PROPERTIES IMPORTED_LOCATION
                        # Specifies the location of the library.
             ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libnonfree.so )
    include_directories(C:/Users/what/Documents/OpenCV-android-  
    sdk/sdk/native/jni/include)
    add_library( lib-opencv SHARED IMPORTED )
    set_target_properties(lib-opencv PROPERTIES IMPORTED_LOCATION    
    ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
    find_library(log-lib
                  log )
    target_link_libraries( native-lib
                       # prebuilt library nonfree.
                       nonfree
                       # prebuilt library opencv java.
                       lib-opencv
                       ${log-lib} )

Then in my MainActivity.java i have

static {
    System.loadLibrary("native-lib");
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
        Log.d(TAG, "static initializer: failed");
    } else {
        Log.d(TAG, "static initializer: successful");
        System.loadLibrary("opencv_java3");
        System.loadLibrary("nonfree");
    }

//to load the opencv and the nonfree prebuilt library. And this line code to call to the native method.

MainActivity.processImage(image.getNativeObjAddr(),finalResult.getNativeObjAddr()); But when i use sift in the .cpp file the application crashes without no error. Here is the native-lib.cpp file

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/xfeatures2d.hpp>
using namespace std;

extern "C"
{
JNIEXPORT void JNICALL
Java_com_the_example_opencv31cmake_MainActivity_processImage(JNIEnv *env, jclass type,
                                                             jlong imagesAddress_,
                                                             jlong resultAddress) {

    cv::Mat input=*(cv::Mat*)imagesAddress_;
     vector<cv::KeyPoint> keypoint;
     cv::Ptr<cv::xfeatures2d::SIFT> sift=cv::xfeatures2d::SIFT::create();
      cv::Mat siftDetectedImage;
      cv::cvtColor(input,siftDetectedImage,CV_BGRA2BGR);
      sift->detect(siftDetectedImage,keypoint);
      cv::Mat imageDescriptor;
      sift->compute(siftDetectedImage,keypoint,imageDescriptor);
      cv::drawKeypoints(siftDetectedImage,keypoint,siftDetectedImage,cv::Scalar(255,0,0),cv::DrawMatchesFlags::DRAW_OVER_OUTIMG);
      cv::Mat image3;
       cv::cvtColor(imageDescriptor,image3,CV_BGR2BGRA);
      cv::Mat &resultImage=*(cv::Mat*)resultAddress;
      resultImage=image3;

}
}
edit retag flag offensive close merge delete