Polygon¶
-
class Polygon(exterior, interior=
[], sly_id=None, class_id=None, labeler_login=None, updated_at=None, created_at=None)[source]¶ Bases:
VectorGeometryClosed 2D polygon with exterior contour and optional interior holes. Immutable.
Polygon geometry for a single
Label.Polygonobject is immutable.- Parameters:
- exterior¶
Exterior coordinates, object contour is defined with these points.
- interior=
[]¶ Interior coordinates, object holes are defined with these points.
- sly_id : int, optional¶
Polygon ID in Supervisely server.
- class_id : int, optional¶
ID of ObjClass to which Polygon belongs.
- labeler_login : str, optional¶
Login of the user who created Polygon.
- updated_at : str, optional¶
Date and Time when Polygon was modified last. Date Format: Year:Month:Day:Hour:Minute:Seconds. Example: ‘2021-01-22T19:37:50.158Z’.
- created_at : str, optional¶
Date and Time when Polygon was created. Date Format is the same as in “updated_at” parameter.
- Raises:
ValueError – if len(exterior) < 3 or len(any element in interior list) < 3
- Usage Example:
import supervisely as sly exterior = [sly.PointLocation(730, 2104), sly.PointLocation(2479, 402), sly.PointLocation(3746, 1646)] # or exterior = [[730, 2104], [2479, 402], [3746, 1646]] # or exterior = [(730, 2104), (2479, 402), (3746, 1646)] interior = [[sly.PointLocation(1907, 1255), sly.PointLocation(2468, 875), sly.PointLocation(2679, 1577)]] # or interior = [[[730, 2104], [2479, 402], [3746, 1646]]] # or interior = [[(730, 2104), (2479, 402), (3746, 1646)]] figure = sly.Polygon(exterior, interior)
Methods
Returns the allowed transforms for the Polygon.
Approximates a Polygon curve with the specified precision.
Clone from GEOMETRYYY
Convert geometry config from json format
Convert geometry config to json format
Convert geometry to another geometry shape.
Crops current Polygon.
Draws the figure contour on a given bitmap canvas :param bitmap: np.ndarray :param color: [R, G, B] :param thickness: (int) :param config: drawing config specific to a concrete subclass, e.g. per edge colors.
Flips current VectorGeometry in horizontal.
Flips current VectorGeometry in vertical.
Convert a json dict to Polygon.
Returns the name of the geometry.
Returns 2D boolean mask of the geometry.
Get the name of the geometry.
Crops object like "crop" method, but return results with coordinates relative to rect :param rect: Rectangle for crop.
Resizes current VectorGeometry.
Rotates current VectorGeometry.
Scales current VectorGeometry.
Creates Rectangle object from current VectorGeometry.
Convert the VectorGeometry to a json dict.
Translates current VectorGeometry.
Validate geometry.
Attributes
Polygon area.
VectorGeometry exterior points.
Converts exterior attribute of VectorGeometry to numpy array.
VectorGeometry interior points.
Converts interior attribute of VectorGeometry to numpy array.
- classmethod from_json(data)[source]¶
Convert a json dict to Polygon. Read more about Supervisely format.
- Parameters:
- Returns:
Polygon from json.
- Return type:
- Usage Example:
import supervisely as sly figure_json = { "points": { "exterior": [ [2104, 730], [402, 2479], [1646, 3746] ], "interior": [ [ [1255, 1907], [875, 2468], [577, 2679] ] ] } } figure = sly.Polygon.from_json(figure_json)
- classmethod name()¶
Get the name of the geometry.
Same as
geometry_name(), but shorter. In order to make the code more concise.- Returns:
string with name of geometry
- static geometry_name()[source]¶
Returns the name of the geometry.
- Returns:
name of the geometry
- Return type:
- clone()¶
Clone from GEOMETRYYY
-
convert(new_geometry, contour_radius=
0, approx_epsilon=None)¶ Convert geometry to another geometry shape.
-
draw_contour(bitmap, color, thickness=
1, config=None)¶ Draws the figure contour on a given bitmap canvas :param bitmap: np.ndarray :param color: [R, G, B] :param thickness: (int) :param config: drawing config specific to a concrete subclass, e.g. per edge colors
- fliplr(img_size)¶
Flips current VectorGeometry in horizontal.
- Parameters:
- Returns:
Flipped VectorGeometry in horizontal.
- Return type:
- Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable height, width = 300, 400 fliplr_figure = figure.fliplr((height, width))
- flipud(img_size)¶
Flips current VectorGeometry in vertical.
- Parameters:
- Returns:
Flipped VectorGeometry in vertical.
- Return type:
- Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable height, width = 300, 400 flipud_figure = figure.flipud((height, width))
- get_mask(img_size)¶
Returns 2D boolean mask of the geometry. With shape as img_size (height, width) and filled with True values inside the geometry and False values outside. dtype = np.bool shape = img_size
- relative_crop(rect)¶
Crops object like “crop” method, but return results with coordinates relative to rect :param rect: Rectangle for crop. :type rect:
Rectangle:returns: List of Geometry after relative crop. :rtype: List[Geometry]` :raises NotImplementedError: if method is not implemented in subclass
- resize(in_size, out_size)¶
Resizes current VectorGeometry.
- Parameters:
- Returns:
Resized VectorGeometry.
- Return type:
- Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable in_height, in_width = 300, 400 out_height, out_width = 600, 800 resize_figure = figure.resize((in_height, in_width), (out_height, out_width))
- rotate(rotator)¶
Rotates current VectorGeometry.
- Parameters:
- rotator¶
ImageRotator for rotate.
- Returns:
Rotated VectorGeometry.
- Return type:
- Usage Example:
from supervisely.geometry.image_rotator import ImageRotator # Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable height, width = 300, 400 rotator = ImageRotator((height, width), 25) rotate_figure = figure.rotate(rotator)
- scale(factor)¶
Scales current VectorGeometry.
- Parameters:
- Returns:
Scaled VectorGeometry.
- Return type:
- Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable scale_figure = figure.scale(0.75)
- to_bbox()¶
Creates Rectangle object from current VectorGeometry.
- Returns:
Rectangle from VectorGeometry.
- Return type:
- Usage Example:
rectangle = figure.to_bbox()
- to_json()¶
Convert the VectorGeometry to a json dict. Read more about Supervisely format.
- Returns:
Json format as a dict
- Return type:
- Usage Example:
figure_json = figure.to_json() print(figure_json) # Output: { # "points": { # "exterior": [ # [2104, 730], # [402, 2479], # [1646, 3746] # ], # "interior": [ # [ # [1255, 1907], # [875, 2468], # [1577, 2679] # ] # ] # } # }
- translate(drow, dcol)¶
Translates current VectorGeometry.
- Parameters:
- Returns:
Translated VectorGeometry.
- Return type:
- Usage Example:
# Remember that VectorGeometry class object is immutable, and we need to assign new instance of VectorGeometry to a new variable translate_figure = figure.translate(150, 250)
- validate(obj_class_shape, settings)¶
Validate geometry.
- Parameters:
- Raises:
ValueError – if geometry validation error
- property area : float¶
Polygon area.
- Returns:
Area of current Polygon.
- Return type:
- Usage Example:
print(figure.area) # Output: 7288.0
- property exterior : list[supervisely.geometry.point_location.PointLocation]¶
VectorGeometry exterior points.
- Returns:
VectorGeometry exterior points
- Return type:
List[
PointLocation]- Usage Example:
exterior = figure.exterior
- property exterior_np : numpy.ndarray¶
Converts exterior attribute of VectorGeometry to numpy array.
- Returns:
Numpy array
- Return type:
np.ndarray
- Usage Example:
print(figure.exterior_np) # Output: # [[ 730 2104] # [2479 402] # [3746 1646]]
- property interior : list[list[supervisely.geometry.point_location.PointLocation]]¶
VectorGeometry interior points.
- Returns:
VectorGeometry interior points
- Return type:
List[List[
PointLocation]]- Usage Example:
interior = figure.interior
- property interior_np¶
Converts interior attribute of VectorGeometry to numpy array.
- Returns:
Numpy array
- Return type:
List[np.ndarray]
- Usage Example:
print(figure.interior_np) # Output: # [array([[1907, 1255], # [2468, 875], # [2679, 1577]])]