Try to introduce OpenCV to Android application

Introduction

I'm tired of making apps that can be completed with just the defaults of Android Studio, so Use OpenCV to create images and expand the range of apps. Install it as a starting point and try it out.

Continuation of Import device image with Android app

environment

OpenCV 4.1.2 AndroidStudio 3.4.1

Installation

I referred to this article. Installation and import are completed without any problems.

Using OpenCV with Android Studio

Import in Mat format

In the previous article, I went to the point of importing device image data with bitmap. Convert this bitmap to Mat format for handling with OpenCV.

The following methods are provided in the Utils class of android.package included in the OpenCV Library. Use this to convert bitmap to Mat. By converting the image to Mat format, you will be able to edit the image in various ways using the OpenCV Library.

Utils.java


/**
 * Short form of the bitmapToMat(bmp, mat, unPremultiplyAlpha=false).
 * @param bmp is a valid input Bitmap object of the type 'ARGB_8888' or 'RGB_565'.
 * @param mat is a valid output Mat object, it will be reallocated if needed, so Mat may be empty.
 */
public static void bitmapToMat(Bitmap bmp, Mat mat) {
    bitmapToMat(bmp, mat, false);
}

Main.java


//bmp_img:Imported bitmap
//mat_img:Prepare Mat format variables in advance
Utils.bitmapToMat(bmp_img,mat_img); 

Try using Mat

This time, I will try to make an application that converts a color image to grayscale when you press the button. As for which method to use, I will steadily search from the official documentation. Open Source Computer Vision Or if you search the entire project with [gray] with OpenCV installed, you will find something like that.

This time, the following methods were prepared in the Imageproc class.

Imageproc.java


public static void cvtColor(Mat src, Mat dst, int code) {
    cvtColor_1(src.nativeObj, dst.nativeObj, code);
}

In the argument of code, put a constant of what kind of conversion is to be performed. ʻRefer to the list of conversion methods in the file of imageproc.hpp. This time, I want to convert a color image to grayscale, so I use COLOR_RGB2GRAY`.

Main.java


Utils.bitmapToMat(bmp_img,mat_img); //bmp_img:Imported bitmap,mat_img:Prepare Mat format variables
Mat mat_output = new Mat();
cvtColor(mat_img,mat_output,COLOR_RGB2GRAY);

This should convert the mat_img image to grayscale and put it in mat_output. Finally, convert this mat_output to bitmap again and output it to ImageView.

Main.java


private Mat mat_output = new Mat();

public void onClick(View v) {
    Utils.bitmapToMat(bmp_img,mat_img); //Convert from Bitmap to Mat
    cvtColor(mat_img,mat_output,COLOR_RGB2GRAY);    //Convert to GrayScale
    Utils.matToBitmap(mat_output,bmp_img);  //Back to Bitmap
    img_picture.setImageBitmap(bmp_img);    //Display in ImageView
}

A crash has occurred!

This completes! I thought, but the moment I pressed the button, a crash occurred. When I debugged, a crash occurred when I was newing Mat in the first place.

Looking at the error message ... java.lang.UnsatisfiedLinkError: No implementation found for long org.opencv.core.Mat.n_Mat() (tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__)

It says something like Mat () cannot be found. When I looked at Stackoverflow when I was in trouble, I found a person with the same circumstances. No implementation found for long org.opencv.core.Mat.n_Mat() error Using OpenCV

To summarize the content of the article

--OnCreate () has been called before loading OpenCV Library -Initialize OpenCV asynchronously using ** OpenCV Manager ** and so on --Execute BaseLoaderCallback before OnCreate () and declare an instance of Mat () in it. --Check if OpenCV is installed every time with OnResume ()

What I found after a little more research

--This ** OpenCV Manager ** is installed separately for each user. --The capacity of the app will be reduced by not including the Library in the app. -Run ** OpenCVLoader.initAsync ** to launch ** OpenCVManager ** --BaseLoaderCallback is called when Init is completed, so Mat is generated in it.

I tried it

For the time being, I used the source code written in StackOverflow as it is. When I tried debugging,

  1. mLoaderCallback.onManagerConnected (LoaderCallbackInterface.SUCCESS) is called in OnResume ()
  2. An instance of Mat is created in onManagerConnected

I was able to confirm the flow.

When I thought that I would be required to install ** OpenCV Manager ** when starting the app, there was nothing. Also, OpenCVLoader.initAsync in OnResume () was not called. (I missed it at the first startup ...?) It may depend on the version of Android or OpenCV.

result

It's done! The photo is the character of "Ice Monster" from a shaved ice shop in Taiwan? is. The mango shaved ice was really good, so be sure to check it out when you go to Taiwan. gray.png

Finally

It took a little time to build the environment, but now I can handle OpenCV. Looking at the official documentation, it seems that a huge library is prepared, so I would like to touch on various things from now on.

Recommended Posts

Try to introduce OpenCV to Android application
Introduction to Android application development
Introduce two-factor authentication to your Rails application
I tried to make a simple face recognition Android application using OpenCV
Android app: Try to summarize events and listeners
(Android) Try to display static text using DataBinding
Try deploying Rails application to EC2-Part 2 (Server construction)-
Introduce docker to the application you are creating
[Introduction] Try to create a Ruby on Rails application
Try to release gem
Introduce Vue.js to Rails
Introduction to Android Layout
[Note] Challenge to develop Android application for business use
Rails6 I tried to introduce Docker to an existing application
Android application development preparation 7/15
[Introduction to Android application development] Let's make a counter
How to use OpenCV 4 on Android and view camera live view
How to create an application
Try Spring Boot from 0 to 100.
How to introduce Basic authentication
Introduce Maven to Tomcat project
Deploy your application to WildFly
Rewriting from applet to application
[Android] Connect to MySQL (unfinished)
A newcomer tries to summarize the Android view (beginner Android application development)
Try to make a cross-platform application with JRuby (jar file generation)
I tried to make an Android application with MVC now (Java)