Ask Your Question

Area of a single pixel object in OpenCV

asked 2012-06-29 09:38:18 -0600

Abid Rahman K gravatar image


My question seems to be simple.

Consider I have a black image with a single white pixel in it. When I find the contour, its location is returned.

But when I find its area, it returns 0.0. Why is that ? Isn't should be '1'?

And result is same with using moments.

I thought area is number of pixels that comprises that contour. But now it is not.

Can anyone clear this to me?

Regards Abid Rahman K

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2012-07-05 06:13:38 -0600

Michael Burdinov gravatar image

updated 2012-07-05 07:03:38 -0600

The area of polygon is 0 instead of 1, because the polygon is not a square with 1 pixel edge, but a point. This happened because polygon returned by findContours() is the polygon that connects centers of neighbor edge pixels (and there is a very good reason for this behavior). Your object has only one pixel and so returned polygon has only one vertex.

In general polygons returned by findContours() are not exact, and thus their area will almost always somewhat different from number of white pixels.

edit flag offensive delete link more


and there is a very good reason for this behavior -- Can you elaborate it?

Abid Rahman K gravatar imageAbid Rahman K ( 2012-07-05 09:37:22 -0600 )edit

There is a trade-off between "accurate" contour vs "useful & efficient" contour. Here an example: assume the image is split in two parts by diagonal line. According to image exact separating contour is a huge number of "stairs", i.e. edges whose length is one pixel. Another option for separating contour is a diagonal line. Line is not an exact separation, but it is much better result for most applications. It is more informative (imagine calculation of perimeter for exact separation), and it is much more efficient both in time and space. This is just a small example, whole discussion of what polygon should be extracted from an image is much more complicate than that.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-07-05 16:06:11 -0600 )edit

answered 2012-06-30 13:15:38 -0600

Documentation of cv::contourArea() says:

the area is computed using the Green formula. Thus, the returned area and the number of non-zero pixels, if you draw the contour using drawContours() or fillPoly() , can be different.

edit flag offensive delete link more


Yeah, I have read that and I have mentioned it in question. I couldn't find what is green formula or whatever i found doesn't seem understandable and dont even know if it is correct. So my question is, if area of a single pixel is not one, then how it is calculated?

Abid Rahman K gravatar imageAbid Rahman K ( 2012-07-01 10:11:20 -0600 )edit

+1 - for the second link. that page is a good observation. Thank you!!!

Abid Rahman K gravatar imageAbid Rahman K ( 2012-07-05 09:38:45 -0600 )edit

answered 2012-07-01 10:49:07 -0600

AlexanderShishkov gravatar image

You can read about Green formula in wikipedia:'s_theorem The area of the contour is computed in continuous 2D space, so this area does not mean "number of pixels". If you need calculate number of pixels, you can set an ROI around the contour and compute the area with CountNonZero applied on this area.

edit flag offensive delete link more


Isn't it wrong to say its area is zero? since it occupies some part of image. For example, for a 10x10 image, it occupies 1% of the image.

Abid Rahman K gravatar imageAbid Rahman K ( 2012-07-01 12:22:12 -0600 )edit

answered 2012-06-30 12:49:33 -0600

Maybe it's the area inside the contour? What happens if you have 8 white pixels surrounding a black pixel or something similar?

edit flag offensive delete link more

Question Tools


Asked: 2012-06-29 09:38:18 -0600

Seen: 8,789 times

Last updated: Jul 05 '12