MCPcopy Create free account
hub / github.com/OpenGVLab/DragGAN / create_square_mask

Function create_square_mask

draggan/utils.py:181–216  ·  view source on GitHub ↗

Create a square mask tensor. Args: height (int): The height of the mask. width (int): The width of the mask. center (list): The center of the square mask as a list of two integers. Order [y,x] radius (int): The radius of the square mask. Returns: tor

(
    height: int, width: int, center: list, radius: int
)

Source from the content-addressed store, hash-verified

179
180
181def create_square_mask(
182 height: int, width: int, center: list, radius: int
183) -> torch.Tensor:
184 """Create a square mask tensor.
185
186 Args:
187 height (int): The height of the mask.
188 width (int): The width of the mask.
189 center (list): The center of the square mask as a list of two integers. Order [y,x]
190 radius (int): The radius of the square mask.
191
192 Returns:
193 torch.Tensor: The square mask tensor of shape (1, 1, height, width).
194
195 Raises:
196 ValueError: If the center or radius is invalid.
197 """
198 if not isinstance(center, list) or len(center) != 2:
199 raise ValueError("center must be a list of two integers")
200 if not isinstance(radius, int) or radius <= 0:
201 raise ValueError("radius must be a positive integer")
202 if (
203 center[0] < radius
204 or center[0] >= height - radius
205 or center[1] < radius
206 or center[1] >= width - radius
207 ):
208 raise ValueError("center and radius must be within the bounds of the mask")
209
210 mask = torch.zeros((height, width), dtype=torch.float32)
211 x1 = int(center[1]) - radius
212 x2 = int(center[1]) + radius
213 y1 = int(center[0]) - radius
214 y2 = int(center[0]) + radius
215 mask[y1: y2 + 1, x1: x2 + 1] = 1.0
216 return mask.bool()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected