Skip to content

Add median image filter as a transform #5264

Closed

Description

@dzenanz

Is your feature request related to a problem? Please describe.
One problem is explained in #3178.

Describe the solution you'd like
Add a median filter based on @ebrahimebrahim's medianblur_with_gpu

Describe alternatives you've considered
#3178 mentions some alternatives, such as mean filtering. Those are suboptimal for many use cases.

Additional context
Model it after GaussianSmooth:

class GaussianSmooth(Transform):
"""
Apply Gaussian smooth to the input data based on specified `sigma` parameter.
A default value `sigma=1.0` is provided for reference.
Args:
sigma: if a list of values, must match the count of spatial dimensions of input data,
and apply every value in the list to 1 spatial dimension. if only 1 value provided,
use it for all spatial dimensions.
approx: discrete Gaussian kernel type, available options are "erf", "sampled", and "scalespace".
see also :py:meth:`monai.networks.layers.GaussianFilter`.
"""
backend = [TransformBackends.TORCH]
def __init__(self, sigma: Union[Sequence[float], float] = 1.0, approx: str = "erf") -> None:
self.sigma = sigma
self.approx = approx
def __call__(self, img: NdarrayTensor) -> NdarrayTensor:
img = convert_to_tensor(img, track_meta=get_track_meta())
img_t, *_ = convert_data_type(img, torch.Tensor, dtype=torch.float)
sigma: Union[Sequence[torch.Tensor], torch.Tensor]
if isinstance(self.sigma, Sequence):
sigma = [torch.as_tensor(s, device=img_t.device) for s in self.sigma]
else:
sigma = torch.as_tensor(self.sigma, device=img_t.device)
gaussian_filter = GaussianFilter(img_t.ndim - 1, sigma, approx=self.approx)
out_t: torch.Tensor = gaussian_filter(img_t.unsqueeze(0)).squeeze(0)
out, *_ = convert_to_dst_type(out_t, dst=img, dtype=out_t.dtype)
return out

Activity

  1. dzenanz commented on Oct 4, 2022

    @dzenanz
    ContributorAuthor

    Can someone assign this to me? I am starting work on it today.

  2. ebrahimebrahim commented on Oct 5, 2022

    @ebrahimebrahim
    Contributor

    Note that there may be a much better approach than the filter I wrote.

    • Because mine has a giant sparse kernel (size $n^6$ when the filter size is $n$), it quickly becomes unwieldy as you increase the filter size. I wonder if there is a clever way around this. I sometimes see the MONAI folks implementing 2D or 3D convolutions in terms of 1D convolutions, but I never dug into how that actually works and when it can be done. Would be good to look at how some other convolution-based transforms in MONAI are implemented.
    • My implementation is 3D and really we want something that is general to any spatial dimensionality. Actually I stole mine from the Kornia project; they had a 2D median filter that worked the same way.
  3. wyli commented on Oct 5, 2022

    @wyli
    Contributor

    I saw a similar implementation in tensorflow https://github.com/tensorflow/addons/blob/v0.17.0/tensorflow_addons/image/filters.py#L125-L200 given pytorch's convolution is up to 3d, perhaps we can support 1/2/3d as a starting point.

    for the separable approximation, it seems it's useful in practice (for 2d) https://ieeexplore.ieee.org/document/4767047, maybe we can create another feature request for it after finishing this ticket, what do you think?

  4. added 2 commits that reference this issue on Oct 12, 2022
    9731823
    e081510
  5. added 4 commits that reference this issue on Oct 24, 2022
    c33b7ea
    aed8fe5
    bc23df4
    e33b2ac
  6. added 2 commits that reference this issue on Nov 2, 2022
    9ec6471
    615a43b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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