From 2e83a057c17c34b615afcdc3a29a84eb498a6a8e Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Thu, 16 Oct 2014 21:38:18 -0400 Subject: [PATCH] Added common utility (getContourAreaPercentInsideMask) to utility class --- src/openalpr/utility.cpp | 32 ++++++++++++++++++++++++++++++++ src/openalpr/utility.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/src/openalpr/utility.cpp b/src/openalpr/utility.cpp index f76f3cb..693fdef 100644 --- a/src/openalpr/utility.cpp +++ b/src/openalpr/utility.cpp @@ -17,6 +17,8 @@ * along with this program. If not, see . */ +#include + #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 > contours, std::vector 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) { diff --git a/src/openalpr/utility.h b/src/openalpr/utility.h index 4e95fa5..a2c5b16 100644 --- a/src/openalpr/utility.h +++ b/src/openalpr/utility.h @@ -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 > contours, std::vector hierarchy, int contourIndex); + cv::Mat equalizeBrightness(cv::Mat img); cv::Rect expandRect(cv::Rect original, int expandXPixels, int expandYPixels, int maxX, int maxY);