Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ class NormalizeIntensity(Transform):
nonzero: whether only normalize non-zero values.
channel_wise: if using calculated mean and std, calculate on each channel separately
or calculate on the entire image directly.
dtype: output data type, defaut to float32.
"""

def __init__(
Expand All @@ -208,11 +209,13 @@ def __init__(
divisor: Optional[Sequence] = None,
nonzero: bool = False,
channel_wise: bool = False,
dtype: np.dtype = np.float32,
) -> None:
self.subtrahend = subtrahend
self.divisor = divisor
self.nonzero = nonzero
self.channel_wise = channel_wise
self.dtype = dtype

def _normalize(self, img: np.ndarray, sub=None, div=None) -> np.ndarray:
slices = (img != 0) if self.nonzero else np.ones(img.shape, dtype=np.bool_)
Expand Down Expand Up @@ -252,7 +255,7 @@ def __call__(self, img: np.ndarray) -> np.ndarray:
else:
img = self._normalize(img, self.subtrahend, self.divisor)

return img
return img.astype(self.dtype)


class ThresholdIntensity(Transform):
Expand Down
4 changes: 3 additions & 1 deletion monai/transforms/intensity/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class NormalizeIntensityd(MapTransform):
nonzero: whether only normalize non-zero values.
channel_wise: if using calculated mean and std, calculate on each channel separately
or calculate on the entire image directly.
dtype: output data type, defaut to float32.
"""

def __init__(
Expand All @@ -236,9 +237,10 @@ def __init__(
divisor: Optional[np.ndarray] = None,
nonzero: bool = False,
channel_wise: bool = False,
dtype: np.dtype = np.float32,
) -> None:
super().__init__(keys)
self.normalizer = NormalizeIntensity(subtrahend, divisor, nonzero, channel_wise)
self.normalizer = NormalizeIntensity(subtrahend, divisor, nonzero, channel_wise, dtype)

def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
Expand Down
1 change: 1 addition & 0 deletions tests/test_normalize_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class TestNormalizeIntensity(NumpyImageTestCase2D):
def test_default(self):
normalizer = NormalizeIntensity()
normalized = normalizer(self.imt)
self.assertTrue(normalized.dtype == np.float32)
expected = (self.imt - np.mean(self.imt)) / np.std(self.imt)
np.testing.assert_allclose(normalized, expected, rtol=1e-6)

Expand Down