Integrate OpenCV 4 into Android via Gradle

Integrate OpenCV 4 into Android via Gradle

OpenCV is an open-source library that can be used for image processing, computer vision, machine learning, etc. OpenCV 4 can be easily integrated into Android via Gradle using com.quickbirdstudios:opencv dependency. It doesn't require Android NDK toolset.

This tutorial provides step by step how to set up OpenCV 4 in Android project.

  1. Open Android Studio and start a new project.
  2. Choose empty activity.
  3. Provide application name, package name and location where project should be saved. Choose Java or Kotlin language.
  4. Open module's build.gradle file and add OpenCV 4 dependency in dependencies section.

app/build.gradle

dependencies {
    // Other dependencies
    // ...
    implementation 'com.quickbirdstudios:opencv:4.5.3.0'
}
  1. Open a layout XML file and add ImageView.

app/src/main/res/layout/activity_main.xml

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

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
  1. Copy the test image into app/src/main/res/drawable directory.
  2. In the MainActivity class, initialize OpenCV with OpenCVLoader.
  3. Use the method Utils.loadResource() to load a resource. We use orange.png image for testing. It stored in the drawable directory.
  4. Android Bitmap use RGB color channels. By default, OpenCV Mat use BGR color channels. Use the method Imgproc.cvtColor to convert color channels.
  5. Initialize a bitmap with the specified width and height using the method Bitmap.createBitmap(). The config ARGB_8888 defines that each channel (RGB and alpha) is stored with 8 bits of precision.
  6. Use the method Utils.matToBitmap() to convert OpenCV Mat to Android Bitmap.
  7. To display an image in the ImageView use method setImageBitmap().

app/src/main/java/com/example/app/MainActivity.kt

package com.example.app

import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import org.opencv.android.OpenCVLoader
import org.opencv.android.Utils
import org.opencv.imgproc.Imgproc

class MainActivity : AppCompatActivity()
{
    companion object {
        init {
            if (OpenCVLoader.initDebug()) {
                Log.d("MY_APP", "OpenCV loaded")
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val mat = Utils.loadResource(this, R.drawable.orange)
        Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2RGB)

        val bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888)
        Utils.matToBitmap(mat, bitmap)

        val myImageView: ImageView = findViewById(R.id.myImageView)
        myImageView.setImageBitmap(bitmap)
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.