1 | initial version |
The area of polygon is 0 instead of 1, because the polygon you are measured is not what you think. If the image has single white pixel, then the polygon found by findContours() is just a point, not a square with 1 pixel edge. This happened because polygon returned by findContours() is the polygon that connects centers of neighbor edge pixels. Your object has only one pixel and so returned polygon has only one point.
There a very good reason for this behavior - it is very intuitive. Imagine that someone drawed rhombus 1000x1000 in image (assume for simplicity that the only values in image are 0 and 255). When you extract contours of it you would like to get polygon with just 4 edges. Polygon that give exact separation between inside and outside of rhombus will have 2000 edges, one pixel long each. Time and space overhead for creation of such polygon may be unacceptable. And such polygon usually quite useless.
Conclusion: Polygon returned by findContours() is not exact, and thus its area will almost always somewhat different from number of pixels in object it defines.
2 | No.2 Revision |
The area of polygon is 0 instead of 1, because the polygon you are measured is not what you think. If the image has single white pixel, then the polygon found by findContours() is just a point, not a square with 1 pixel edge. edge, but a point. This happened because polygon returned by findContours() is the polygon that connects centers of neighbor edge pixels. pixels (and there is a very good reason for this behavior). Your object has only one pixel and so returned polygon has only one point.vertex.
There a very good reason for this behavior - it is very intuitive. Imagine that someone drawed rhombus 1000x1000 in image (assume for simplicity that the only values in image are 0 and 255). When you extract contours of it you would like to get polygon with just 4 edges. Polygon that give exact separation between inside and outside of rhombus will have 2000 edges, one pixel long each. Time and space overhead for creation of such polygon may be unacceptable. And such polygon usually quite useless.
Conclusion:
Polygon In general polygons returned by findContours() is are not exact, and thus its their area will almost always somewhat different from number of pixels in object it defines.white pixels.