Skip to content

Inquiry Regarding Functionality of CropForeground in MONAI Library #7314

Closed

Description

@77QiZhang

I have been using the CropForeground function to perform background segmentation for extracting foreground information. However, after the cropping operation, I've noticed that the original bounding box coordinates for the background are lost. I am inquiring whether there is a solution within MONAI to perform padding on the cropped background to a specific size while preserving the original background information.

In essence, I aim to pad the cropped background to a predetermined size based on the cropping operation, ensuring retention of the original background information. I would greatly appreciate your guidance or insights regarding any methods or alternative functions within MONAI that could address this requirement.

Thank you for your time and consideration in addressing this query.

Activity

  1. 77QiZhang commented on Dec 13, 2023

    @77QiZhang
    Author

    the following code snippet related to my issue

    import logging
    import sys
    import torch
    import matplotlib.pyplot as plt
    import json

    from monai.config import print_config
    from monai.data import Dataset
    from monai.transforms import (
    Compose,
    LoadImaged,
    EnsureChannelFirstd,
    EnsureTyped,
    CropForegroundd,
    SpatialPadd,
    )

    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    print_config()
    version

    train_json_path = 'my_data_path.json'

    train_transforms = Compose([
    LoadImaged(keys=["image", "label"]),
    EnsureChannelFirstd(keys=["image", "label"]),
    EnsureTyped(keys=["image"], dtype=torch.float32),
    CropForegroundd(keys=["image"], source_key="label", lazy=True),
    SpatialPadd(keys=["image"], spatial_size=(128,96,32), mode='median', lazy=True),
    ])

    train_data = json.load(open(train_json_path))
    train_ds = Dataset(
    data=train_data,
    transform=train_transforms,
    )
    for data in train_ds:
    image = data['image']
    plt.imshow(image[0,0,:,:], cmap='gray')
    plt.show()
    break

    plt_show

  2. KumoLiu commented on Dec 13, 2023

    @KumoLiu
    Contributor

    Hi @77QiZhang, thanks for the reporting.
    It appears to be a bug in lazy mode, so I've created a PR to address it.
    Thanks!

  3. added a commit that references this issue on Dec 18, 2023
    90d2acb
  4. added a commit that references this issue on Jan 30, 2024
    0e89c92
  5. added a commit that references this issue on Mar 25, 2024
    469db7a
  6. added a commit that references this issue on Apr 11, 2024
    0163d5b
  7. added a commit that references this issue on Apr 28, 2024
    3c1dea0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions