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
OpenCV 4.1.2 AndroidStudio 3.4.1
I referred to this article. Installation and import are completed without any problems.
Using OpenCV with Android Studio
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);
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
}
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.
For the time being, I used the source code written in StackOverflow as it is. When I tried debugging,
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.
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.
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