Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Get number of pixels within a contour

Hi,

I am looking for an efficient way to get the number of pixels within a contour.

cv::contourArea is not suited, as explained in this question. For instance, if I have a single-pixel contour, the output should be 1 (cv::contourArea outputs 0). If I have a 2-pixels wide square, the output should be 4 (cv::contourArea outputs 1), and so on.

One easy (but inefficient) solution would be to use cv::drawContour and count the number of non-zero pixels. Considering I have to do this for millions of contours, this will perform bad.

Am I missing other suitable solutions, maybe involving some manipulation of contour points or other OpenCV functions?

Thank you in advance, Alessandro