Skip to content

Commit

Permalink
normalising polygons has more control on starting point and orientati…
Browse files Browse the repository at this point in the history
…on (#117)

* normalising polygons has more control on starting point and orientation

* missing import

* isort
  • Loading branch information
PatBall1 authored Jul 26, 2023
1 parent 68424a8 commit fd199e8
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions detectree2/models/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import rasterio
from rasterio.crs import CRS
from shapely.geometry import Polygon, box, shape
from shapely.ops import unary_union
from shapely.ops import orient, unary_union


def polygon_from_mask(masked_arr):
Expand Down Expand Up @@ -405,11 +405,34 @@ def load_geopandas_dataframes(folder):


# Function to normalize and average polygons, considering weights
#def normalize_polygon(polygon, num_points):
# total_perimeter = polygon.length
# distance_between_points = total_perimeter / num_points
# points = [polygon.boundary.interpolate(i * distance_between_points) for i in range(num_points)]
# return Polygon(points)

def normalize_polygon(polygon, num_points):
# Orient polygon to ensure consistent vertex order (counterclockwise)
polygon = orient(polygon, sign=1.0)

# Get all points
points = list(polygon.exterior.coords)

# Get point with minimum average of x and y
min_avg_point = min(points, key=lambda point: sum(point)/len(point))

# Rotate points to start from min_avg_point
min_avg_point_idx = points.index(min_avg_point)
points = points[min_avg_point_idx:] + points[:min_avg_point_idx]

# Create a new polygon with ordered points
polygon = Polygon(points)

total_perimeter = polygon.length
distance_between_points = total_perimeter / num_points
points = [polygon.boundary.interpolate(i * distance_between_points) for i in range(num_points)]
return Polygon(points)

normalized_points = [polygon.boundary.interpolate(i * distance_between_points) for i in range(num_points)]
return Polygon(normalized_points)


def average_polygons(polygons, weights=None, num_points=300):
Expand Down

0 comments on commit fd199e8

Please sign in to comment.