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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 27, 2016 at 8:31
\$\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.

forsvarir
11.8k7 gold badges39 silver badges72 bronze badges
answered Mar 3, 2017 at 13:12
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.