@@ -18,7 +18,7 @@ class Mask(Geometry):
1818
1919 @property
2020 def geometry (self ):
21- mask = self .raster (binary = True )
21+ mask = self .raster (color = 1 )
2222 polygons = (
2323 shape (shp )
2424 for shp , val in shapes (mask , mask = None )
@@ -29,28 +29,45 @@ def geometry(self):
2929 def raster (self ,
3030 height : Optional [int ] = None ,
3131 width : Optional [int ] = None ,
32- binary = False ) -> np .ndarray :
32+ canvas : Optional [np .ndarray ] = None ,
33+ color : Optional [Union [int , Tuple [int , int , int ]]] = None ,
34+ thickness = None ) -> np .ndarray :
3335 """
36+ # TODO: Optionally use the color. a color of 1 will result in a binary canvas
37+
38+
3439 Removes all pixels from the segmentation mask that do not equal self.color
3540
3641 Args:
37- height:
38-
42+ height (int): Optionally resize mask height before drawing.
43+ width (int): Optionally resize mask width before drawing.
44+ canvas (np.ndarray): Optionall provide a canvas to draw on
45+ color (Union[int, Tuple[int,int,int]]): Color to draw the canvas.
46+ Defaults to using the encoded color in the mask.
47+ int will return the mask as a 1d array
48+ tuple[int,int,int] will return the mask as a 3d array
49+ thickness (None): Unused, exists for a consistent interface.
3950 Returns:
4051 np.ndarray representing only this object
52+ as opposed to the mask that this object references which might have multiple objects determined by colors
4153 """
54+
4255 mask = self .mask .value
4356 mask = np .alltrue (mask == self .color , axis = 2 ).astype (np .uint8 )
57+
4458 if height is not None or width is not None :
4559 mask = cv2 .resize (mask ,
4660 (width or mask .shape [1 ], height or mask .shape [0 ]))
47- if binary :
48- return mask
49- else :
50- color_image = np .zeros ((mask .shape [0 ], mask .shape [1 ], 3 ),
51- dtype = np .uint8 )
52- color_image [mask .astype (np .bool )] = self .color
53- return color_image
61+
62+ dims = [mask .shape [0 ], mask .shape [1 ]]
63+ color = color or self .color
64+ if isinstance (color , (tuple , list )):
65+ dims = dims + [len (color )]
66+
67+ canvas = canvas if canvas is not None else np .zeros (tuple (dims ),
68+ dtype = np .uint8 )
69+ canvas [mask .astype (np .bool )] = color
70+ return canvas
5471
5572 def create_url (self , signer : Callable [[bytes ], str ]) -> str :
5673 """
0 commit comments