Integrated OPENCV Environment in Android Studio (includes Opencv_contrib section)

Source: Internet
Author: User

I said in my last blog that integrating OpenCV in Android, but that version of OPENCV is not sift and surf algorithms, because these algorithms are patented and are not included in the precompiled library, so if you want to use the SIFT and surf algorithms, Need to compile OpenCV Android SDK yourself. In the OpenCV 2.4.x version, these algorithms are included in the nonfree module; starting with version 3.0, some algorithms for image feature matching (such as sift,surf,brief,freak, etc.) were transferred to the OPENCV_ Contrib project in the Xfeatures2d module.

We need to get down to the Opencv_contrib section from GitHub, compile it, GitHub address: Https://github.com/opencv/opencv_contrib, note that you need to download the OpenCV For the Android library. With regard to the compilation of the Opencv_contrib library, I compiled it successfully in Windows and Linux (which I will mention later in the blog), but the compilation of the Android platform encountered me with great difficulty. I Baidu for a long time, found a relatively reliable blog: http://johnhany.net/2016/07/build-opencv-manager-for-android-on-ubuntu/, I follow the author said step by step to compile, Did not succeed in the end. Fortunately, the author at the back of the article gave its successful compilation of the OpenCV Android SDK, is the OpenCV3.2 version, here I also give the link: https://pan.baidu.com/s/1kVOejLt, thank the author again. But unfortunately, when the author originally compiled only unlocked sift, surf and freak, and did not unlock brief, may not meet the requirements of all, here I also hope that if there is a great God compiled successfully, will be successful after the library sent me a copy.

Here, we already have the Android OpenCV SDK compiled with the opencv_contrib part, the directory structure is as follows:

    

Because we need to use tools such as CMAKE,NDK, we need to pre-open the SDK Manager for management (it is recommended to install the NDK provided in the SDK manager for easy management),

    

    

Then we create a new project in Android Studio, and when we create it, we check the include C + + support, and then the next step.

    

After entering the project, we found that the directory structure of the project has changed a little, the main directory has a CPP directory, and a cmakelist file (Android Studio2, we can call C + + through the CMake management NDK, Without having to pass the Android.mk file, this is undoubtedly a boon).

    

Then we refer to the operation of my previous blog, click File->new->import Module Add Library, click File->project Structure Add dependency, Modify the parameters in the Opencvlibrary build.gradle file to be the same as in the app's Build.gradle file.

We want to copy all the files in the compiled library sdk->native->libs to the main directory of the project, and rename it as: Jnilibs

The library that will be compiled sources->opencv_contrib->modules->xfeatures2d-> Freak.cpp,precomp.hpp,sift.cpp,surf.cpp,surf.hpp,xfeatures2d_init.cpp a total of 6 files in the SRC directory to app->src->main-> CPP folder. Where the precomp.hpp file needs to be modified as follows:

Comment out the 第52-53 line:

1 #include "opencv2/core/private.hpp" 2 #include "opencv2/core/private.cuda.hpp"

Comment out the 62nd line

#include "opencv2/core/private.hpp"

Then we need to modify the CmakeList.txt file to:

1 cmake_minimum_required (VERSION 3.4.1)2 3 set (Cmake_verbose_makefile on)4 set (ocvlibs "${cmake_source_dir}/src/main/jnilibs")5 include_directories (${cmake_source_dir}/src/main/cpp/include)6 7 add_library (libopencv_java3 SHARED imported)8 set_target_properties (LIBOPENCV_JAVA3 properties9 imported_location "${ocvlibs}/${android_abi}/libopencv_java3.so")Ten  One Add_library (# Sets the name of the library. A xfeatures2d -   - # Sets the library as a shared library. the SHARED -   - # provides a relative path to your source file (s). - Src/main/cpp/xfeatures2d_init.cpp + Src/main/cpp/sift.cpp - Src/main/cpp/surf.cpp + src/main/cpp/freak.cpp) A  at Find_library (# Sets the name of the path variable. - Log-lib -  - # Specifies the name of the NDK library that - # you want CMake to locate. - log) in  - target_link_libraries (# Specifies the target library. to xfeatures2d Android Log libopencv_java3 +   - # Links The target library to the log library the # included in the NDK. *${log-lib})

Finally, we need to modify the App/build.gradle file to:

1 Apply plugin: ' Com.android.application '2 3 Android {4 compilesdkversion5 buildtoolsversion "27.0.1"6 Defaultconfig {7 ApplicationID "Com.example.demo02"8 minsdkversion9 targetsdkversionTen Versioncode 1 One Versionname "1.0" A testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner" - Externalnativebuild { - CMake { the cppflags "-std=c++11", "-frtti", "-fexceptions" - abifilters ' x86 ', ' x86_64 ', ' Armeabi ', ' armeabi-v7a ', ' arm64-v8a ', ' MIPS ', ' MIPS64 ' -             } -         } +     } - Sourcesets { + Main { A jnilibs.srcdirs = [' Src/main/jnilibs '] at         } -     } - Buildtypes { - Release { - minifyenabled false - proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro ' in         } -     } to Externalnativebuild { + CMake { - path "CMakeLists.txt" the         } *     } $ }Panax Notoginseng  - Dependencies { the compile ' com.android.support:design:25.3.1 ' + compile Filetree (include: [' *.jar '], dir: ' Libs ') A androidtestcompile (' com.android.support.test.espresso:espresso-core:2.2.2 ', { the exclude group: ' Com.android.support ', module: ' Support-annotations ' +     }) - compile ' com.android.support:appcompat-v7:25.3.1 ' $ testcompile ' junit:junit:4.12 ' $ Compile Project (': openCVLibrary320 ') -}

So far, we can use the SIFT and surf algorithms in Android, the code, the Www.baidu.com ~ ~ ~

  

Integrated OPENCV Environment in Android Studio (includes Opencv_contrib section)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.