How to implement OpenCV-4.5.1 to Android projects

Step-1

Download OpenCV-4.5.1 for Android. You can also choose other versions of OpenCV.

Step-2

Create an Android project. If you already have a project, you can pass this step and continue with Step-3.

  • Create New Project like given in the below.
  • Choose “Empty Activity”
  • Name your project. In my case it was “OpenCV-Android-Example” with package name “com.example.opencv_android_example”.

Step-3

  • Import OpenCV module to your project by following the path :
  • You should choose the folder in the following path
  • After selecting the OpenCV folder, ADT Import Preferences will be showed and in this step you can select all and finish.

Step-4

  • Open build.gradle file of your project that should be look like given below and type “implementation project(path: ‘:opencv451’)”, “:opencv-451” can be changing based on your module name. (When importing module it automatically name it to “sdk”, so I have changed it to “opencv451” by right clicking on sdk module and refactoring module name)
  • After typing “implementation project(path:’opencv451’)”, you should Sync Project and it should be ready to go.
  • You should also check that minimum sdk version of both opencv and your app should be same. In my case, it was 21.

Step-5

So, we are ready to go now. Let’s check if everything works correctly. In this regard, we should write a simple application which has only a Button and ImageView, my activity_main.xml file like below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/image_view"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="centerInside"
android:layout_centerInParent="true"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:text="Click"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/image_view"/>
</RelativeLayout>
package com.example.opencv_android_example;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity {


Button button;
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OpenCVLoader.initDebug();
button = findViewById(R.id.button);
imageView = findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hello();
}
});
}


private void hello(){
Mat emptyMat = new Mat(500,500, CvType.CV_8UC3);
Imgproc.circle(emptyMat,new Point(250,250),25,new Scalar(255,255,255));
Bitmap bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
Utils.matToBitmap(emptyMat,bitmap);
imageView.setImageBitmap(bitmap);
}

}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store