java - 如何解决OpenCv绘制轮廓错误

标签 java android opencv

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

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)

程序是

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

这里有什么问题吗?

最佳答案

问题是您为drawContours方法提供了错误类型的垫子,您可以使用

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

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

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

关于java - 如何解决OpenCv绘制轮廓错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18743991/

相关文章:

java - 如何定义 GOOGLE_APPLICATION_CREDENTIALS?

java - 如果屏幕上显示多个二维码,请扫描二维码

android - 使用 WorkManager 和 Notification Service 在服务器上上传媒体

android - 更改设备的语言设置(区域设置)

android - 我希望能够将数据从 kinect 发送到 android 应用程序

opencv - 在尝试复制 Bit16 Mat 时,实际上只有一半被复制

java - web.xml 文件是否应该受版本控制?

java - 为什么 indexof 对于在 java 中转换为列表的数组会失败?

opencv - HoughLines(opencv)之前的Canny有什么用?

c++ - 循环缓冲区的线程安全实现