0

我正在开发一个应用程序,我必须从相机拍照,然后检测图片中的边缘正方形(像文档页面。)......经过长时间的搜索,我找到了 OpenCV 库来实现这一点,我已经成功导入android的java库,但问题是当我调用opencv的方法来检测Square(方法是

Imgproc.findContours(converted, contours,hierarchy,Imgproc.CHAIN_APPROX_SIMPLE,Imgproc.RETR_LIST) )..它给了我

异常... OpenCV 错误: _CvContourScanner* cvStartFindContours(void*, CvMemStorage*, int, int, int, CvPoint), 文件 /home/reports/ci/ 中不支持的格式或格式组合(FindContours 仅支持 8uC1 和 32sC1 图像) slave/50-SDK/opencv/modules/imgproc/src/contours.cpp,第 196 行

我正在向您发送一些代码------------------------------------------ ------------

公共无效转换图像(){

    Mat ori = new Mat();
    Mat converted = new Mat(200, 200, CvType.CV_8UC1, new Scalar(0));

    try {
        ori = Utils.loadResource(MainActivity.this, R.drawable.ic_launcher, Highgui.CV_LOAD_IMAGE_COLOR);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Imgproc.cvtColor(ori,  converted, Imgproc.COLOR_RGB2GRAY, 4);  // convert Image to grayscale
    Imgproc.threshold(ori, converted, 50, 250, Imgproc.ADAPTIVE_THRESH_MEAN_C); // threshold the image

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>(10);
    Mat hierarchy = new Mat(200, 200, CvType.CV_32FC1, new Scalar(0));

    Imgproc.findContours(converted, contours, hierarchy,Imgproc.CHAIN_APPROX_SIMPLE,Imgproc.RETR_LIST);

    ImageView frame = (ImageView) findViewById(R.id.imageView1);

    Imgproc.cvtColor(converted, converted, Imgproc.COLOR_GRAY2RGBA, 4); // convert Image back to RGB
    Bitmap bmp = Bitmap.createBitmap(converted.cols(), converted.rows(), Bitmap.Config.ARGB_8888);




    frame.setImageBitmap(bmp);
    }

任何帮助将不胜感激-----------------提前致谢

4

3 回答 3

3

In 'cvFindCounters' The first argument is the input image; this image should be an 8-bit single-channel image and will be interpreted as binary. so Instead of passing a 4 channel image , you should be passing a single channel image.

this should work for you.
Imgproc.cvtColor(ori, converted, Imgproc.COLOR_RGB2GRAY, 1);

于 2015-01-02T05:15:29.640 回答
0

尝试使用

Imgproc.cvtColor(ori,  converted, Imgproc.COLOR_RGB2GRAY, 1);
Imgproc.cvtColor(converted, converted, Imgproc.COLOR_GRAY2RGBA, 1);

代替

Imgproc.cvtColor(ori,  converted, Imgproc.COLOR_RGB2GRAY, 4);
Imgproc.cvtColor(converted, converted, Imgproc.COLOR_GRAY2RGBA, 4);

因为您需要使用 1 个频道。

于 2013-05-23T09:44:55.613 回答
0

首先将位图加载为 -

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.ic_launcher);

然后将位图转换为 Mat -

Mat m = new Mat();
Utils.bitmapToMat(icon, m);
Imgproc.cvtColor(m, m, Imgproc.COLOR_RGB2GRAY, 1);

如上所述执行你的阈值和findcontours..基本想法是将Bitmap转换为单通道的Mat..

于 2013-05-23T12:46:22.550 回答