Ask Your Question
0

Unable to convert mat to bitmap

asked 2014-10-30 13:51:03 -0600

Deepak Kumar gravatar image

updated 2020-09-22 08:37:16 -0600

hi, i am not able to convert mat to bitmap in the below code. can any one tell me what wrong i am doing here.

here is the code :-

    Mat rgb = new Mat (); 
    Mat rgb32f = new Mat();
    Mat gry = new Mat ();   
    Mat imfilter = new Mat (height, width, CvType.CV_32F, new Scalar(4));
    Mat res = new Mat();
    Bitmap bmp = t1.copy(Bitmap.Config.ARGB_8888, true);

    Utils.bitmapToMat(bmp, rgb);
    rgb.convertTo(rgb32f, CvType.CV_32F);

    Mat mu = new Mat();     
    Size ksize = new Size(5,5);
    Imgproc.blur(rgb32f, mu,ksize);

    Mat mu2 = new Mat();
    Imgproc.blur(rgb32f.mul(rgb32f), mu2, ksize);

    Mat sigma = new Mat();
    Core.sqrt(mu2, sigma);

    Core.normalize(sigma, gry, 0.0, 1.0, Core.NORM_MINMAX);

    Bitmap scale = Bitmap.createBitmap(gry.cols(), gry.rows(), Bitmap.Config.ARGB_8888);


    Utils.matToBitmap(gry, scale);

Error comes in the last line Utils.matToBitmap() function.

here is logcat :-

10-30 21:23:27.999: E/cv::error()(8900): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 97

10-30 21:23:28.009: E/org.opencv.android.Utils(8900): nMatToBitmap catched cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)

10-30 21:23:28.009: W/dalvikvm(8900): threadid=1: thread exiting with uncaught exception (group=0x41bb3798)

10-30 21:23:28.009: E/AndroidRuntime(8900): FATAL EXCEPTION: main

10-30 21:23:28.009: E/AndroidRuntime(8900): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)

edit retag flag offensive close merge delete

Comments

And what is the error code?

Doombot gravatar imageDoombot ( 2014-10-30 14:31:23 -0600 )edit

the exact error msg, please.

(but it looks, like your gry Mat is float, not uchar)

berak gravatar imageberak ( 2014-10-31 03:07:59 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2019-06-04 14:06:30 -0600

mylji gravatar image

This is a really old question, but I'm going to answer it anyways just in case anyone stumbles on this issue:

You can't convert a CV_32F Mat to Bitmap using Utils.matToBitmap.

Taken directly from documentation:

"The input Mat object has to be of the types 'CV_8UC1' (gray-scale), 'CV_8UC3' (RGB) or 'CV_8UC4' (RGBA)"

Links: https://docs.opencv.org/java/2.4.9/or...

Hopefully it will help someone.

edit flag offensive delete link more
0

answered 2015-11-09 11:23:08 -0600

I had the same problem.

Try this:

Bitmap scale = Bitmap.createBitmap(gry.width(), gry.height(), Bitmap.Config.ARGB_8888);

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-10-30 13:51:03 -0600

Seen: 7,706 times

Last updated: Jun 04 '19