Recently READ some opencV related content, here to make a record and learning experience, and share with you, I am also online when learning a lot of people’s blog.
Now is the open source era, OpencV is also an open source library, but also has the official website and community, so introduce what we go to the official website to see, understand what is OpencV, used for what, here will not be worded.
1, download
Download address of each version,
You can download the corresponding resources according to your work platform, I really use OpencV in the field of Android, so the download is 3.4.5 Android Pack, the content behind is also aimed at the use of OpencV in Android development.
2. SDK structure
The structure of the decompressed file is as follows
- Opencv-manager: OpencV-Manager: OpencV-Manager: OpencV-Manager: OpencV-Manager
- Samples: Samples are several official OpencV demo projects, including project source code and packaged APK.
- SDK: This is the key, the future development is also used here.
- Etc: identification of related cascade classifiers and so on, I do not know much about them at present, and I will not use them in early learning.
- Java: This is opencV official provision of an OpencV Android library project, provides a complete opencV ability, because OpencV bottom is written in C/C ++, but now many programming languages, Java, Python and so on, so the official for different language platforms, the bottom library for the second package, When used, the project will be directly imported as a library, which will be detailed later.
- Native: Some libraries for the Native layer
- 3rdParty: Third-party libraries
- Jni: Headers for some cmake compilation scripts and dynamic libraries
- Libs: officially made according to different platform architecture. So dynamic library, providing complete OpencV capability, slightly larger, the size of a single architecture corresponding. So file is more than 10M
- Staticlibs: make different functions into. A static library, you can choose to load the corresponding. A static library according to the opencV capability used, which is conducive to reducing the application volume.
3. Development mechanics
Opencv bottom is C/C ++ written, the language threshold is high, in addition to the mathematics, graphics and other knowledge also increased the difficulty of learning, so the official for different levels and application direction of personnel to provide different development methods, in order to benefit the vast number of OpencV ability requirements.
1. Do you want to configure the SDK native library in the project?
Before using OpencV capabilities, native libraries provided by OpencV need to be loaded. Opencv provides two ways:
- To load from the local application package, libraries and header files of the relevant Native layer are required to be copied to the project and configured. Then the application will be loaded from the local application according to the configuration.
- Opencv-manager is an APK installation package installed in advance on the device, which provides OpencV capability to other applications in the way of AIDL. Therefore, there is no need to copy native library into the project during application development, because the APP can run through AIDL. Load from opencv-Manager.
Why does OpencV offer both?
The reason is that the opencV native library file size is relatively large.
If it is loaded from a local package and many applications installed on the device use OpencV, a Native library file must be copied for each application, wasting storage space. In addition, the configuration of Native library in the project is also relatively tedious.
Although pre-installation of OpencV-Manager on the device saves storage space, but a major drawback is that there is no guarantee that users will install OpencV-Manager on the device. Besides, users will not install another app in advance for your app, and users will not listen to you.
Therefore, when learning or developing demo, you can install OpencV-Manager in advance, which saves the operation of copying and configuring native library, and also saves the space of mobile phone. However, if you are developing a formal app, it is better to load it from a local package.
2. Does it involve NDK development?
While Java is still the mainstream for Android development, Kotlin is fully compatible with Java, despite its growing popularity. Opencv is c/ C ++ implementation. So the use of OpencV ability in Android needs to solve the problem of communication between two languages, the use of course is JNI, OpencVSDK Java project on JNI call OpencV method encapsulation, development directly call encapsulated Java method to achieve the call of OpencV ability, It’s nice not having to deal with C/C ++.
But after all, The bottom layer of OpencV is C/C ++ implementation, want to use 100% of the ability of OpencV, or the METHOD provided by the SDK Java library can not meet the requirements, or need to use C/C ++ to achieve part of the function, packaging into JNI method. Besides, now it’s all team development, so you can have special partners to implement the low-level work, right? This requires copying and configuring relevant native libraries and using NDK for development.
Of course, the better ones can download the source code and modify it, compile it themselves, not only get the latest SDK, but also implement the underlying algorithms and so on as needed.
Here’s a quick summary of where the different development mechanisms apply:
- Java library + Opencv-manager. apk: suitable for personal learning or demo, install Opencv-Manager. apk on the device in advance, and then import the Java library project, you can carry out subsequent development.
- Java library + Native library: Develop online applications without special requirements for OpencV capabilities.
- Java library + Native library + self-encapsulation implementation of JNI approach: develop online applications, but the OpencV capabilities provided by the Java library does not meet business requirements.
4, development of actual combat
After the above discussion, the development mechanism of Android OpencV has a general understanding, the following are several development methods for simple implementation.
1. Java library + OpencV-manager.apk
To prepare
What needs to be prepared is the SDK’s Java library project and the device platform corresponding OpencV-manager.apk
configuration
- Install OpencV-manager.apk on the device
- Create a new project in Android Studio and open it if it already exists
- Import SDK Java library project:
File-New-Import module
, locate the Java library project, import the library project module name is openCVLibrary343 - Configure the main Module to rely on openCVLibrary343: add it to dependencies in the guild.gradle file of the main modlue
implementation project(':openCVLibrary343')
, and then synchronize the code.
The development of
Initialize OpencV in the Activity’s onResume method
// To use OpenCV Engine Service, you need to install OpenCV Manager before running the device
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this.new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
if (status == LoaderCallbackInterface.SUCCESS) {
Log.d(TAG, "onManagerConnected: success");
} else {
super.onManagerConnected(status); }}});Copy the code
Blur the mean value of the image
// Load images locally
Bitmap originImg = BitmapFactory.decodeFile("filePath");
// Initialize Mat
Mat src = new Mat(originImg.getHeight(), originImg.getWidth(), CvType.CV_8UC4);
/ / bitmap mat
Utils.bitmapToMat(originImg, src);
// The mean is ambiguous
Imgproc.blur(src, src, new Size(3.3));
// Initialize the bitmap after processing
Bitmap processedImg = Bitmap.createBitmap(src.cols(), src.rows(), Bitmap.Config.ARGB_8888);
/ / mat bitmap
Utils.matToBitmap(src, processedImg);
Copy the code
2. Java library + Native library
The difference with the above method lies in:
- No prior installation of Opencv-manager.apk is required
- You need to configure the Native library in your project
To prepare
The things that need to be prepared are the Java library project of SDK and native library (SDK-native LiBS).
configuration
- Import the Java library project and configure the dependencies. The specific steps are described in the “Java library + Opencv-manager.apk” development mode
- Copy the liBS folder to the src-main-jnilibs path of the project. If the jniLis folder does not exist, create a new one
- Open the build.gradle file of the Module, add the following code to the Android node, and synchronize the code
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs/libs']}}Copy the code
The development of
It is different from the [Java library + OpencV-manager.apk] development mode when opencV is initialized, everything else is the same.
Initialize OpencV in the Activity’s onResume method
// Use the app native library
OpenCVLoader.initDebug();
Copy the code
3. Java library + Native library + self-encapsulated JNI method
This way is to use NDK for JNI development on the basis of [Java library + Native library] development mode.
To prepare
The things that need to be prepared are THE Java library project of SDK, native library (SDK-native libs), native library header file (SDK-native jNI-include).
configuration
- Refer to blog to establish a project to support NDK development.
- Complete the relevant configuration in the development mode of Java library + Native library.
- Copy the native library header file (SDK-native-jni-include) to the src-main-cpp directory (if there is no such directory, please follow the procedure of [Audio and Video Development 01 — AS3.x NDK Development Environment Construction] first)
- Edit the content of cmakelists. TXT file (if there is no such file, please first follow the process of [Audio and Video Development 01 — AS3.x NDK Development Environment construction] to operate)
- The configuration is cumbersome. If any problem occurs, search for it and learn about android Studio Cmake NDK development.
Cmake_minimum_required (VERSION 3.4.1 track) include_directories (${CMAKE_SOURCE_DIR}/src/main/cpp/include)
add_library(libopencv_java3 SHARED IMPORTED)
set_target_properties(libopencv_java3 PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/libs/${ANDROID_ABI}/libopencv_java3.so)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
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 )
target_link_libraries( # Specifies the target library.
native-lib libopencv_java3
# Links the target library to the log library
# included in the NDK.
${log-lib} )
Copy the code
The development of
Watch the demo, which is jNI development.
conclusion
Android using OpencV for development, for different development and application scenarios to choose the development way, in general, involves a lot of things, Android knowledge, NDK/JNI knowledge, C/C ++ knowledge, OpencV knowledge, mathematical knowledge, etc..
Based on Android development, I built a development framework through this article, and learned OpencV API and relevant theoretical knowledge in the process of writing demo, as well as NDK/JNI and C/C ++.