How to Load an Image using OpenCV in Android?
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library which is used for image and video processing. In this article, we are going to build an application that shows the demonstration of how we can load images in OpenCV on Android.
Mat Class in OpenCV
The class Mat represents an n-dimensional dense numerical single-channel or multi-channel array. It can be used to store real or complex-valued vectors and matrices, grayscale or color images, etc.
Prerequisite: It is very important to integrate OpenCV in Android Studio to make this application. If you haven’t done it then you can go through the below article: How to Add OpenCV library into Android Application using Android Studio?
What we are going to build in this article?
In this article, we will be creating an application with two features i.e to select and capture images in OpenCV and then perform a small operation of converting them images from RGB to Gray. A sample video is given below to get an idea about what we are going to do in this article.
Step by Step Implementation
Step 1. Create a New Project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- Name the application at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2. Adding permissions required
Navigate to app > manifests > AndroidManifest.xml file and paste the following piece of code to add camera permission
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
After implementing the above code design of activity_main.xml file will look like this-
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Here is the final output of our application.
Please Login to comment...