11

I wanted to simply convert a bitmap from Android to a Mat object for OpenCV. This topic is often adressed on Stack Overflow. For example:

convert Mat to Bitmap Opencv for Android ;

convert Bitmap to Mat after capture image using android camera ;

templateMatching mattoBitmap opencv for android

There is even more to find. I followed the instrcutions in this answers, but I'm still unable to get is done the right way.

Minimal code:

//First convert Bitmap to Mat
Mat ImageMat = new Mat ( image.getHeight(), image.getWidth(), CvType.CV_8U, new Scalar(4));
Bitmap myBitmap32 = image.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, ImageMat);

//Do smth.
Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_RGB2GRAY,4);

//Then convert the processed Mat to Bitmap
Bitmap resultBitmap = Bitmap.createBitmap(ImageMat.cols(),  ImageMat.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(ImageMat, resultBitmap);

//Set member to the Result Bitmap. This member is displayed in an ImageView
mResult = resultBitmap;

(note: image is a Bitmap supplied to this lines of code)

Errors:

08-07 15:13:59.188: E/AndroidRuntime(2115): FATAL EXCEPTION: main

08-07 15:13:59.188: E/AndroidRuntime(2115): java.lang.NoClassDefFoundError: org.opencv.core.Mat

But my imports are:

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;


//OpenCV
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

Would really appreciate any sort of help. Thanks DanS

Community
  • 1
  • 1
DanS
  • 183
  • 1
  • 2
  • 12
  • One Error found: Forgot to export the opencv lib. (Im new to java) New Error is UnsatisfiedLink Error as noted here : http://stackoverflow.com/questions/11614227/android-unsatisfiedlinkerror-with-opencv-2-4-2 – DanS Aug 07 '13 at 13:39
  • Now with the link noted above the code works ! – DanS Aug 07 '13 at 14:06
  • If the link above works. Can you either answer this question or close it? – GPPK Jun 05 '14 at 08:24

2 Answers2

1

You only can do your work with OpenCV after it is initialized. So you need to initialize it like that:

1.Create a Callback:

private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
        case LoaderCallbackInterface.SUCCESS:
            //DO YOUR WORK/STUFF HERE 
            break;
        default:
            super.onManagerConnected(status);
            break;
        }
    }
};

2.You need to initialize the callback in the onResume Method of your Activity:

@Override
    protected void onResume() {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this,
                mOpenCVCallBack);
    }

and thats it, i hope it was helpful :D

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
Mert
  • 1,333
  • 1
  • 12
  • 15
0

you should add OpenCV lib dependecy to your android prj config(properties->Android->Library ->add [opencv andrid prg])

yazt
  • 1