Quantcast
Channel: OpenCV Q&A Forum - Latest question feed
Viewing all articles
Browse latest Browse all 19555

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

$
0
0
I started by reading in this `Mat`. [![enter image description here][1]][1] Then I converted it to Greyscale and applied `Imgproc.canny()` to it, getting the following mask. [![enter image description here][2]][2] Then I used `Imgproc.findContours()` to find the contours, `Imgproc.drawContours()`, and `Core.putText()` to label the contours with numbers: [![enter image description here][3]][3] 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][4]][4] **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 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][5]][5] **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);`][6], 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][7]][7] ![image description](http://i.stack.imgur.com/P4WKW.png) 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](http://i.stack.imgur.com/zpQT9.png) ![image description](http://i.stack.imgur.com/jQcio.png) 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](http://i.stack.imgur.com/nujp0.png) ![image description](http://i.stack.imgur.com/bN6ym.png) **NOW I am completely Unable to Understand what is happening here.** [1]: http://i.stack.imgur.com/OHInM.png [2]: http://i.stack.imgur.com/T0enC.png [3]: http://i.stack.imgur.com/9KAGV.png [4]: http://i.stack.imgur.com/G1WwH.png [5]: http://i.stack.imgur.com/1bS3Y.png [6]: http://docs.opencv.org/java/2.4.9/org/opencv/core/Mat.html [7]: http://i.stack.imgur.com/IzTgZ.png [8]: http://i.stack.imgur.com/zpQT9.png [9]: http://i.stack.imgur.com/nujp0.png

Viewing all articles
Browse latest Browse all 19555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>