Compute the foreground and background of input label data, return the indices after fattening. For example: ``label = np.array([[[0, 1, 1], [1, 0, 1], [1, 1, 0]]])`` ``foreground indices = np.array([1, 2, 3, 5, 6, 7])`` and ``background indices = np.array([0, 4, 8])`` Args:
(
label: NdarrayOrTensor, image: NdarrayOrTensor | None = None, image_threshold: float = 0.0
)
| 444 | |
| 445 | |
| 446 | def map_binary_to_indices( |
| 447 | label: NdarrayOrTensor, image: NdarrayOrTensor | None = None, image_threshold: float = 0.0 |
| 448 | ) -> tuple[NdarrayOrTensor, NdarrayOrTensor]: |
| 449 | """ |
| 450 | Compute the foreground and background of input label data, return the indices after fattening. |
| 451 | For example: |
| 452 | ``label = np.array([[[0, 1, 1], [1, 0, 1], [1, 1, 0]]])`` |
| 453 | ``foreground indices = np.array([1, 2, 3, 5, 6, 7])`` and ``background indices = np.array([0, 4, 8])`` |
| 454 | |
| 455 | Args: |
| 456 | label: use the label data to get the foreground/background information. |
| 457 | image: if image is not None, use ``label = 0 & image > image_threshold`` |
| 458 | to define background. so the output items will not map to all the voxels in the label. |
| 459 | image_threshold: if enabled `image`, use ``image > image_threshold`` to |
| 460 | determine the valid image content area and select background only in this area. |
| 461 | """ |
| 462 | check_non_lazy_pending_ops(label, name="map_binary_to_indices") |
| 463 | # Prepare fg/bg indices |
| 464 | if label.shape[0] > 1: |
| 465 | label = label[1:] # for One-Hot format data, remove the background channel |
| 466 | label_flat = ravel(any_np_pt(label, 0)) # in case label has multiple dimensions |
| 467 | fg_indices = nonzero(label_flat) |
| 468 | if image is not None: |
| 469 | check_non_lazy_pending_ops(image, name="map_binary_to_indices") |
| 470 | img_flat = ravel(any_np_pt(image > image_threshold, 0)) |
| 471 | img_flat, *_ = convert_to_dst_type(img_flat, label, dtype=bool) |
| 472 | bg_indices = nonzero(img_flat & ~label_flat) |
| 473 | else: |
| 474 | bg_indices = nonzero(~label_flat) |
| 475 | |
| 476 | # no need to save the indices in GPU, otherwise, still need to move to CPU at runtime when crop by indices |
| 477 | fg_indices, *_ = convert_data_type(fg_indices, device=torch.device("cpu")) |
| 478 | bg_indices, *_ = convert_data_type(bg_indices, device=torch.device("cpu")) |
| 479 | return fg_indices, bg_indices |
| 480 | |
| 481 | |
| 482 | def map_classes_to_indices( |
searching dependent graphs…