1
\$\begingroup\$

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++)
{
    currentarea=cv::contourArea(contoursIn[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)
        max=currentarea;      
    if (currentareaCounted>maxAreaCounted)
        maxAreaCounted=currentareaCounted;

}

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?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
  • 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.

\$\endgroup\$
0

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.