@@ -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,48 @@ 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
35+ ) -> np .ndarray :
3336 """
37+ # TODO: Optionally use the color. a color of 1 will result in a binary canvas
38+
39+
3440 Removes all pixels from the segmentation mask that do not equal self.color
3541
3642 Args:
37- height:
38-
43+ height (int): Optionally resize mask height before drawing.
44+ width (int): Optionally resize mask width before drawing.
45+ canvas (np.ndarray): Optionall provide a canvas to draw on
46+ color (Union[int, Tuple[int,int,int]]): Color to draw the canvas.
47+ Defaults to using the encoded color in the mask.
48+ int will return the mask as a 1d array
49+ tuple[int,int,int] will return the mask as a 3d array
50+ thickness (None): Unused, exists for a consistent interface.
3951 Returns:
4052 np.ndarray representing only this object
53+ as opposed to the mask that this object references which might have multiple objects determined by colors
4154 """
55+
4256 mask = self .mask .value
4357 mask = np .alltrue (mask == self .color , axis = 2 ).astype (np .uint8 )
58+
4459 if height is not None or width is not None :
4560 mask = cv2 .resize (mask ,
4661 (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
62+
63+ dims = [mask .shape [0 ], mask .shape [1 ]]
64+ color = color or self .color
65+ if isinstance (color , (tuple , list )):
66+ dims = dims + [len (color )]
67+
68+ canvas = canvas if canvas is not None else np .zeros (tuple (dims ), dtype = np .uint8 )
69+ canvas [mask .astype (np .bool )] = color
70+ return canvas
71+
72+
73+
5474
5575 def create_url (self , signer : Callable [[bytes ], str ]) -> str :
5676 """
0 commit comments