From eb3317694eacb958596371303c11da28513e30c8 Mon Sep 17 00:00:00 2001 From: sharingan000 Date: Thu, 4 Apr 2024 14:57:57 +1000 Subject: [PATCH] Include all three algorithms --- supervision/geometry/utils.py | 60 +++++++++++++++++++++++++++++++---- test/geometry/test_utils.py | 54 +++++++++++++++++++++---------- 2 files changed, 90 insertions(+), 24 deletions(-) diff --git a/supervision/geometry/utils.py b/supervision/geometry/utils.py index 401f672e..6e293214 100644 --- a/supervision/geometry/utils.py +++ b/supervision/geometry/utils.py @@ -3,6 +3,50 @@ import numpy as np from supervision.geometry.core import Point +class PolygonCenterGetter: + + @staticmethod + def mean_algo(polygon: np.ndarray) -> Point: + """ + Calculate the center of the polygon as the center of mass + of the points. + """ + center = np.mean(polygon, axis=0).astype(int) + + return Point(x=center[0], y=center[1]) + + @staticmethod + def frame_algo(polygon: np.ndarray) -> Point: + """ + Calculate the center of the polygon as the center of mass + of the frame formed by the points of this polygon. + """ + polygon = polygon.astype(np.float32) + shifted_polygon = np.roll(polygon, 1, axis=0) + points = (shifted_polygon + polygon) / 2 + vectors = shifted_polygon - polygon + mass = np.sum(vectors ** 2, axis=1) ** 0.5 + center = np.dot(mass, points) / np.sum(mass) + center = center.astype(int) + + return Point(x=center[0], y=center[1]) + + @staticmethod + def solid_algo(polygon: np.ndarray) -> Point: + """ + Calculate the center of the polygon as the center of mass + of a solid homogeneous figure + """ + shift_polygon = np.roll(polygon, -1, axis=0) + signed_areas = np.cross(polygon, shift_polygon) / 2 + if signed_areas.sum() == 0: + center = np.mean(polygon, axis=0).astype(int) + return Point(x=center[0], y=center[1]) + centroids = (polygon + shift_polygon) / 3.0 + center = np.average(centroids, axis=0, weights=signed_areas).astype(int) + return Point(x=center[0], y=center[1]) + + def get_polygon_center(polygon: np.ndarray) -> Point: """ Calculate the center of a polygon. @@ -32,11 +76,13 @@ def get_polygon_center(polygon: np.ndarray) -> Point: Point(x=1, y=1) ``` """ - polygon = polygon.astype(np.float32) - shifted_polygon = np.roll(polygon, 1, axis=0) - points = (shifted_polygon + polygon) / 2 - vectors = shifted_polygon - polygon - mass = np.sum(vectors ** 2, axis=1) ** 0.5 - center = ((mass @ points) / np.sum(mass)).round() - return Point(x=center[0], y=center[1]) + n: int = polygon.shape[0] # amount of points + + if n <= 10 ** 2: + return PolygonCenterGetter.mean_algo(polygon) + + if n <= 10 ** 4: + return PolygonCenterGetter.solid_algo(polygon) + + return PolygonCenterGetter.frame_algo(polygon) diff --git a/test/geometry/test_utils.py b/test/geometry/test_utils.py index 5457463b..bb6ec60b 100644 --- a/test/geometry/test_utils.py +++ b/test/geometry/test_utils.py @@ -5,26 +5,46 @@ from supervision.geometry.core import Point from supervision.geometry.utils import get_polygon_center +def generate_test_polygon(n: int) -> np.ndarray: + """ + Generate a semicircle with a given number of points. + + Parameters: + n (int): amount of points in polygon + + Returns: + Polygon: test polygon in the form of a semicircle. + + Examples: + ```python + from supervision.geometry.utils import get_polygon_center + import numpy as np + + test_polygon = generate_test_data(1000) + + get_polygon_center(test_polygon) + Point(x=500, y=1212) + ``` + """ + r: int = n // 2 + x_axis = np.linspace(0, 2 * r, n) + y_axis = (r ** 2 - (x_axis - r) ** 2) ** 0.5 + 2 * r + polygon = np.array([x_axis, y_axis]).T + + return polygon + + @pytest.mark.parametrize( "polygon, expected_result", [ - (np.array([[0, 0], [0, 2], [2, 2], [2, 0]]), Point(x=1, y=1)), - (np.array([[0, 0], [3, 4], [6, 0]]), Point(x=3, y=1)), - (np.array([[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [5, 2]]), Point(x=2, y=2)), - ( - np.array([[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [4, 4], [4, 0]]), - Point(x=2, y=2), - ), - (np.array([[0, 2], [2, 4], [4, 2], [2, 0]]), Point(x=2, y=2)), - ( - np.array([[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1000]]), - Point(x=0, y=500), - ), - (np.array([[0, 0], [13, 200], [0, 150]]), Point(x=4, y=100)), - ( - np.array([[0, 0], [0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3], [3, 0]]), - Point(x=2, y=1), - ), + (generate_test_polygon(10), Point(x=5, y=13)), + (generate_test_polygon(50), Point(x=25, y=69)), + (generate_test_polygon(100), Point(x=50, y=138)), + (generate_test_polygon(1000), Point(x=500, y=1212)), + (generate_test_polygon(3000), Point(x=1499, y=3636)), + (generate_test_polygon(10000), Point(x=5000, y=12122)), + (generate_test_polygon(20000), Point(x=9999, y=23889)), + (generate_test_polygon(50000), Point(x=25000, y=59724)), ], ) def test_get_polygon_center(polygon: np.ndarray, expected_result: Point) -> None: