Ask Your Question
0

program crashes with opencv error while trying to call draw contour

asked 2013-09-11 09:54:26 -0600

rat gravatar image

updated 2013-09-11 15:55:31 -0600

Moster gravatar image

0 down vote favorite

Basically I am trying to draw first contour with a colour. But this program crashes with 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);

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-11 14:15:36 -0600

Moster gravatar image

updated 2013-09-11 14:22:14 -0600

The problem is that you do not correctly initialize the Mat you want to draw on. You currently want to draw on an empty Mat. That makes no sense. You can take the size of any img Mat you have (result, tmpMat, canny) and initialize the Mat with zeros.

 Mat mask = Mat.zeros( canny.size(), CV_8UC3);
 Imgproc.drawContours(mask, contours, 0 , new Scalar(255))

Btw, I noticed an error. You convert from RGB2BGRA, but then only from BGR2GRAY. Maybe just convert RGB2BGR first.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-09-11 09:54:26 -0600

Seen: 1,868 times

Last updated: Sep 11 '13