1

I use OpenCV 2.4.0 on Android and try to find contours in a binary imgage.

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat mIntermediateMat = new Mat();
Imgproc.Canny(img, mIntermediateMat, 50, 100);
Imgproc.findContours(mIntermediateMat, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

But the function throws a "Unrecognized or unsupported array type in function CvMat..." exception.

Also i try this Mat as input:

Mat mIntermediateMat = new Mat(height, width, CvType.CV_8UC1, new Scalar(0));

But i get the same exception.

ChHaupt
  • 363
  • 3
  • 6
  • 17

2 Answers2

1

Try this function to find contours :

public static ArrayList<Rect> detection_contours(Mat outmat) {
    Mat v = new Mat();
    Mat vv = outmat.clone();
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(vv, contours, v, Imgproc.RETR_LIST,
            Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = 100;
    int maxAreaIdx = -1;
    Rect r = null;
    ArrayList<Rect> rect_array = new ArrayList<Rect>();

    for (int idx = 0; idx < contours.size(); idx++) {
        Mat contour = contours.get(idx);
        double contourarea = Imgproc.contourArea(contour);
        if (contourarea > maxArea) {
            // maxArea = contourarea;
            maxAreaIdx = idx;
            r = Imgproc.boundingRect(contours.get(maxAreaIdx));
            rect_array.add(r);
          //  Imgproc.drawContours(imag, contours, maxAreaIdx, new Scalar(0,0, 255));
        }

    }

    v.release();

    return rect_array;

}
Joker
  • 11
  • 2
0

Make sure that after the Canny() call, your mIntermediateMat is of type CvType.CV_8*

Rui Marques
  • 8,567
  • 3
  • 60
  • 91
  • But the Mat mIntermediateMat = new Mat(height, width, CvType.CV_8UC1, new Scalar(0)); should work? – ChHaupt Sep 18 '12 at 14:09
  • It should if it is after canny. Please post the complete error message if you still have problems. – Rui Marques Sep 18 '12 at 16:13
  • After the Canny the image type is CV_8UC1. And Mat mIntermediateMat = new Mat(height, width, CvType.CV_8UC1, new Scalar(0)); replaced the canny. The complete error message is: "CvException [org.opencv.core.CvException: ../modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function CvMat* cvGetMat(const CvArr*, CvMat*, int*, int)" – ChHaupt Sep 18 '12 at 16:16