简体   繁体   中英

OpenCV4Android - find/drawContour error

What I'm attempting to do is take a thresholded image and perform findContours on it then draw it out to a rotation corrected image. The rotation corrected image and the thresholded image work as expected so I'm at a bit of a loss figuring out why this would crash. The thersholded image is a grey version of the rotation corrected image that has had a binary threshold applied to it.

public void findImageContours(Mat passedThreshInt, Mat passedRotatedInit) 
    {
    /* Get Thresholded input image */
    Mat initThresh = passedThreshInt.clone();

    /* Get image to draw Contours on */
    Mat initRotated = passedRotatedInit.clone();

    /* Get contours from threshold image */
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(initThresh, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    /* Draw Contours */
    Scalar CONTOUR_COLOR = new Scalar(255,0,0,255);

    Log.e(TAG, "Contours count: " + contours.size());
    Imgproc.drawContours(initRotated, contours, -1, CONTOUR_COLOR);

    /* Save Output */
    contouredInit = initRotated.clone(); //ContouredInit is Global
    Utils.matToBitmap(contouredInit, contouredBitmapInit); // contouredBitmapInit is Global
    }

Error:

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 /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 97  

nMatToBitmap catched cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/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)

And after that a JDI Dispatch Error.

Dimensions of Bitmap has to match the dimensions of OpenCV Mat . I suggest you create Bitmap as below.

Bitmap bitmapImg = Bitmap.createBitmap( matImg.cols(), matImg.rows(), Bitmap.Config.ARGB_8888 );

For future errors of these kind, a look into opencv github repository( 1 ) might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM