MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / __call__

Method __call__

monai/apps/detection/transforms/array.py:234–265  ·  view source on GitHub ↗

Args: boxes: source bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode`` src_spatial_size: original image spatial size before zooming, used only when keep_size=True.

(self, boxes: NdarrayTensor, src_spatial_size: Sequence[int] | int | None = None)

Source from the content-addressed store, hash-verified

232 self.kwargs = kwargs
233
234 def __call__(self, boxes: NdarrayTensor, src_spatial_size: Sequence[int] | int | None = None) -> NdarrayTensor:
235 """
236 Args:
237 boxes: source bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode``
238 src_spatial_size: original image spatial size before zooming, used only when keep_size=True.
239 """
240 spatial_dims: int = get_spatial_dims(boxes=boxes)
241 self._zoom = ensure_tuple_rep(self.zoom, spatial_dims) # match the spatial image dim
242
243 if not self.keep_size:
244 return zoom_boxes(boxes, self._zoom)
245
246 if src_spatial_size is None:
247 raise ValueError("keep_size=True, src_spatial_size must be provided.")
248
249 src_spatial_size = ensure_tuple_rep(src_spatial_size, spatial_dims)
250 dst_spatial_size = [int(round(z * ss)) for z, ss in zip(self._zoom, src_spatial_size)]
251 self._zoom = tuple(ds / float(ss) for ss, ds in zip(src_spatial_size, dst_spatial_size))
252 zoomed_boxes = zoom_boxes(boxes, self._zoom)
253
254 # See also keep_size in monai.transforms.spatial.array.Zoom()
255 if not np.allclose(np.array(src_spatial_size), np.array(dst_spatial_size)):
256 for axis, (od, zd) in enumerate(zip(src_spatial_size, dst_spatial_size)):
257 diff = od - zd
258 half = abs(diff) // 2
259 if diff > 0: # need padding (half, diff - half)
260 zoomed_boxes[:, axis] = zoomed_boxes[:, axis] + half
261 zoomed_boxes[:, axis + spatial_dims] = zoomed_boxes[:, axis + spatial_dims] + half
262 elif diff < 0: # need slicing (half, half + od)
263 zoomed_boxes[:, axis] = zoomed_boxes[:, axis] - half
264 zoomed_boxes[:, axis + spatial_dims] = zoomed_boxes[:, axis + spatial_dims] - half
265 return zoomed_boxes
266
267
268class ResizeBox(Transform):

Callers

nothing calls this directly

Calls 4

get_spatial_dimsFunction · 0.90
ensure_tuple_repFunction · 0.90
zoom_boxesFunction · 0.85
arrayMethod · 0.80

Tested by

no test coverage detected