Ask Your Question
0

OpenCV Error in findContours()

asked 2017-07-17 18:02:46 -0600

nish gravatar image

I'm trying to find the contours of an image. Though I tried in different ways, same error (that is "Unsupported format or combination of formats ([Start] FindContours support only 8uC1 and 32sC1 images) in cvStartFindContours") is given. This is my source code. How can I fix this problem. Thank you.

public class ContourDetection {

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat image = Highgui.imread("D:\\Region.bmp");

    if (image.empty() == true) {
        System.out.println("Error: no image found!");
    } else {

        List<MatOfPoint> contours = new ArrayList<>();
        Mat image_2 = new Mat(image.size(), image.depth());

        //image.convertTo(image_2, CvType.CV_32SC1);
        Imgproc.cvtColor(image, image_2, CvType.CV_8UC1);
        Imgproc.threshold(image_2, image_2, 150, 255, CV_THRESH_BINARY);

        //System.out.println(image_2.channels() + " " + image_2.depth() + CvType.CV_8U+image_2.type());
        findContours(image_2, contours, new Mat(), Imgproc.RETR_FLOODFILL, Imgproc.CHAIN_APPROX_SIMPLE);

        // Draw all the contours such that they are filled in.
        Mat contourImg = new Mat(image.size(), image.type());
        for (int i = 0; i < contours.size(); i++) {
            Imgproc.drawContours(contourImg, contours, i, new Scalar(255, 255, 255), -1);
        }

        Highgui.imwrite("D:\\debug_image.bmp", contourImg); 
    }
}

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-07-18 00:37:31 -0600

berak gravatar image

updated 2017-07-18 01:34:28 -0600

you've got the wrong flags here:

  // please do not preallocate it, and depth() is wrong
  Mat image_2 = new Mat(image.size(), image.depth()); 
  // does not what you expect
  Imgproc.cvtColor(image, image_2, CvType.CV_8UC1);

which probably should have been:

   Mat image_2 = new Mat(); // leave empty.
   Imgproc.cvtColor(image, image_2, Imgproc.COLOR_BGR2GRAY);

please have another look at the Imgproc module

edit flag offensive delete link more

Comments

I tried with that flag also but same error is coming. Thank You @berak for answering my question.

nish gravatar imagenish ( 2017-07-18 01:24:02 -0600 )edit

try a System.out.println( image_2.dump()); type() and depth() must both be 0

berak gravatar imageberak ( 2017-07-18 01:37:11 -0600 )edit

Both are 0 :(

nish gravatar imagenish ( 2017-07-18 03:38:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-07-17 18:01:21 -0600

Seen: 2,841 times

Last updated: Jul 18 '17