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

Class RandAdjustContrast

monai/transforms/intensity/array.py:1247–1320  ·  view source on GitHub ↗

Randomly changes image intensity with gamma transform. Each pixel/voxel intensity is updated as: x = ((x - min) / intensity_range) ^ gamma * intensity_range + min Args: prob: Probability of adjustment. gamma: Range of gamma values. If single number, val

Source from the content-addressed store, hash-verified

1245
1246
1247class RandAdjustContrast(RandomizableTransform):
1248 """
1249 Randomly changes image intensity with gamma transform. Each pixel/voxel intensity is updated as:
1250
1251 x = ((x - min) / intensity_range) ^ gamma * intensity_range + min
1252
1253 Args:
1254 prob: Probability of adjustment.
1255 gamma: Range of gamma values.
1256 If single number, value is picked from (0.5, gamma), default is (0.5, 4.5).
1257 invert_image: whether to invert the image before applying gamma augmentation. If True, multiply all intensity
1258 values with -1 before the gamma transform and again after the gamma transform. This behaviour is mimicked
1259 from `nnU-Net <https://www.nature.com/articles/s41592-020-01008-z>`_, specifically `this
1260 <https://github.com/MIC-DKFZ/batchgenerators/blob/7fb802b28b045b21346b197735d64f12fbb070aa/batchgenerators/augmentations/color_augmentations.py#L107>`_
1261 function.
1262 retain_stats: if True, applies a scaling factor and an offset to all intensity values after gamma transform to
1263 ensure that the output intensity distribution has the same mean and standard deviation as the intensity
1264 distribution of the input. This behaviour is mimicked from `nnU-Net
1265 <https://www.nature.com/articles/s41592-020-01008-z>`_, specifically `this
1266 <https://github.com/MIC-DKFZ/batchgenerators/blob/7fb802b28b045b21346b197735d64f12fbb070aa/batchgenerators/augmentations/color_augmentations.py#L107>`_
1267 function.
1268 """
1269
1270 backend = AdjustContrast.backend
1271
1272 def __init__(
1273 self,
1274 prob: float = 0.1,
1275 gamma: Sequence[float] | float = (0.5, 4.5),
1276 invert_image: bool = False,
1277 retain_stats: bool = False,
1278 ) -> None:
1279 RandomizableTransform.__init__(self, prob)
1280
1281 if isinstance(gamma, (int, float)):
1282 if gamma <= 0.5:
1283 raise ValueError(
1284 f"if gamma is a number, must greater than 0.5 and value is picked from (0.5, gamma), got {gamma}"
1285 )
1286 self.gamma = (0.5, gamma)
1287 elif len(gamma) != 2:
1288 raise ValueError("gamma should be a number or pair of numbers.")
1289 else:
1290 self.gamma = (min(gamma), max(gamma))
1291
1292 self.gamma_value: float = 1.0
1293 self.invert_image: bool = invert_image
1294 self.retain_stats: bool = retain_stats
1295
1296 self.adjust_contrast = AdjustContrast(
1297 self.gamma_value, invert_image=self.invert_image, retain_stats=self.retain_stats
1298 )
1299
1300 def randomize(self, data: Any | None = None) -> None:
1301 super().randomize(None)
1302 if not self._do_transform:
1303 return None
1304 self.gamma_value = self.R.uniform(low=self.gamma[0], high=self.gamma[1])

Callers 6

__init__Method · 0.90
test_use_caseMethod · 0.90
test_correct_resultsMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_use_caseMethod · 0.72
test_correct_resultsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…