Mask3D¶
- class Mask3D[source]¶
Bases:
supervisely.geometry.geometry.GeometryMask 3D geometry for a single
Label.Mask3Dobject is immutable.- Parameters
- data : np.ndarray
Mask 3D mask data. Must be a numpy array with only 2 unique values: [0, 1] or [0, 255] or [False, True].
- sly_id : int, optional
Mask 3D ID in Supervisely server.
- class_id : int, optional
ID of
ObjClassto which Mask 3D belongs.- labeler_login : str, optional
Login of the user who created Mask 3D.
- updated_at : str, optional
Date and Time when Mask 3D 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 Mask 3D was created. Date Format is the same as in “updated_at” parameter.
- Raises
ValueError, if data is not bool or no pixels set to True in data- Usage example
import supervisely as sly # Create simple Mask 3D mask3d = np.zeros((3, 3, 3), dtype=np.bool_) mask3d[0:2, 0:2, 0:2] = True shape = sly.Mask3D(mask3d) print(shape.data) # Output: # [[[ True True False] # [ True True False] # [False False False]] # [[ True True False] # [ True True False] # [False False False]] # [[False False False] # [False False False] # [False False False]]]
Methods
Draw a 2D mask on a 3D Mask.
allowed_transformsConvert base64 encoded string to numpy array.
Clone from GEOMETRYYY
config_from_jsonconfig_to_jsonconvertCreates Mask3D geometry from file.
- param rect
Rectangle
Convert numpy array to base64 encoded string.
- param bitmap
np.ndarray
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.
- param img_size
(rows, cols)
- param img_size
(rows, cols)
Create a Mask3D geometry object from bytes.
Load figure geometry from file.
Convert a json dict to Mask 3D.
Return geometry name
Returns 2D boolean mask of the geometry.
Same as geometry_name(), but shorter.
Crops object like "crop" method, but return results with coordinates relative to rect :param rect: :return: list of Geometry
- param in_size
(rows, cols)
Rotates around image center -> New Geometry :param rotator: ImageRotator :return: Geometry
Scales around origin with a given factor.
- rtype
Convert the Mask 3D to a json dict.
- param drow
int rows shift
validateAttributes
- return
float
-
add_mask_2d(mask_2d, plane_name, slice_index, origin=
None)[source]¶ Draw a 2D mask on a 3D Mask.
- Parameters
- mask_2d : np.ndarray
2D array with a flat mask.
- plane_name : str
Name of the plane: “axial”, “sagittal”, “coronal”.
- slice_index : int
Slice index of the volume figure.
- origin : Optional[List[int]], NoneType
(row, col) position. The top-left corner of the mask is located on the specified slice (optional).
- static base64_2_data(encoded_string)[source]¶
Convert base64 encoded string to numpy array.
- Parameters
- s : str
Input base64 encoded string.
- Returns
Bool numpy array
- Return type
np.ndarray- Usage example
import supervisely as sly encoded_string = 'H4sIAGWoWmQC/zPWMdYxrmFkZAAiIIAz4AAAE56ciyEAAAA=' figure_data = sly.Mask3D.base64_2_data(encoded_string) print(figure_data) # [[[1 1 0] # [1 1 0] # [0 0 0]] # [[1 1 0] # [1 1 0] # [0 0 0]] # [[0 0 0] # [0 0 0] # [0 0 0]]]
- clone()¶
Clone from GEOMETRYYY
- crop(rect)¶
- Parameters
- rect
Rectangle
- Returns
list of Geometry
- static data_2_base64(data)[source]¶
Convert numpy array to base64 encoded string.
- Parameters
- mask : np.ndarray
Bool numpy array.
- Returns
Base64 encoded string
- Return type
- Usage example
import supervisely as sly import os import nrrd address = 'https://app.supervise.ly/' token = 'Your Supervisely API Token' api = sly.Api(address, token) meta_json = api.project.get_meta(PROJECT_ID) meta = sly.ProjectMeta.from_json(meta_json) ann_json = api.volume.annotation.download_bulk(DATASET_ID, [VOLUME_ID]) figure_id = ann_json[0]["spatialFigures"][0]["id"] path_for_mesh = f"meshes/{figure_id}.nrrd" api.volume.figure.download_stl_meshes([figure_id], [path_for_mesh]) mask3d_data, _ = nrrd.read(path_for_mesh) encoded_string = sly.Mask3D.data_2_base64(mask3d_data) print(encoded_string) # 'H4sIAGWoWmQC/zPWMdYxrmFkZAAiIIAz4AAAE56ciyEAAAA='
-
draw(bitmap, color, thickness=
1, config=None)¶ - Parameters
- bitmap
np.ndarray
- color
[R, G, B]
- thickness
used only in Polyline and Point
- config
drawing config specific to a concrete subclass, e.g. per edge colors
-
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)¶
- Parameters
- img_size
(rows, cols)
- Returns
Geometry
- flipud(img_size)¶
- Parameters
- img_size
(rows, cols)
- Returns
Geometry
- static from_file(figure, file_path)[source]¶
Load figure geometry from file.
- Parameters
- figure : VolumeFigure
Spatial figure
- file_path : str
Path to nrrd file with data
- classmethod from_json(json_data)[source]¶
Convert a json dict to Mask 3D.
- Parameters
- data : dict
Mask in json format as a dict.
- Returns
Mask3D object
- Return type
- Usage example
import supervisely as sly figure_json = { "mask_3d": { "data": "eJzrDPBz5+WS4mJgYOD19HAJAtLMIMwIInOeqf8BUmwBPiGuQPr///9Lb86/C2QxlgT5BTM4PLuRBuTwebo4hlTMSa44sKHhISMDuxpTYrr03F6gDIOnq5/LOqeEJgDM5ht6", }, "shape": "mask_3d", "geometryType": "mask_3d" } figure = sly.Mask3D.from_json(figure_json)
- 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
- classmethod name()¶
Same as geometry_name(), but shorter. In order to make the code more concise.
- Returns
string with name of geometry
- relative_crop(rect)¶
Crops object like “crop” method, but return results with coordinates relative to rect :param rect: :return: list of Geometry
- resize(in_size, out_size)¶
- Parameters
- in_size
(rows, cols)
- out_size
(128, 256) (128, KEEP_ASPECT_RATIO) (KEEP_ASPECT_RATIO, 256)
- Returns
Geometry
- rotate(rotator)¶
Rotates around image center -> New Geometry :param rotator: ImageRotator :return: Geometry
- scale(factor)¶
Scales around origin with a given factor. :param: factor (float): :return: Geometry
- to_json()[source]¶
Convert the Mask 3D to a json dict.
- Returns
Json format as a dict
- Return type
- Usage example
import supervisely as sly mask = np.array([[[1 1 0] [1 1 0] [0 0 0]] [[1 1 0] [1 1 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]], dtype=np.bool_) figure = sly.Mask3D(mask) figure_json = figure.to_json() print(json.dumps(figure_json, indent=4)) # Output: { # "mask_3d": { # "data": "eJzrDPBz5+WS4mJgYOD19HAJAtLMIMwIInOeqf8BUmwBPiGuQPr///9Lb86/C2QxlgT5BTM4PLuRBuTwebo4hlTMSa44sKHhISMDuxpTYrr03F6gDIOnq5/LOqeEJgDM5ht6", # }, # "shape": "mask_3d", # "geometryType": "mask_3d" # }
- translate(drow, dcol)¶
- Parameters
- drow
int rows shift
- dcol
int cols shift
- Returns
Geometry
- property area¶
- Returns
float