Ask Your Question

Contours. What are those really?

asked 2015-01-19 20:47:38 -0500

KansaiRobot gravatar image

updated 2015-01-19 23:32:15 -0500

Hello. I am new to OpenCV but I am practicing a lot lately. I quite like it.

Please pardon my confused state of mind but sometimes understanding the theory behind the algorithms is quite mind blowing.

I am currently working with watershed, and the required markers. It seems that to find these markers automatically we use findContours and drawContours. After doing this I tried to show them in a window but I got a black window, so I can't figure it out what are those really, I mean visually. (Intuitively I suppose they are what they say "contours", but why I don't see them??)

Strange as it sounds, in some examples contours are quite easily shown but in others it is not possible.

Anyway, I used step #5 of this tutorial


to be able to "see" the contours.

Anyway, my question is, what are contours really? I mean, I know they are a vector of vectors. But then when I use drawContourns to generate markers for my watershed, do they generate "labels"?? Numbers? Each contour generates a different label??

Thousand thanks for any help :)

The reason I ask this is because in the strategy for watershed, it tell us that an image has to have "labels" and unknown portions (to be watershed) should be labeled "0" and to do this they use contours. So I am perplexed at the concept of contours and markers

See for reference for example


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-01-27 06:08:09 -0500

updated 2015-01-27 06:43:28 -0500

What is a contour?

A contour is basically a set of points that are connected to each other. Opencv's function cv::findCountours returns a vector with as many elements as there are isolated sets of connected pixels in the image, and this is also known as a 'blob'. Each of this elements in the vector is no more than a list of Points that contain the (x,y) coordinates of each pixel in the outer contour of the blob.

Why are these contours so useful?

Blob analysis is a very important area of computer vision and is used in many applications (such as part inspection, simple object detection and much more). Many applications consist in treating the raw image in such a way that the objects of interest that are to be examined become white blobs, and everything that is not an object of interest (background / other objects) are black. An image in which the objects of interest are white blobs and everything else is black is called a binary image.

Blobs are used so much because they contain a lot of information. OpenCV has a lot of functions that output important information about those, and here are some examples:

cv::Point calculateBlobCentroid(const std::vector<cv::Point> blob)
        cv::Moments mu = cv::moments(blob);
        cv::Point centroid = cv::Point (mu.m10/mu.m00 , mu.m01/mu.m00);

        return centroid;

This function returns the centroid of a blob.

arcLength(InputArray curve, bool closed)

Returns the perimeter of the blob

boundingRect(InputArray points)

Returns a rectangle that encloses the blob

contourArea(InputArray contour, bool oriented=false )

Returns the area of the blob (number of white pixels).

You can check all the information you can get from a contour in the following link .

About the trouble you're having with watershed, please check this link, where you can find a very nice tutorial on how to apply that method and and how it works.

Good luck

edit flag offensive delete link more



Each of this elements in the vector is no more than a list of Points that contain the (x,y) coordinates of each pixel in the blob. --> afaik this is not true. Doesn't a contour only contain the outer pixels of a blob?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-27 06:34:25 -0500 )edit

Ups, my bad! You are right. I'll edit my answer

Pedro Batista gravatar imagePedro Batista ( 2015-01-27 06:42:41 -0500 )edit

And maybe you should add a little explanation about inner and outer contours? Meaning that are 2 blobs are selfcontained that they are in hierarchical order?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-27 06:48:15 -0500 )edit

Never really explored how that works, so I don't know how to explain that too well.

Pedro Batista gravatar imagePedro Batista ( 2015-01-27 07:15:10 -0500 )edit

No problem, if author has a question about it I will jump in!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-27 07:53:21 -0500 )edit

Thank you Pedro and Steven. As you can see in this question I am using the concept of contours, so I would like to understand the hierarchical order as well.

This time my problem is that I want to keep some contours and discard others. Thank you

KansaiRobot gravatar imageKansaiRobot ( 2015-02-18 02:02:55 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-01-19 20:47:38 -0500

Seen: 492 times

Last updated: Jan 27 '15