How to solve the OpenCv's drawing contour error

1

Basically, I am trying to draw the first contour with a color. But this program crashes with the following error :

9-11 09:56:38.230: D/dalvikvm(1920): GC_FOR_ALLOC freed 71K, 10% free 2824K/3124K, paused 0ms, total 3ms

09-11 09:56:38.340: D/dalvikvm(1920): GC_FOR_ALLOC freed 379K, 17% free 2958K/3564K, paused 3ms, total 4ms

09-11 09:56:38.360: D/dalvikvm(1920): GC_FOR_ALLOC freed 107K, 10% free 3361K/3696K, paused 2ms, total 3ms

09-11 09:56:38.390: D/dalvikvm(1920): GC_FOR_ALLOC freed 170K, 10% free 3702K/4100K, paused 4ms, total 4ms

09-11 09:56:38.420: E/cv::error()(1920): OpenCV Error: Bad argument (Unknown array type) in cv::Mat cv::cvarrToMat(const CvArr *, bool, bool, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/matrix.cpp, line 698

09-11 09:56:38.430: A/libc(1920): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 1920 (pencvratstudy01)

Program is

@Override
protected void onCreate(Bundle savedInstanceState) {

    Log.i(TAG, "called onCreate");
     super.onCreate( savedInstanceState );

     if (!OpenCVLoader.initDebug()) {
         // Handle initialization error
     }
     setContentView(R.layout.activity_main);


     // load an image from Resource directory
     Mat mMat = new Mat();
     try {
        mMat = Utils.loadResource(this, R.drawable.baby,
                                                 Highgui.CV_LOAD_IMAGE_COLOR);
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     // Create result object with correct color
     Mat result = new Mat(); 
     Imgproc.cvtColor(mMat, result, Imgproc.COLOR_RGB2BGRA);


     // create tmpMat object for gray image and blur it
     Mat tmpMat = new Mat();
     Imgproc.cvtColor(result,tmpMat , Imgproc.COLOR_BGR2GRAY);
     Imgproc.blur(tmpMat, tmpMat, new Size(3,3));


     /* find cany of tmpMat */
     Mat canny = new Mat();
     Imgproc.Canny( tmpMat, canny , 2 , 4);

     // find contours
     Mat hierarchy = new Mat();
     List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
     Imgproc.findContours(canny, contours, hierarchy, Imgproc.RETR_EXTERNAL, 
                                                    Imgproc.CHAIN_APPROX_SIMPLE);

     // draw contour on mask object   
     Mat mask = new Mat();
     Imgproc.drawContours(mask, contours, 0 , new Scalar(255));


     // create bitmap and draw on imageView
     Bitmap bmp;
     bmp = Bitmap.createBitmap(mask.cols(), mask.rows(), Bitmap.Config.ARGB_8888);
     Utils.matToBitmap(mask, bmp);

     ImageView imgView = (ImageView) findViewById(R.id.sampleImageView);
     imgView.setImageBitmap(bmp);


}

What is the problem here?

java
android
opencv
asked on Stack Overflow Sep 11, 2013 by user1776817 • edited Feb 19, 2019 by SenDjasni

1 Answer

0

The problem is that you are giving the wrong type of mat to the drawContours method, you can use

Mat mask = Mat.zeros(result.rows(),result.cols(),result.type());

Another advise would be to load opencv before the onCreate by using a static block:

static {
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    }
}
answered on Stack Overflow Sep 12, 2013 by Ran Wakshlak • edited Sep 12, 2013 by Ran Wakshlak

User contributions licensed under CC BY-SA 3.0