I'm trying to use a different method to calculate a blob area from contourArea, using the countNonZero function. I want to see if there is significant approximation introduced by contourArea function, and also be able to calculate area of contour with auto-intersection.

cv::findContours(drawingIn, contoursIn, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);

double max=0;
double currentarea=0;
int currentareaCounted = 0;
int maxAreaCounted = 0;

for (unsigned int i=0;i<contoursIn.size();i++)
    cv::Mat drawingMatCount = cv::Mat::zeros(ROI_INTERACTION,ROI_INTERACTION, CV_8UC1);
    cv::drawContours(drawingMatCount, contoursIn, i, 255, -1, 8);
    currentareaCounted = cv::countNonZero(drawingMatCount);

    if (currentarea>max)
    if (currentareaCounted>maxAreaCounted)


Is there something that could be improved? This code works, but my doubt is about declaring a new matrix every iteration. Could this lead to some performance issue?


1 Answer 1

  • Declare drawingMatCount once before the loop
  • cv::drawContours(drawingMatCount, contoursIn, i, 0, -1, 8); at the end of the loop

BTW: this is not the blob area, its the contour area, including the holes.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.