Home » Android » android – OpenCV nMatToBitmap Assertion Failed

android – OpenCV nMatToBitmap Assertion Failed

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m getting the following errors using some generic functions within OpenCV for Android

12-05 21:08:55.486: E/cv::error()(6658): 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/oleg/sources/opencv/modules/java/generator/src/cpp/utils.cpp, line 107
12-05 21:08:55.486: E/org.opencv.android.Utils(6658): nMatToBitmap catched cv::Exception: /home/oleg/sources/opencv/modules/java/generator/src/cpp/utils.cpp:107: 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)
12-05 21:08:55.486: E/CameraBridge(6658): Mat type: Mat [ 144*192*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0x1024c0, dataAddr=0x44783010 ]
12-05 21:08:55.486: E/CameraBridge(6658): Bitmap type: 384*288
12-05 21:08:55.486: E/CameraBridge(6658): Utils.matToBitmap() throws an exception: /home/oleg/sources/opencv/modules/java/generator/src/cpp/utils.cpp:107: 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)

I’m not sure if this is the error itself or if it’s caused by another problem.

How to&Answers:

The assertion error is telling you that one or more of the following tests is failing:

src.dims == 2
info.height == (uint32_t)src.rows
info.width == (uint32_t)src.cols

I’m guessing info contains the dimensions of the destination bitmap. In that case, either your source Mat is not 2 dimensions or the dimensions of the destination bitmap don’t match the dimensions of the source Mat.

These two lines

12-05 21:08:55.486: E/CameraBridge(6658): Mat type: Mat [ 144*192*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0x1024c0, dataAddr=0x44783010 ]
12-05 21:08:55.486: E/CameraBridge(6658): Bitmap type: 384*288

suggest that your Mat is 144×192 and your bitmap is 384×288. It looks like one is portrait and the other landscape plus your bitmap is twice the resolution of your Mat.

Answer:

I don’t have enough rep to comment so I’ll provide an answer instead:

When working with the ‘onCameraFrame’ method – this assertion is thrown if the ‘mat’ you return doesn’t match the size of the frame used to display the output.

In other words – if you’re resizing for some sort of processing, make sure to return it to the original size before throwing it back to the display.

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame 
inputFrame) {
    mRgba = inputFrame.rgba();

    Mat resizeImage = new Mat();
    Size sz = new Size(800, 600); // Scale up to 800x600
    Imgproc.resize(mRgba, resizeImage, sz);

    // Do some sort of processing here on resized image.

    Mat afterResize = new Mat();
    Size szAfter = new Size(640, // Scale back down to 640x480 (original dim.)
    Imgproc.resize(resizeImage, afterResize, szAfter);

    return afterResize;
}