convert Bitmap to Mat after capture image using android camera
Asked Answered
C

4

37
Mat b = new Mat();
Bitmap bmp = getIntent().getExtras().getParcelable("image_send");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_image);
    Mat tmp = new Mat (bmp.getWidth(), bmp.getHeight(), CvType.CV_8UC1);
    Utils.bitmapToMat(bmp, tmp);
    Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
    //Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2RGB, 4);
    Utils.matToBitmap(tmp, bmp);

    iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageBitmap(bmp);
}

Can't display the bmp. My app has stopped after taking a picture.

Clop answered 30/6, 2013 at 12:19 Comment(1)
you can't use opencv code in onCreate (the native so's werent loaded yet)Ethereal
B
61

Utils.bitmapToMat converts an Android Bitmap to an OpenCV Mat. It requires a bitmap of type ARGB_8888 or RGB_565.

import org.opencv.android.Utils;

Mat mat = new Mat();    
Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32, mat);
Buford answered 17/7, 2013 at 15:7 Comment(3)
What is the use of this line Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);? Why can't I just do Utils.bitmapToMat(bmp, mat);?Iasis
i'm really in need of help with this question, can you help me, please? #61216902Bookshelf
I can answer the above question. The bitmapToMat only accepts certain bitmap types. This ensures it is ARGB_8888. If it's the wrong format, you'll get garbage on your mat.Kelsey
S
7

Mat tmp = new Mat (bmp.getWidth(), bmp.getHeight(), CvType.CV_8UC1);

OpenCV Mat constructor expects rows, cols pair instead of width, height as its arguments, invert them.

Try:

Mat tmp = new Mat (bmp.getHeight(), bmp.getWidth(), CvType.CV_8UC1);

Softy answered 13/9, 2015 at 16:30 Comment(1)
Also the type is wrong, at the time of conversion the image is still in color+alpha (CV_8UC4)Flip
A
0

With Camera2 this task is very fast, only you need config the ImageReader with ImageFormat on YUV_420_888 and then proccess frames with OpenCV like this:

// You can read image with differents patterns for example grayscale:
Mat mGray(height, width, cv::IMREAD_GRAYSCALE, pFrameData); 

A complete implementation in the next answer: https://mcmap.net/q/426364/-how-to-convert-amp-rotate-raw-nv21-array-image-android-media-image-from-front-cam-portrait-mode-in-onimageavailable-android-camera2

Aril answered 17/3, 2018 at 1:26 Comment(0)
R
-1

Same problem with my app. In the main activity, I had to render OpenCV utilizable. (I am assuming your app threw a link error when Mat library was used). All sample apps do this. Include this your in main activity.

 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i("OpenCVManager setup", "OpenCV loaded successfully");
                  //Use openCV libraries after this  
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this,
                mLoaderCallback);
    }
Retrochoir answered 12/11, 2016 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.