# Area of a single pixel object in OpenCV

Hi,

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 close merge delete

Sort by ยป oldest newest most voted

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.

more

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

( 2012-07-05 09:37:22 -0500 )edit
3

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.

( 2012-07-05 16:06:11 -0500 )edit

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.

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?

( 2012-07-01 10:11:20 -0500 )edit
1

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

( 2012-07-05 09:38:45 -0500 )edit

You can read about Green formula in wikipedia: http://en.wikipedia.org/wiki/Green'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.

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.

( 2012-07-01 12:22:12 -0500 )edit

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

more

Official site

GitHub

Wiki

Documentation