October 07 2017 0Comment

How to use Opencv with Android

(Last Updated On: October 8, 2017)

OpenCV is Open sourced computer vision library. It is machine learning software library used for Image processing and computer vision techniques. OpenCV have more than 2500 optimized algorithms for Image Processing. It is mostly used for real time computer vision applications. OpenCV can be used in any platform like C++, Python, android but for use in each platform we must configure its related sdk in that project. Today we will discuss use of OpenCV in android studio. We will also some basic examples. First of all if we want to use OpenCV in Android then we need to configure it in application. If we want to use OpenCV in android studio we need to follow following steps.

Configuration Steps:

  • Download Open CV android sdk from opencv.org
  • Extract the downloaded zip folder anywhere on the system
  • Now open android studio and build an empty project with blank activity.
  • Go to “File –> New –> Import Module”


  • Click on browse Go to “OpenCV- android- sdk –> sdk –> java “
  • Select the java folder and click ok , next and finish
  • You shall see OpenCV library added to your project in the project explorer


After that you will see lot of errors in your project


  • Go to “Files –> Project Structure –> Under Modules select app and note the Compile sdk version and Built Tools Version OpenCV –>src–>gradle .build
  • Now go to “Gradle Scripts–> build. gradle(Module: OpenCV)
  • Replace the compile and build tool version with the one in project structure and Sync the project
  • OpenCV android sdk is the wrapper for native OpenCV and in turn executes all the code in native library for optimization purpose


      After that

  • Go to Project Explorer and under your project
  • Go to “app –> src –> main“
  • Right click on main go to “New–> Directory ” Enter name of directory ” jniLibs ” and click Ok
  • Now open the OpenCV- android- sdk folder that was extracted
  • Go to ” sdk –> native –> libs” and copy all the folder present inside the libs folder
  • Paste these copied folders in the ‘ jniLibs ‘ folder just created



Last but main thing is   module dependency. For module selection

  • Go to ” File à project structure –> select app under ‘Modules’ –> select Dependencies section “
  • Click “+” button and then click on “3.Module Dependency”.
  • Select OpenCV and click Ok twice
  • Add this line in onCreate Method of the MainActivity or any other activity you want to use opencv in “ System. loadLibrary (“opencv_java3″);”



Now after OpenCV configuration in android application we are able to use OpenCV libraries and build in function so we will write our first program to convert an image from RGB to Gray scale.

Code Directions:

For that we will import some Image processing libraries in our application. Like

import org.opencv.core.Mat;

import org.opencv.core.Core;

import org.opencv.android.Utils;

import org.opencv.imgproc.Imgproc;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;



After that we create one button and two ImageView’s one for original image (Loaded from resources) and second for gray scaled image.


ImageView imageView1, imageView2;

Button btn;


Then in onCreate function we include “opencv_java3” Main library.







After that we load  image from resources using Bitmap object and set ImageView’s and onCLickListener.

Bitmap img = BitmapFactory.decodeResource(getResources(),R.drawable.pen); imageView1=(ImageView)findViewById(R.id.imageView);


btn =(Button)findViewById(R.id.button);




Then OnClick function starts where we create two Mat objects to store and process image. Mat is a multi-dimensional array which can store any size of image dynamically.

Mat source = new Mat();

Mat dest = new Mat();


After Mat objects we will convert our image from BitMaptoMat so we can process it.

Utils.bitmapToMat(img, source);


Now our resourced image is stored in source. Next we will convert image color from RGB to Gray scale.

Imgproc.cvtColor(source, dest, Imgproc.COLOR_RGB2GRAY);


After image conversion we set its dimensions and convert it to bitmap to show in Image view.

Bitmap btmp = Bitmap.createBitmap(dest.width(),dest.height(),Bitmap.Config.ARGB_8888);




Write a Reply or Comment