如何解决OpenCv的绘图轮廓误差

时间:2013-09-11 14:29:48

标签: java android opencv

基本上,我试图用颜色绘制第一个轮廓。但是这个程序因以下错误崩溃了:

  

9-11 09:56:38.230:D / dalvikvm(1920):GC_FOR_ALLOC释放71K,10%免费2824K / 3124K,暂停0ms,总计3ms

     

09-11 09:56:38.340:D / dalvikvm(1920):GC_FOR_ALLOC释放379K,17%免费2958K / 3564K,暂停3ms,总计4ms

     

09-11 09:56:38.360:D / dalvikvm(1920):GC_FOR_ALLOC释放107K,10%免费3361K / 3696K,暂停2ms,总计3ms

     

09-11 09:56:38.390:D / dalvikvm(1920):GC_FOR_ALLOC释放170K,10%免费3702K / 4100K,暂停4ms,总计4ms

     

09-11 09:56:38.420:E / cv :: error()(1920):OpenCV错误: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):致命信号11(SIGSEGV)位于0x00000000(代码= 1),线程1920(pencvratstudy01)

计划

@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);


}

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

问题是你给drawContours方法提供了错误类型的mat,你可以使用

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

另一个建议是使用静态块在onCreate之前加载opencv:

static {
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    }
}