Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why is the drawContour() in OpenCV generating this strange Mask?

I started by reading in this Mat.

enter image description here

Then I converted it to Greyscale and applied Imgproc.canny() to it, getting the following mask.

enter image description here

Then I used Imgproc.findContours() to find the contours, Imgproc.drawContours(), and Core.putText() to label the contours with numbers:

enter image description here

Then I did Rect boundingRect = Imgproc.boundingRect(contours.get(0)); Mat submatrix = new Mat(); submatrix = originalMat.submat(boundingRect); to get following submatrix:

enter image description here

So far so good. The Problem starts hereafter:

NOW I NEEDED A MASK OF THE submatrix. So I decided to use Imgproc.drawContours() to get the mask:

Mat mask = new Mat(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1);
        List<MatOfPoint> contourList = new ArrayList<>();
        contourList.add(contours.get(0));
        Imgproc.drawContours(mask, contourList, 0, new Scalar(255), -1);

I got the following mask:

enter image description here

WHAT I WAS EXPECTING was a filled (in white color) diamond shape on black background.

WHy am I getting this unexpected result?

Why is the drawContour() in OpenCV generating this strange Mask?

I started by reading in this Mat.

enter image description here

Then I converted it to Greyscale and applied Imgproc.canny() to it, getting the following mask.

enter image description here

Then I used Imgproc.findContours() to find the contours, Imgproc.drawContours(), and Core.putText() to label the contours with numbers:

enter image description here

Then I did Rect boundingRect = Imgproc.boundingRect(contours.get(0)); Mat submatrix = new Mat(); submatrix = originalMat.submat(boundingRect); to get following submatrix:

enter image description here

So far so good. The Problem starts hereafter:

NOW I NEEDED A MASK OF THE submatrix. So I decided to use Imgproc.drawContours() to get the mask:

Mat mask = new Mat(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1);
        List<MatOfPoint> contourList = new ArrayList<>();
        contourList.add(contours.get(0));
        Imgproc.drawContours(mask, contourList, 0, new Scalar(255), -1);

I got the following mask:

enter image description here

WHAT I WAS EXPECTING was a filled (in white color) diamond shape on black background.

WHy am I getting this unexpected result?


EDIT:

  1. When I replaced Mat mask = new Mat(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1); by Mat mask = Mat.zeros(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1);, the last mask with white colored garbage was replaced by an empty black mask withOUT any white color on it. I got the following submat and mask:

    enter image description here image description

  2. I was getting the first contour in the list of contours (named contours) by contours.get(0), and using this first contour to calculate Imgproc.boundingRect() as well as in contourList.add(contours.get(0)); later (where contourList is the list of just one contour which will be used in the last drawContours()).

    Then I went ahead to change contours.get(0) to contours.get(1) in Imgproc.boundingRect() as well as in contourList.add(); (just before Imgproc.drawContours()). That resulted in this submat and mask:

    enter image description here image description

  3. Then I changed back to contours.get(0) in Imgproc.boundingRect(); and let contourList.add(contours.get(1)); be there. Got the following submat and mask:

    image description image description

NOW I am completely Unable to Understand what is happening here.

Why is the drawContour() in OpenCV generating this strange Mask?

I started by reading in this Mat.

enter image description here

Then I converted it to Greyscale and applied Imgproc.canny() to it, getting the following mask.

enter image description here

Then I used Imgproc.findContours() to find the contours, Imgproc.drawContours(), and Core.putText() to label the contours with numbers:

enter image description here

Then I did Rect boundingRect = Imgproc.boundingRect(contours.get(0)); Mat submatrix = new Mat(); submatrix = originalMat.submat(boundingRect); to get following submatrix:

enter image description here

So far so good. The Problem starts hereafter:

NOW I NEEDED A MASK OF THE submatrix. So I decided to use Imgproc.drawContours() to get the mask:

Mat mask = new Mat(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1);
        List<MatOfPoint> contourList = new ArrayList<>();
        contourList.add(contours.get(0));
        Imgproc.drawContours(mask, contourList, 0, new Scalar(255), -1);

I got the following mask:

enter image description here

WHAT I WAS EXPECTING was a filled (in white color) diamond shape on black background.

WHy am I getting this unexpected result?


EDIT:

  1. When I replaced Mat mask = new Mat(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1); by Mat mask = Mat.zeros(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1);, the last mask with white colored garbage was replaced by an empty black mask withOUT any white color on it. I got the following submat and mask:

    enter image description here image description

  2. I was getting the first contour in the list of contours (named contours) by contours.get(0), and using this first contour to calculate Imgproc.boundingRect() as well as in contourList.add(contours.get(0)); later (where contourList is the list of just one contour which will be used in the last drawContours()).

    Then I went ahead to change contours.get(0) to contours.get(1) in Imgproc.boundingRect() as well as in contourList.add(); (just before Imgproc.drawContours()). That resulted in this submat and mask:

    enter image description hereimage description image description

  3. Then I changed back to contours.get(0) in Imgproc.boundingRect(); and let contourList.add(contours.get(1)); be there. Got the following submat and mask:

    image description image description

NOW I am completely Unable to Understand what is happening here.

Why is the drawContour() in OpenCV generating this strange Mask?

I started by reading in this a Mat.

enter image description here

Then I converted it to Greyscale and applied Imgproc.canny() to it, getting the following mask.

enter image description here

Then I used Imgproc.findContours() to find the contours, Imgproc.drawContours(), and Core.putText() to label the contours with numbers:

enter image description herenumbers

Then I did Rect boundingRect = Imgproc.boundingRect(contours.get(0)); Mat submatrix = new Mat(); submatrix = originalMat.submat(boundingRect); to get following submatrix:

enter image description here

So far so good. The Problem starts hereafter:

NOW I NEEDED A MASK OF THE submatrix. So I decided to use Imgproc.drawContours() to get the mask:

Mat mask = new Mat(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1);
        List<MatOfPoint> contourList = new ArrayList<>();
        contourList.add(contours.get(0));
        Imgproc.drawContours(mask, contourList, 0, new Scalar(255), -1);

I got the following mask:

enter image description hereenter image description here

WHAT I WAS EXPECTING was a filled (in white color) diamond shape on black background.

WHy am I getting this unexpected result?


EDIT:

  1. When I replaced Mat mask = new Mat(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1); by Mat mask = Mat.zeros(submatrix.rows(), submatrix.cols(), CvType.CV_8UC1);, the last mask with white colored garbage was replaced by an empty black mask withOUT any white color on it. I got the following submat and mask:

    enter image description here image description

  2. I was getting the first contour in the list of contours (named contours) by contours.get(0), and using this first contour to calculate Imgproc.boundingRect() as well as in contourList.add(contours.get(0)); later (where contourList is the list of just one contour which will be used in the last drawContours()).

    Then I went ahead to change contours.get(0) to contours.get(1) in Imgproc.boundingRect() as well as in contourList.add(); (just before Imgproc.drawContours()). That resulted in this submat and mask:

    image description image description

  3. Then I changed back to contours.get(0) in Imgproc.boundingRect(); and let contourList.add(contours.get(1)); be there. Got the following submat and mask:

    image description image description

NOW I am completely Unable to Understand what is happening here.