Bitmap¶
- class Bitmap[source]¶
Bases:
supervisely.geometry.bitmap_base.BitmapBase
Bitmap geometry for a single
Label
.Bitmap
object is immutable.- Parameters
- data : np.ndarray
Bitmap mask data. Must be a numpy array with only 2 unique values: [0, 1] or [0, 255] or [False, True].
- origin : PointLocation, optional
PointLocation
: top, left corner of Bitmap. Position of the Bitmap within image.- sly_id : int, optional
Bitmap ID in Supervisely server.
- class_id : int, optional
ID of
ObjClass
to which Bitmap belongs.- labeler_login : str, optional
Login of the user who created Bitmap.
- updated_at : str, optional
Date and Time when Bitmap 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 Bitmap 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 bitmap mask = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.bool_) figure = sly.Bitmap(mask) # Note, when creating a bitmap, the specified mask is cut off by positive values, based on this, a origin is formed: print(figure.data) # Output: # [[ True True True] # [ True False True] # [ True True True]] origin = figure.origin.to_json() print(json.dumps(origin, indent=4)) # Output: { # "points": { # "exterior": [ # [ # 1, # 1 # ] # ], # "interior": [] # } # Create bitmap from black and white image: img = sly.imaging.image.read(os.path.join(os.getcwd(), 'black_white.jpeg')) mask = img[:, :, 0].astype(bool) # Get 2-dimensional bool numpy array figure = sly.Bitmap(mask)
Methods
Convert base64 encoded string to numpy array.
Make bitwise operations between a given numpy array and Bitmap.
Clone from GEOMETRYYY
config_from_json
config_to_json
convert
Crops current Bitmap.
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.
Flip current Bitmap in horizontal.
Flip current Bitmap in vertical.
Convert a json dict to BitmapBase.
Read bitmap from image by path.
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
Resizes current Bitmap.
Rotates current Bitmap.
Scale current Bitmap.
Compute the skeleton, medial axis transform or morphological thinning of Bitmap.
Create
Rectangle
object from current Bitmap.Get list of contours in Bitmap.
Convert the BitmapBase to a json dict.
Translate current Bitmap.
validate
Attributes
Bitmap area.
Get mask data of Bitmap.
Position of the Bitmap within image.
- static base64_2_data(s)[source]¶
Convert base64 encoded string to numpy array. Supports both compressed and uncompressed masks.
- Parameters
- s : str
Input base64 encoded string.
- Returns
Bool numpy array
- Return type
np.ndarray
- Usage example
import supervisely as sly encoded_string = 'eJzrDPBz5+WS4mJgYOD19HAJAtLMIMwIInOeqf8BUmwBPiGuQPr///9Lb86/C2QxlgT5BTM4PLuRBuTwebo4hlTMSa44sKHhISMDuxpTYrr03F6gDIOnq5/LOqeEJgDM5ht6' figure_data = sly.Bitmap.base64_2_data(encoded_string) print(figure_data) # [[ True True True] # [ True False True] # [ True True True]] uncompressed_string = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA' boolean_mask = sly.Bitmap.base64_2_data(uncompressed_string) print(boolean_mask) # [[ True True True] # [ True False True] # [ True True True]]
- bitwise_mask(full_target_mask, bit_op)[source]¶
Make bitwise operations between a given numpy array and Bitmap.
- Parameters
- full_target_mask : np.ndarray
Input numpy array.
- bit_op : Numpy logical operation
Type of bitwise operation(and, or, not, xor), uses numpy logic functions.
- Returns
Bitmap object or empty list
- Return type
- Usage example
import supervisely as sly mask = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.bool_) figure = sly.Bitmap(mask) array = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]], dtype=np.bool_) bitwise_figure = figure.bitwise_mask(array, np.logical_and) print(bitwise_figure.data) # Output: # [[ True True True] # [False False False] # [False True False]]
- clone()¶
Clone from GEOMETRYYY
- crop(rect)[source]¶
Crops current Bitmap.
- Parameters
- rect : Rectangle
Rectangle object for cropping.
- Returns
List of Bitmaps
- Return type
- Usage Example
crop_figures = figure.crop(sly.Rectangle(1, 1, 300, 350))
- static data_2_base64(mask)[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 address = 'https://app.supervise.ly/' token = 'Your Supervisely API Token' api = sly.Api(address, token) # Get annotation from API meta_json = api.project.get_meta(PROJECT_ID) meta = sly.ProjectMeta.from_json(meta_json) ann_info = api.annotation.download(IMAGE_ID) ann = sly.Annotation.from_json(ann_info.annotation, meta) # Get Bitmap from annotation for label in ann.labels: if type(label.geometry) == sly.Bitmap: figure = label.geometry encoded_string = sly.Bitmap.data_2_base64(figure.data) print(encoded_string) # 'eJzrDPBz5+WS4mJgYOD19HAJAtLMIMwIInOeqf8BUmwBPiGuQPr///9Lb86/C2QxlgT5BTM4PLuRBuTwebo4hlTMSa44sKHhISMDuxpTYrr03F6gDIOnq5/LOqeEJgDM5ht6'
-
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)¶
Flip current Bitmap in horizontal.
- Parameters
- img_size : Tuple[int, int]
Annotation.img_size
which belongs Bitmap.
- Returns
BitmapBase object
- Return type
BitmapBase
- Usage Example
# Remember that Bitmap class object is immutable, and we need to assign new instance of Bitmap to a new variable height, width = 300, 400 fliplr_figure = figure.fliplr((height, width))
- flipud(img_size)¶
Flip current Bitmap in vertical.
- Parameters
- img_size : Tuple[int, int]
Annotation.img_size
which belongs Bitmap.
- Returns
BitmapBase object
- Return type
BitmapBase
- Usage Example
# Remember that Bitmap class object is immutable, and we need to assign new instance of Bitmap to a new variable height, width = 300, 400 flipud_figure = figure.flipud((height, width))
- classmethod from_json(json_data)¶
Convert a json dict to BitmapBase. Read more about Supervisely format.
- Parameters
- data : dict
Bitmap in json format as a dict.
- Returns
BitmapBase object
- Return type
BitmapBase
- Usage example
import supervisely as sly figure_json = { "bitmap": { "origin": [1, 1], "data": "eJzrDPBz5+WS4mJgYOD19HAJAtLMIMwIInOeqf8BUmwBPiGuQPr///9Lb86/C2QxlgT5BTM4PLuRBuTwebo4hlTMSa44sKHhISMDuxpTYrr03F6gDIOnq5/LOqeEJgDM5ht6" }, "shape": "bitmap", "geometryType": "bitmap" } figure = sly.Bitmap.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)[source]¶
Resizes current Bitmap.
- Parameters
- Returns
Bitmap object
- Return type
- Usage Example
in_height, in_width = 800, 1067 out_height, out_width = 600, 800 # Remember that Bitmap class object is immutable, and we need to assign new instance of Bitmap to a new variable resize_figure = figure.resize((in_height, in_width), (out_height, out_width))
- rotate(rotator)[source]¶
Rotates current Bitmap.
- Parameters
- rotator : ImageRotator
ImageRotator
for Bitamp rotation.
- Returns
Bitmap object
- Return type
- Usage Example
import supervisely as sly from supervisely.geometry.image_rotator import ImageRotator height, width = ann.img_size rotator = ImageRotator((height, width), 25) # Remember that Bitmap class object is immutable, and we need to assign new instance of Bitmap to a new variable rotate_figure = figure.rotate(rotator)
- scale(factor)¶
Scale current Bitmap.
- Parameters
- factor : float
Scale parameter.
- Returns
BitmapBase object
- Return type
BitmapBase
- Usage Example
# Remember that Bitmap class object is immutable, and we need to assign new instance of Bitmap to a new variable scale_figure = figure.scale(0.75)
- skeletonize(method_id)[source]¶
Compute the skeleton, medial axis transform or morphological thinning of Bitmap.
- Parameters
- method_id : SkeletonizeMethod
Method to convert bool numpy array.
- Returns
Bitmap object
- Return type
- Usage example
# Remember that Bitmap class object is immutable, and we need to assign new instance of Bitmap to a new variable skeleton_figure = figure.skeletonize(SkeletonizeMethod.SKELETONIZE) med_ax_figure = figure.skeletonize(SkeletonizeMethod.MEDIAL_AXIS) thin_figure = figure.skeletonize(SkeletonizeMethod.THINNING)
- to_bbox()¶
Create
Rectangle
object from current Bitmap.- Returns
Rectangle object
- Return type
- Usage Example
rectangle = figure.to_bbox()
- to_contours()[source]¶
Get list of contours in Bitmap.
- Returns
List of Polygon objects
- Return type
- Usage example
figure_contours = figure.to_contours()
- to_json()¶
Convert the BitmapBase to a json dict. Read more about Supervisely format.
- Returns
Json format as a dict
- Return type
- Usage example
import supervisely as sly mask = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.bool_) figure = sly.Bitmap(mask) figure_json = figure.to_json() print(json.dumps(figure_json, indent=4)) # Output: { # "bitmap": { # "origin": [1, 1], # "data": "eJzrDPBz5+WS4mJgYOD19HAJAtLMIMwIInOeqf8BUmwBPiGuQPr///9Lb86/C2QxlgT5BTM4PLuRBuTwebo4hlTMSa44sKHhISMDuxpTYrr03F6gDIOnq5/LOqeEJgDM5ht6" # }, # "shape": "bitmap", # "geometryType": "bitmap" # }
- translate(drow, dcol)¶
Translate current Bitmap.
- property area¶
Bitmap area.
- Returns
Area of current Bitmap
- Return type
- Usage example
print(figure.area) # Output: 88101.0
- property data¶
Get mask data of Bitmap.
- Returns
Data of Bitmap.
- Return type
np.ndarray
- property origin¶
Position of the Bitmap within image.
- Returns
Top, left corner of Bitmap.
- Return type