MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / NormalizeIntensity

Class NormalizeIntensity

monai/transforms/intensity/array.py:839–948  ·  view source on GitHub ↗

Normalize input based on the `subtrahend` and `divisor`: `(img - subtrahend) / divisor`. Use calculated mean or std value of the input image if no `subtrahend` or `divisor` provided. This transform can normalize only non-zero values or entire image, and can also calculate mean and s

Source from the content-addressed store, hash-verified

837
838
839class NormalizeIntensity(Transform):
840 """
841 Normalize input based on the `subtrahend` and `divisor`: `(img - subtrahend) / divisor`.
842 Use calculated mean or std value of the input image if no `subtrahend` or `divisor` provided.
843 This transform can normalize only non-zero values or entire image, and can also calculate
844 mean and std on each channel separately.
845 When `channel_wise` is True, the first dimension of `subtrahend` and `divisor` should
846 be the number of image channels if they are not None.
847 If the input is not of floating point type, it will be converted to float32
848
849 Args:
850 subtrahend: the amount to subtract by (usually the mean).
851 divisor: the amount to divide by (usually the standard deviation).
852 nonzero: whether only normalize non-zero values.
853 channel_wise: if True, calculate on each channel separately, otherwise, calculate on
854 the entire image directly. default to False.
855 dtype: output data type, if None, same as input image. defaults to float32.
856 """
857
858 backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
859
860 def __init__(
861 self,
862 subtrahend: Sequence | NdarrayOrTensor | None = None,
863 divisor: Sequence | NdarrayOrTensor | None = None,
864 nonzero: bool = False,
865 channel_wise: bool = False,
866 dtype: DtypeLike = np.float32,
867 ) -> None:
868 self.subtrahend = subtrahend
869 self.divisor = divisor
870 self.nonzero = nonzero
871 self.channel_wise = channel_wise
872 self.dtype = dtype
873
874 @staticmethod
875 def _mean(x):
876 if isinstance(x, np.ndarray):
877 return np.mean(x)
878 x = torch.mean(x.float())
879 return x.item() if x.numel() == 1 else x
880
881 @staticmethod
882 def _std(x):
883 if isinstance(x, np.ndarray):
884 return np.std(x)
885 x = torch.std(x.float(), unbiased=False)
886 return x.item() if x.numel() == 1 else x
887
888 def _normalize(self, img: NdarrayOrTensor, sub=None, div=None) -> NdarrayOrTensor:
889 img, *_ = convert_data_type(img, dtype=torch.float32)
890
891 if self.nonzero:
892 slices = img != 0
893 masked_img = img[slices]
894 if not slices.any():
895 return img
896 else:

Callers 8

__init__Method · 0.90
__call__Method · 0.90
__init__Method · 0.90
test_defaultMethod · 0.90
test_nonzeroMethod · 0.90
test_channel_wiseMethod · 0.90
test_channel_wise_intMethod · 0.90
test_value_errorsMethod · 0.90

Calls

no outgoing calls

Tested by 5

test_defaultMethod · 0.72
test_nonzeroMethod · 0.72
test_channel_wiseMethod · 0.72
test_channel_wise_intMethod · 0.72
test_value_errorsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…