paste_zayo

Inscribed_rectangle_1

Feb 27th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import geopandas as gpd
  2. import numpy as np
  3. import cv2 as cv
  4. import largestinteriorrectangle as lir
  5.  
  6. # Read the dataset containing the polygons
  7. data = gpd.read_file(
  8.     r"C:\Working_desktop\GATE_stuff\Location_allocation\New_data_07022024\Test_processes\test_set_singlepart.gpkg")
  9.  
  10. # Initialize an empty image
  11. img = np.zeros((1000, 1000, 3), dtype="uint8")
  12.  
  13. # Loop through each polygon in the dataset
  14. for index, row in data.iterrows():
  15.     # Extract the polygon coordinates and convert to float64
  16.     polygon = np.array(row['geometry'].exterior.coords, dtype=np.float64)
  17.  
  18.     # Calculate the largest inscribed rectangle for the polygon
  19.     rectangle = lir.lir(polygon)
  20.  
  21.     # Draw the polygon on the image
  22.     cv.polylines(img, [polygon.astype(int)], True, (0, 255, 0), 1)
  23.  
  24.     # Draw the largest inscribed rectangle on the image
  25.     cv.rectangle(img, tuple(rectangle[0]), tuple(rectangle[1]), (0, 0, 255), 1)
  26.  
  27. # Display the image
  28. cv.imshow('Polygons with Inscribed Rectangles', img)
  29. cv.waitKey(0)
  30. cv.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment