Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

From the documentation:

C++: void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())

contours – Detected contours. Each contour is stored as a vector of points.

hierarchy – Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i] , the elements hierarchy[i][0] , hiearchy[i][1] , hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.

So Hierarchies contains information about the image topology, and has the same number of elements as contours. Contours will contain all the detected contours. If contours isn't 30 but you expect it to be 30, then there is probably something going wrong beforehand. Why don't you try drawing the contours onto your image to see what is being detected? https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=drawcontours#drawcontours

click to hide/show revision 2
No.2 Revision

updated 2019-10-29 10:18:03 -0600

berak gravatar image

From the documentation:

C++: void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())

contours – Detected contours. Each contour is stored as a vector of points.

hierarchy – Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i] , the elements hierarchy[i][0] , hiearchy[i][1] , hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.

So Hierarchies contains information about the image topology, and has the same number of elements as contours. Contours will contain all the detected contours. If contours isn't 30 but you expect it to be 30, then there is probably something going wrong beforehand. Why don't you try drawing the contours onto your image to see what is being detected? https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=drawcontours#drawcontours

detected?

https://docs.opencv.org/master/d6/d6e/group__imgproc__draw.html#ga746c0625f1781f1ffc9056259103edbc

click to hide/show revision 3
No.3 Revision

updated 2019-10-29 10:18:55 -0600

berak gravatar image

From the documentation:

C++: void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())

contours – Detected contours. Each contour is stored as a vector of points.

hierarchy – Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i] , the elements hierarchy[i][0] , hiearchy[i][1] , hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.

So Hierarchies contains information about the image topology, and has the same number of elements as contours. Contours will contain all the detected contours. If contours isn't 30 but you expect it to be 30, then there is probably something going wrong beforehand. Why don't you try drawing the contours onto your image image to see what is being detected?

https://docs.opencv.org/master/d6/d6e/group__imgproc__draw.html#ga746c0625f1781f1ffc9056259103edbc