Create a circular mask tensor. Args: h (int): The height of the mask tensor. w (int): The width of the mask tensor. center (Optional[Tuple[int, int]]): The center of the circle as a tuple (y, x). If None, the middle of the image is used. radius (Optional[int
(
h: int,
w: int,
center: Optional[Tuple[int, int]] = None,
radius: Optional[int] = None,
)
| 148 | |
| 149 | |
| 150 | def create_circular_mask( |
| 151 | h: int, |
| 152 | w: int, |
| 153 | center: Optional[Tuple[int, int]] = None, |
| 154 | radius: Optional[int] = None, |
| 155 | ) -> torch.Tensor: |
| 156 | """ |
| 157 | Create a circular mask tensor. |
| 158 | |
| 159 | Args: |
| 160 | h (int): The height of the mask tensor. |
| 161 | w (int): The width of the mask tensor. |
| 162 | center (Optional[Tuple[int, int]]): The center of the circle as a tuple (y, x). If None, the middle of the image is used. |
| 163 | radius (Optional[int]): The radius of the circle. If None, the smallest distance between the center and image walls is used. |
| 164 | |
| 165 | Returns: |
| 166 | A boolean tensor of shape [h, w] representing the circular mask. |
| 167 | """ |
| 168 | if center is None: # use the middle of the image |
| 169 | center = (int(h / 2), int(w / 2)) |
| 170 | if radius is None: # use the smallest distance between the center and image walls |
| 171 | radius = min(center[0], center[1], h - center[0], w - center[1]) |
| 172 | |
| 173 | Y, X = np.ogrid[:h, :w] |
| 174 | dist_from_center = np.sqrt((Y - center[0]) ** 2 + (X - center[1]) ** 2) |
| 175 | |
| 176 | mask = dist_from_center <= radius |
| 177 | mask = torch.from_numpy(mask).bool() |
| 178 | return mask |
| 179 | |
| 180 | |
| 181 | def create_square_mask( |
nothing calls this directly
no outgoing calls
no test coverage detected