Skip to content

Commit

Permalink
Merge pull request #616 from oesteban/enh/clip-and-invert
Browse files Browse the repository at this point in the history
ENH: Add an inversion operation to ``IntensityClip``
  • Loading branch information
oesteban authored Feb 8, 2021
2 parents c2d7fad + 3a9f1be commit 9beccec
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion niworkflows/interfaces/nibabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ class _IntensityClipInputSpec(BaseInterfaceInputSpec):
dtype = traits.Enum(
"int16", "float32", "uint8", usedefault=True, desc="output datatype"
)
invert = traits.Bool(False, usedefault=True, desc="finalize by inverting contrast")


class _IntensityClipOutputSpec(TraitedSpec):
Expand All @@ -378,6 +379,7 @@ def _run_interface(self, runtime):
p_max=self.inputs.p_max,
nonnegative=self.inputs.nonnegative,
dtype=self.inputs.dtype,
invert=self.inputs.invert,
newpath=runtime.cwd,
)
return runtime
Expand Down Expand Up @@ -472,7 +474,7 @@ def _gen_reference(


def _advanced_clip(
in_file, p_min=35, p_max=99.98, nonnegative=True, dtype="int16", newpath=None
in_file, p_min=35, p_max=99.98, nonnegative=True, dtype="int16", invert=False, newpath=None,
):
"""
Remove outliers at both ends of the intensity distribution and fit into a given dtype.
Expand Down Expand Up @@ -517,6 +519,9 @@ def _advanced_clip(
data -= data.min()
data /= data.max()

if invert:
data = 1.0 - data

if dtype in ("uint8", "int16"):
data = np.round(255 * data).astype(dtype)

Expand Down

0 comments on commit 9beccec

Please sign in to comment.