From 510ad8e77927a1d71be14921a5717e0791f60330 Mon Sep 17 00:00:00 2001 From: Ovidiu Parvu Date: Thu, 12 Sep 2013 10:11:38 +0100 Subject: [PATCH] - Changed the type of the minEnclosingTriangle function parameters by using proxy classes InputArray/OutputArray instead of std::vector - Adapted the source code to accommodate this change --- modules/imgproc/include/opencv2/imgproc.hpp | 4 +- modules/imgproc/src/min_enclosing_triangle.cpp | 57 +++++++++++++++++++++----- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index 7011bb3..57456b7 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -1452,8 +1452,8 @@ CV_EXPORTS_W void minEnclosingCircle( InputArray points, CV_OUT Point2f& center, CV_OUT float& radius ); //! computes the minimal enclosing triangle for a convex polygon defined by at least three points -CV_EXPORTS_W void minEnclosingTriangle( const std::vector &convexPolygon, - CV_OUT std::vector &triangle, CV_OUT double& area ); +CV_EXPORTS_W void minEnclosingTriangle( InputArray convexPolygon, + CV_OUT OutputArray triangle, CV_OUT double& area ); //! matches two contours using one of the available algorithms CV_EXPORTS_W double matchShapes( InputArray contour1, InputArray contour2, diff --git a/modules/imgproc/src/min_enclosing_triangle.cpp b/modules/imgproc/src/min_enclosing_triangle.cpp index cc6e96b..7b64f65 100644 --- a/modules/imgproc/src/min_enclosing_triangle.cpp +++ b/modules/imgproc/src/min_enclosing_triangle.cpp @@ -94,9 +94,8 @@ #define VALIDATION_SIDE_B_TANGENT 1 #define VALIDATION_SIDES_FLUSH 2 -// Constant values +// Threshold value for geometrical comparisons -#define PI 3.14159265358979323846264338327950288419716939937510 #define EPSILON 1E-5 @@ -157,6 +156,10 @@ static bool areOnTheSameSideOfLine(const cv::Point2f &p1, const cv::Point2f &p2, static double areaOfTriangle(const cv::Point2f &a, const cv::Point2f &b, const cv::Point2f &c); +static void copyConvexPolygon(cv::InputArray convexPolygon); + +static void copyResultingTriangle(const std::vector &resultingTriangle, cv::OutputArray triangle); + static double distanceBtwPoints(const cv::Point2f &a, const cv::Point2f &b); static double distanceFromPointToLine(const cv::Point2f &a, const cv::Point2f &linePointB, @@ -167,6 +170,8 @@ static bool findGammaIntersectionPoints(unsigned int polygonPointIndex, const cv const cv::Point2f &side2EndVertex, cv::Point2f &intersectionPoint1, cv::Point2f &intersectionPoint2); +static void findMinEnclosingTriangle(std::vector &triangle, double& area); + static void findMinimumAreaEnclosingTriangle(std::vector &triangle, double &area); static cv::Point2f findVertexCOnSideB(); @@ -262,12 +267,42 @@ static void updateSidesCA(); * @param triangle Minimum area triangle enclosing the given polygon * @param area Area of the minimum area enclosing triangle */ -void cv::minEnclosingTriangle( const std::vector &convexPolygon, - CV_OUT std::vector &triangle, CV_OUT double& area ) { +void cv::minEnclosingTriangle(cv::InputArray convexPolygon, + CV_OUT cv::OutputArray triangle, CV_OUT double& area) { + std::vector resultingTriangle; + + copyConvexPolygon(convexPolygon); + findMinEnclosingTriangle(resultingTriangle, area); + copyResultingTriangle(resultingTriangle, triangle); +} + + +/////////////////////////////// Helper functions definition ////////////////////////////// + + +//! Copy the provided convex polygon to the global variable "polygon" +/*! +* @param convexPolygon The provided convex polygon +*/ +static void copyConvexPolygon(cv::InputArray convexPolygon) { + cv::Mat convexPolygonMat = convexPolygon.getMat(); + + convexPolygonMat.copyTo(polygon); +} + +//! Find the minimum enclosing triangle and its area for the given polygon +/*! +* The overall complexity of the algorithm is theta(n) where "n" represents the number +* of vertices in the convex polygon +* +* @param convexPolygon Convex polygon defined by at least three points +* @param triangle Minimum area triangle enclosing the given polygon +* @param area Area of the minimum area enclosing triangle +*/ +static void findMinEnclosingTriangle( std::vector &triangle, double& area) { // Check if the polygon is convex and is a k-gon with k > 3 - CV_Assert(isContourConvex(convexPolygon) && (convexPolygon.size() > 3)); + CV_Assert(isContourConvex(polygon) && (polygon.size() > 3)); - polygon = convexPolygon; area = std::numeric_limits::max(); // Clear all points previously stored in the vector @@ -278,9 +313,11 @@ void cv::minEnclosingTriangle( const std::vector &convexPolygon, findMinimumAreaEnclosingTriangle(triangle, area); } - -/////////////////////////////// Helper functions definition ////////////////////////////// - +//! Copy resultingTriangle to the OutputArray triangle +static void copyResultingTriangle(const std::vector &resultingTriangle, + cv::OutputArray triangle) { + cv::Mat(resultingTriangle).convertTo(triangle, triangle.fixedType() ? triangle.type() : CV_32F); +} //! Initialisation function static void initialise() { @@ -847,7 +884,7 @@ static double angleOfLineWrtOxAxis(const cv::Point2f &a, const cv::Point2f &b) { double y = b.y - a.y; double x = b.x - a.x; - double angle = (std::atan2(y, x) * 180 / PI); + double angle = (std::atan2(y, x) * 180 / CV_PI); return (angle < 0) ? (angle + 360) : angle; -- 2.7.4