A Guide to Preparing OpenCV for Android

[ad_1]

This tutorial guides Android builders in getting ready the favored library OpenCV (Open Laptop Imaginative and prescient) to be used. By a step-by-step information, the library will likely be imported into Android Studio (the official IDE for Android).

Upon set up and setup, OpenCV can be utilized for performing any of the operations it helps, corresponding to object detection, segmentation, monitoring, and extra.

On the finish of this tutorial, OpenCV is used to use the Canny filter to a picture. The Android Studio mission related to that is obtainable on GitHub right here:

ahmedfgad/OpenCVAndroid
Using OpenCV in Android Devices. Contribute to ahmedfgad/OpenCVAndroid development by creating an account on GitHub.

 

Overview of OpenCV

 
OpenCV is a imaginative and prescient library constructed for doing complicated, real-time operations on photographs. It’s a free, open supply library initially written in C++. It contains interfaces with Python, Java, and MATLAB. Without having to jot down many strains of code to construct an operation, OpenCV already helps constructing such operations with a easy interface, the place the person can write only a few strains of code.

Earlier than discussing importing OpenCV into and Android mission, let’s begin by constructing an Android mission and guarantee that the Android improvement surroundings is working as anticipated.

The factors that will likely be coated on this tutorial are as follows:

  • Constructing an Android Studio Undertaking
  • Working the Undertaking
  • Modifying the Undertaking to Show a Toast Message
  • Downloading OpenCV
  • Importing OpenCV in Android Studio
  • Fixing Doable Gradle Sync Errors
  • Including OpenCV as a Dependency
  • Including Native Libraries
  • Utilizing OpenCV for Filtering Photographs
  • Abstract

 

Constructing an Android Studio Undertaking

 
Let’s undergo the steps of constructing a brand new Android Studio mission. The primary is to create a brand new mission from the File menu as illustrated within the subsequent determine.

Image for post
 

By deciding on the “New Undertaking” menu merchandise, a brand new window seems asking for a couple of particulars (e.g. app title and mission listing). The app title we’ll used for this tutorial is OpenCVAndroid.

Image for post
 

By clicking the Subsequent button, one other window seems asking for the goal units and the minimal SDK. You possibly can choose one of many SDKs obtainable in your surroundings. You possibly can decrease the minimal SDK for those who’d prefer to help extra units.

Image for post
 

By clicking Subsequent, one other window seems asking for whether or not a default exercise is to be created throughout the mission or not. There are a couple of totally different choices. If no exercise is to be created, you may choose the top-left choice “Add No Exercise”.

As a result of we’re going to construct an Android app, then there should be an Exercise, even when empty. Because of this, I chosen the “Empty Exercise” choice. Word that this exercise is just not fully empty because it accommodates a TextView that covers the display screen, as we’ll see later, after working the app.

Image for post

By specifying that the app is to incorporate an exercise, one other window seems asking for the Exercise Identify. This title is considered the category title of the Java file related to this exercise. After specifying the right title of your alternative, click on End to create the mission.

Word that you may verify the “Generate Format File” checkbox to create a structure for the exercise. You possibly can both verify or uncheck the “Backwards Compatibility” checkbox.

Image for post

After the mission is created, you may choose the Android mission view and can discover a Java file for the exercise named MainActivity. It’s a Java file with a .java extension, nevertheless it isn’t simply displayed within the Android mission view. There’s additionally an XML structure file named activity_main.xml, as illustrated within the subsequent determine.

Image for post

 

Working the Undertaking

 
With out discussing the implementation of those information, let’s run the mission to make sure the whole lot is working appropriately. To run an Android Studio mission, you both want an emulator (digital gadget) or an actual gadget related by way of a USB cable.

To run the mission, choose the Run ‘app’ choice from the Run menu. A window seems asking whether or not an emulator or a USB gadget is used. I presently don’t have any USB related units and thus will select the obtainable emulator.

Image

 

After the emulator begins up, the appliance will set up robotically and can seem within the app record, as proven beneath. Word that the app title “OpenCV Android” is what we entered beforehand.

Image for post

After the mission runs, the app won’t solely set up but additionally launch robotically. The display screen of the app is proven beneath. As specified beforehand, the exercise structure simply accommodates a TextView displaying “Hi there World!”.

Image for post

After working the mission, we all know that the event surroundings is working appropriately. Earlier than importing OpenCV throughout the mission, it’s higher to get conversant in the mission. Thus, let’s see the contents of the MainActivity.java and activity_main.xml information and make a easy edit.

On the lookout for a spark to take your apps to the subsequent degree? Machine studying permits highly effective, highly-personalized cellular experiences. Subscribe to the Fritz AI Newsletter to learn how.

 

Modifying the Undertaking to Show a Toast Message

 
The content material of the MainActivity.java file is given beneath. The exercise title is MainActivity, which extends the AppCompatActivity class. It’s well-known that an Android exercise extends the Exercise class, however on this mission, it really extends the AppCompatActivity as a result of we checked the “Backwards Compatibility” choice whereas creating the mission.

The exercise has simply the onCreate() technique, which is known as as soon as the exercise is created. It simply makes use of the setContentView() technique for setting the exercise XML structure, which renders the UI of the exercise when launched.

The content material of the XML structure is listed beneath. It merely creates a structure of kind ConstraintLayout with width and peak protecting the gadget display screen. It simply has a single youngster view, which is TextView as specified whereas constructing the mission. The textual content of the TextView is ready to “Hi there World!”.

To get conversant in the mission, let’s make a easy edit by including a brand new Button view, which shows a Toast message when clicked. The edited structure XML file for including the Button view is listed beneath. The Button textual content is ready to “Show Toast Message”. When clicked, a callback technique named displayToast() will likely be referred to as.

The displayToast() technique is carried out within the edited exercise Java file, as proven beneath. It makes use of the Toast class for displaying a toast message.

After working the mission once more and clicking the button, the message will likely be displayed as given within the subsequent determine. By reaching this step, we’ve made positive the mission is working appropriately, and we even have a bit understanding of the primary exercise and its structure. Now, let’s begin importing the OpenCV library.

Image for post

 

Downloading OpenCV

 
For downloading OpenCV, you need to use this hyperlink https://opencv.org/releases, which lists the OpenCV releases. Scrolling down this web page, you will discover OpenCV 3.4.4, which is used on this tutorial. You possibly can obtain the most recent launch obtainable on the present time which is 4.1.Zero if that’s the best choice for you. Keep in mind to obtain the Android launch of the library.

Image

 

You’ll obtain a ZIP file, which you’ll must extract.

Image

 

The listing tree of the extracted folder is given within the subsequent determine. The two vital folders we’ll use are java and libs. Hassle of those information are subfolders of the sdk folder.

The java folder accommodates the java information of the OpenCV. As a result of not all information are written in Java and a few are written in C++ and nonetheless wanted for use inside Java, there’s one other folder named libs that holds these information.

Image

 

Importing OpenCV in Android Studio

 
So as to import a library in Android Studio, go to the File menu and choose “Import Module” as illustrated within the subsequent determine.

Image

 

After deciding on it, a brand new window seems asking for the trail of the module to be imported as proven beneath.

Image

 

Click on on the three dots close to on the fitting facet close to the “Supply Listing” and navigate to the trail the place the sdk folder contained in the downloaded OpenCV for Android exists.

Image

 

After clicking OK, the “Supply Listing” within the earlier menu will change in keeping with the chosen path as illustrated within the determine beneath. The module title will change to replicate that Android Studio detected OpenCV.

Image

 

Click on Subsequent to go to a different window proven beneath, the place you may simply click on End to import the library. After clicking End, you must await the Gradle Sync to complete.

Image

 

Fixing Doable Gradle Sync Errors

 
Quite a lot of errors would possibly seem when constructing the mission utilizing Gradle. Right here we’re going to debate Three of them.

The primary one is that it’s anticipated to come across a Gradle Sync error whereas loading the library as proven beneath. The issue is solely that OpenCV is utilizing an SDK not put in within the surroundings.

Image

 

We are able to remedy this problem in 2 methods. The primary one is to put in the SDK utilized by OpenCV. The second, which will likely be used on this tutorial, is to vary the SDK model utilized by OpenCV to one of many already obtainable SDKs.

To vary the SDK utilized by OpenCV, change the mission view to Undertaking and go to the construct.gradle file beneath the imported OpenCV. Don’t overlook to open the construct.gradle file beneath the OpenCV library, not beneath the app.

Image

 

The content material of this file is listed beneath. Based on the compileSdkVersion discipline, it displays that the SDK model utilized by OpenCV is 14 and hasn’t been put in. This error won’t seem for you if SDK 14 is already put in.

If SDK 14 isn’t put in for you, you may change it to a different obtainable SDK. I’ll use SDK 21. The edited construct.gradle file of the OpenCV library is listed beneath. You possibly can attempt to sync the mission once more after making this edit, and it’s anticipated that the whole lot will work efficiently after that.

The second problem is that the primary line within the construct.gradle file of the OpenCV library is perhaps as proven beneath. This refers to the truth that the imported OpenCV is an utility and never a library. I discovered that this line exists for OpenCV 4.1.0.

apply plugin: 'com.android.utility'

Utilizing this line returns an error whereas constructing the mission. The error says:

Unable to resolve dependency for ‘:app@debugUnitTest/compileClasspath’: Couldn't resolve mission :openCVLibrary433.

As a result of OpenCV is supposed to be imported as a library, the primary line should be the next:

apply plugin: 'com.android.library'

Altering the primary line to the one above is probably not sufficient. It’s because if the primary line within the OpenCV construct.gradle is apply plugin: ‘com.android.utility’, then it’s anticipated to seek out the applicationId set to org.opencv. It’s an issue to have such a discipline in initiatives imported as a library. So you must format it as a remark it utilizing // or just delete it.

 

Including OpenCV as a Dependency

 
Regardless of being imported inside Android Studio, OpenCV isn’t detected contained in the exercise Java file. The mission doesn’t know what org.opencv is. It’s because we even have so as to add the library as a dependency in our mission.

Image for post

So as to try this, choose the “Undertaking Construction” menu merchandise from the File menu, as proven beneath.

Image for post

This opens one other window. Within the left facet beneath the Modules part, click on on app after which go to Dependencies. This opens the dependencies window.

Image

 

So as to add a brand new dependency, click on on the inexperienced + icon on the fitting facet of the display screen, as proven beneath. A menu seems, from which we choose the “Module dependency” choice.

Image

 

A brand new window opens, which reveals an inventory of modules to be chosen as dependencies. We’ve got only a single module for OpenCV, and thus a single merchandise is out there. Choose it and click on OK.

Image

 

This takes you again to the dependencies window, however after OpenCV is added as one of many dependencies. Click on OK to substantiate including OpenCV as a dependency.

Image

 

After doing that, you’ll discover that the OpenCV library is detected throughout the Java code.

Image

 

Including Native Libraries

 
Some information inside OpenCV are native. This implies they aren’t written in Java however C++. These information can be found throughout the libs folder. The folder tree of the downloaded OpenCV file is given once more beneath to refresh your reminiscence concerning the location of the libs folder which is OpenCV/sdk/native/libs.

Image

 

This folder must be copied into the mission beneath this listing: OpenCVAndroid/app/src/primary/. It’s essential to rename the copied folder to be jnilibs. After doing that, the contents of the mission’s primary folder are as proven within the subsequent determine. After doing that, the OpenCV library is prepared to be used.

Image

 

Utilizing OpenCV for Filtering Photographs

 
After importing OpenCV efficiently inside Android Studio, this part of the tutorial makes use of OpenCV to use a filter over a picture. The GitHub project accommodates the Android Studio mission through which OpenCV is already imported, and the Canny filter is utilized to a picture after clicking a button.

The structure of the earlier app will likely be modified so as to add an ImageView, as listed beneath. This isn’t the one change, because the structure used is modified to a LinearLayout. The orientation used for this structure is vertical. Furthermore, the TextView is now not wanted and thus eliminated.

The picture displayed on the ImageView is a useful resource picture named check. So as to add a useful resource picture to the mission, merely use the Android mission view and drag and drop a picture file into the drawable folder, as illustrated beneath. The picture file is known as check.jpg. The useful resource title of this picture, check, is derived from the file title.

Image

 

The app display screen after working the modified app is proven beneath. Subsequent, we write Java code for studying the useful resource picture displayed within the ImageView, course of it utilizing OpenCV, after which show the resultant picture once more within the ImageView.

Image

 

Beforehand, the Button view was displaying a toast message when clicked. This time, it would apply the Canny filter over the picture. The modified Java code is listed beneath. All you must do is to create a drawable useful resource with the title check.

After clicking the button, the useful resource picture is learn as an OpenCV Mat named img. Then the Mat is filtered in keeping with the Canny() technique contained in the Imgproc class and the filtered picture is saved into the img_result Mat.

This Mat is then transformed right into a bitmap picture utilizing the matToBitmap() technique. Lastly, the transformed bitmap picture is ready because the picture displayed on the ImageView utilizing the setImageBitmap() technique. The end result after filtering the picture is proven within the subsequent determine.

Image for post

And that’s it!

 

Abstract

 
This tutorial mentioned the detailed steps for utilizing OpenCV in Android Studio. An Android Studio mission is created, and after ensuring it’s working correctly, we began getting ready OpenCV.

To do that, we first downloaded OpenCV from its official website. After that, we imported OpenCV as a module throughout the Android Studio mission and added it as a dependency. We then copied the libs folder (inside Android Studio) into our mission, renaming it jnilibs. As a last step in getting ready OpenCV for Android Studio, we initialized OpenCVLoader utilizing the onCreate() technique.

Lastly, we ensured OpenCV was working correctly throughout the mission and used it to construct a easy app through which a picture is filtered utilizing Canny.

 

For Contacting the Writer

 

 
Bio: Ahmed Gad acquired his B.Sc. diploma with glorious with honors in data know-how from the School of Computer systems and Data (FCI), Menoufia College, Egypt, in July 2015. For being ranked first in his college, he was beneficial to work as a educating assistant in one of many Egyptian institutes in 2015 after which in 2016 to work as a educating assistant and a researcher in his college. His present analysis pursuits embrace deep studying, machine studying, synthetic intelligence, digital sign processing, and laptop imaginative and prescient.

Original. Reposted with permission.

Associated:

[ad_2]

Source link

Write a comment