Ask Your Question

How to get number of contours

asked 2019-10-28 16:44:30 -0600

Neikonice gravatar image

I would like to determine the number of white areas in this picture. I tried this with the findContours function to solve where all surfaces are detected but i dont know how to get the number of all contours. I thought the number is in the hierarchy but the size of hierarchy is only 24 and there are 33 contours in the image.

This is the code is used:

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

image description

edit retag flag offensive close merge delete



contours.size() : read doc

LBerger gravatar imageLBerger ( 2019-10-28 16:46:21 -0600 )edit

if i use this command i get the number 1077

Neikonice gravatar imageNeikonice ( 2019-10-28 16:49:37 -0600 )edit

Please post your whole code.

sjhalayka gravatar imagesjhalayka ( 2019-10-28 20:14:42 -0600 )edit

1 answer

Sort by » oldest newest most voted

answered 2019-10-29 05:39:14 -0600

fogx gravatar image

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 to see what is being detected?

edit flag offensive delete link more


hi there, i changed the link in your (nice) answer. please do not use or recommend 2.4 docs, they're no more maintained.

berak gravatar imageberak ( 2019-10-29 10:20:14 -0600 )edit

First thank you for the detailed answer. I have implemented your idea to draw the contours but apparently no contours are lost. There are still 33 but if i cout the size of contours the result is 24.

Mat drawing = Mat::zeros( image.size(), CV_8UC3 );

int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
    drawContours( drawing, contours, idx, Scalar(0,0,255), 2, 8, hierarchy );

cout << sizeof(contours)<< endl;
Neikonice gravatar imageNeikonice ( 2019-10-29 15:41:31 -0600 )edit

Question Tools

1 follower


Asked: 2019-10-28 16:44:30 -0600

Seen: 3,534 times

Last updated: Oct 29 '19