gussurya_w
gussurya_w

Reputation: 103

Android: Using Opencv Library for beginner (Conver to opencv format)

i have a problem using opencv library on android. I try to convert imageview to bitmap, then convert bitmap to mat (for opencv process), and then i convert it again to bitmap. But i got force cloce when run the program. This is my code:

public class MainActivity extends Activity {

protected static final String TAG = null;
ImageView img;
Button btn;

private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
       switch (status) {
           case LoaderCallbackInterface.SUCCESS:
           {
          Log.i(TAG, "OpenCV loaded successfully");
          // Create and set View
          setContentView(R.layout.activity_main);
           } break;
           default:
           {
          super.onManagerConnected(status);
           } break;
       }
     }
};

@Override
public void onResume() {
    super.onResume();
    LoaderCallbackInterface mLoaderCallback = null;
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);
}   

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //convert imageview to bitmap
    img =(ImageView) findViewById(R.id.imageView1);
    BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
    final Bitmap imgbitmap = drawable.getBitmap();

    btn=(Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

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

            //convert to grayscale
            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);


            //convert bitmap to imageview 
            ImageView imgbit;
            imgbit = (ImageView) findViewById(R.id.imageView2);
            imgbit.setImageBitmap(resultBitmap);
        }
    });        
}
}

This is my logcat:

12-09 16:27:23.052: D/dalvikvm(7837): Debugger has detached; object registry had 1 entries
12-09 16:27:23.582: D/dalvikvm(7837): GC_EXTERNAL_ALLOC freed 50K, 45% free 3007K/5379K, external 733K/1041K, paused 359ms
12-09 16:27:23.762: D/InputQueue(7837): Registering input channel '2b3941f0 com.gozur.latihanpcd/com.gozur.latihanpcd.MainActivity (client)'
12-09 16:27:23.812: I/FihConfig(7837): can not find this tag <SYSTEM_CUST_HANDWRITING>in the mUsedConfigData Array
12-09 16:27:38.492: D/dalvikvm(7990): GC_EXTERNAL_ALLOC freed 47K, 45% free 3007K/5379K, external 733K/1041K, paused 85ms
12-09 16:27:38.582: D/InputQueue(7990): Registering input channel '2b014008 com.gozur.latihanpcd/com.gozur.latihanpcd.MainActivity (client)'
12-09 16:27:38.642: I/FihConfig(7990): can not find this tag <SYSTEM_CUST_HANDWRITING>in the mUsedConfigData Array
12-09 16:27:40.362: D/WindowManager(7990): MotionEvent obtain 1 : MotionEvent{2afc4910 action=0 x=58.0 y=13.0 pressure=1.0 size=0.0}
12-09 16:27:40.492: W/dalvikvm(7990): No implementation found for native Lorg/opencv/core/Mat;.n_Mat (IIIDDDD)J
12-09 16:27:40.492: D/AndroidRuntime(7990): Shutting down VM
12-09 16:27:40.492: W/dalvikvm(7990): threadid=1: thread exiting with uncaught exception (group=0x2aacc560)
12-09 16:27:40.502: E/AndroidRuntime(7990): Uncaught handler: thread main exiting due to uncaught exception
12-09 16:27:40.532: W/System.err(7990): java.io.FileNotFoundException: /data/logcat_0.txt (Permission denied)
12-09 16:27:40.532: E/AndroidRuntime(7990): FATAL EXCEPTION: main
12-09 16:27:40.532: E/AndroidRuntime(7990): java.lang.UnsatisfiedLinkError: n_Mat
12-09 16:27:40.532: E/AndroidRuntime(7990):     at org.opencv.core.Mat.n_Mat(Native Method)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at org.opencv.core.Mat.<init>(Mat.java:535)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at com.gozur.latihanpcd.MainActivity$1.onClick(MainActivity.java:66)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at android.view.View.performClick(View.java:2494)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at android.view.View$PerformClick.run(View.java:9109)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at android.os.Handler.handleCallback(Handler.java:587)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at android.os.Looper.loop(Looper.java:130)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at java.lang.reflect.Method.invoke(Method.java:507)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
12-09 16:27:40.532: E/AndroidRuntime(7990):     at dalvik.system.NativeStart.main(Native Method)
12-09 16:27:40.572: W/System.err(7990): java.io.FileNotFoundException: /data/plog.log (Permission denied)

Upvotes: 0

Views: 3396

Answers (2)

Binil Jacob
Binil Jacob

Reputation: 501

I faced similar problems and it took me a day to understand. In your case try modifying your onResume() function to :

 @Override
    public void onResume()
    {
        super.onResume();
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_, this, mOpenCVCallBack);
        } else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mOpenCVCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);

        }
    }

I don't see a point in having this line:

 LoaderCallbackInterface mLoaderCallback = null;

Upvotes: 0

Darshan
Darshan

Reputation: 1018

I pretty much doubt that your code can display the original version of your image before you are converting it to Grayscale since your log cat error says

 No implementation found for native Lorg/opencv/core/Mat

which means you have not done static initialization before calling opencv API

 static {
    if (!OpenCVLoader.initDebug()) 
       {
        // Handle initialization error
    } }

Please follow the steps 3 and 4 from here.

Upvotes: 3

Related Questions