# Different method to calculate blob area in OpenCv

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?

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