Added common utility (getContourAreaPercentInsideMask) to utility class

This commit is contained in:
Matt Hill
2014-10-16 21:38:18 -04:00
parent 602b20c883
commit 2e83a057c1
2 changed files with 34 additions and 0 deletions

View File

@@ -17,6 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opencv2/core/core.hpp>
#include "utility.h"
using namespace cv;
@@ -379,6 +381,36 @@ LineSegment LineSegment::getParallelLine(float distance)
return result;
}
// Given a contour and a mask, this function determines what percentage of the contour (area)
// is inside the masked area.
float getContourAreaPercentInsideMask(cv::Mat mask, std::vector<std::vector<cv::Point> > contours, std::vector<cv::Vec4i> hierarchy, int contourIndex)
{
Mat innerArea = Mat::zeros(mask.size(), CV_8U);
drawContours(innerArea, contours,
contourIndex, // draw this contour
cv::Scalar(255,255,255), // in
CV_FILLED,
8,
hierarchy,
2
);
int startingPixels = cv::countNonZero(innerArea);
//drawAndWait(&innerArea);
bitwise_and(innerArea, mask, innerArea);
int endingPixels = cv::countNonZero(innerArea);
//drawAndWait(&innerArea);
return ((float) endingPixels) / ((float) startingPixels);
}
std::string toString(int value)
{

View File

@@ -100,6 +100,8 @@ float angleBetweenPoints(cv::Point p1, cv::Point p2);
cv::Size getSizeMaintainingAspect(cv::Mat inputImg, int maxWidth, int maxHeight);
float getContourAreaPercentInsideMask(cv::Mat mask, std::vector<std::vector<cv::Point> > contours, std::vector<cv::Vec4i> hierarchy, int contourIndex);
cv::Mat equalizeBrightness(cv::Mat img);
cv::Rect expandRect(cv::Rect original, int expandXPixels, int expandYPixels, int maxX, int maxY);