2

I'm a beginner in Android Programming. I have a code for processing an image written in Java using OpenCV. I'm thinking to reuse the code. For this, I have to select an image and have to create Mat Object for it.

I have setup an OnClick Event Listener and calling a function, which in turn uses Intent to select an image. The Function call is as follows.

selectImage.setOnClickListener(
        new Button.OnClickListener() {
                public void onClick(View v){
                    selectImageFromGallery();
                }
        }
);

The Code for selectImageFromGallery() is as follows:

private void selectImageFromGallery(){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/png");
        if(intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent , SELCT_IMAGE_CODE);
        }
 }

I'm processing the result returned by Intent as follows.

@Override
protected void onActivityResult(int requestCode , int resultCode , Intent data){
        if(resultCode == RESULT_OK){
            Uri imageUri;
            if(data == null || data.getData()== null){
                imageUri = uriPhoto;
//                Log.i("URI","HERE");
            }else{
                imageUri = data.getData();

                Log.i("URI",imageUri.toString());

// I'm GETTING URI OF THE SELECTED IMAGE,BEING LOGGED SUCCESSFULLY !

                Imgcodecs imageCodecs = new Imgcodecs();
                Mat obj = imageCodecs.imread(imageUri.getPath());
                Log.i("URI" , "MAT OBJECT CREATED SUCCESSFULLY");
                Log.i("URI" , new Integer((int) obj.size().height).toString());
                Log.i("URI" , new Integer((int) obj.size().width).toString());
            }
            Intent intent = new Intent();
            intent.setData(imageUri);
            setResult(RESULT_OK , intent);
            finish();
        }
 }

But, in LogCat I'm getting the size of the image as 0 (Size of the selected image is 2160 x 1080) as I'm Logging the Height and Width of the Mat Object.

The corresponding LogCat info is

2019-02-06 23:48:21.927 27321-27321/com.example.hari.imagesteganography I/URI: content://com.android.providers.media.documents/document/image%3A110235
2019-02-06 23:48:21.938 27321-27321/com.example.hari.imagesteganography I/URI: MAT OBJECT CREATED SUCCESSFULLY
2019-02-06 23:48:21.940 27321-27321/com.example.hari.imagesteganography I/URI: 0
2019-02-06 23:48:21.940 27321-27321/com.example.hari.imagesteganography I/URI: 0

I have Configured OpenCV successfully with my project and loaded it correctly by System.loadLibrary("opencv_java3")

Is this the correct way to create an Mat Object from an Image selected by the User?

If not, how do I create Mat object in this scenario?

Thanks.

Oladipo
  • 1,579
  • 3
  • 17
  • 33

1 Answers1

2

I just always use convertion to Bitmap. CvType.CV_8UC4 will work for ARGB/RGB (Bitmap.Config.ARGB_8888).

import org.opencv.android.Utils

    @Override
    protected void onActivityResult(int requestCode , int resultCode , Intent data){
        if(resultCode == RESULT_OK){
            Uri imageUri;
            if(data == null || data.getData()== null){
                imageUri = uriPhoto;
//                Log.i("URI","HERE");
            }else{
                imageUri = data.getData();
            Log.i("URI",imageUri.toString());

            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

            Uri imageUri = data.getData();
            Bitmap bmp = MediaStore.Images.Media.getBitmap(
                                                  this.getContentResolver(),
                                                  imageUri);

            Mat obj = new Mat(bmp.width, bmp.height, CvType.CV_8UC4)
            Utils.bitmapToMat(bmp, obj)
            Log.i("URI" , "MAT OBJECT CREATED SUCCESSFULLY");
            Log.i("URI" , String.valueOf(obj.cols()));
            Log.i("URI" , String.valueOf(obj.rows()));
        }
        Intent intent = new Intent();
        intent.setData(imageUri);
        setResult(RESULT_OK , intent);
        finish();
    }

}

Raskilas
  • 691
  • 6
  • 17
  • Thanks for Answer, But ,How to get ```CV_8UC3``` Image? I just tweaked and wrote ```Mat obj = new Mat(bmp.width , bmp.height , CvType.CV_8UC3)```.But still ```obj.type()``` is returning 24,indicating ```CV_8UC4```.I also tried ```cvtColor(obj,this.coverImage,Imgproc.COLOR_BGRA2BGR)```.But,It did'nt work! – R.S.S.H KRISHNA Feb 08 '19 at 12:52
  • As I know, you need to set it manually, but if you will use functions that need, for example, CV_8UC1 like Imgproc.adaptiveThreshold(...) then you may see crashes/exceptions. In other words 8bit grayscale image can be stored in CV_8UC4, but RGB cannot be stored in CV_8UC1. I just use CV_8UC4 when I have no strong limits with memory because it does not affect execution time. – Raskilas Feb 08 '19 at 14:10