20 from __future__
import print_function
22 import SimpleITK
as sitk
26 from functools
import wraps
29 def slice_by_slice_decorator(func):
31 A function decorator which executes func on each 3D sub-volume and *in-place* pastes the results into the
32 input image. The input image type and the output image type are required to be the same type.
34 :param func: A function which take a SimpleITK Image as it's first argument and returns an Image as results.
35 :return: A decorated function.
41 def slice_by_slice(image, *args, **kwargs):
43 dim = image.GetDimension()
47 image = func(image, *args, **kwargs)
50 extract_size = list(image.GetSize())
51 extract_size[iter_dim:] = itertools.repeat(0, dim-iter_dim)
53 extract_index = [0] * dim
54 paste_idx = [slice(
None,
None)] * dim
57 extractor.SetSize(extract_size)
59 for high_idx
in itertools.product(*[range(s)
for s
in image.GetSize()[iter_dim:]]):
63 extract_index[iter_dim:] = high_idx
64 extractor.SetIndex(extract_index)
69 paste_idx[iter_dim:] = high_idx
70 image[paste_idx] = func(extractor.Execute(image), *args, **kwargs)
78 print(
"Usage: SubDimensionProcess inputImage outputImage", file=sys.stderr)
84 adaptive_histogram_equalization_2d = slice_by_slice_decorator(sitk.AdaptiveHistogramEqualization)
86 adaptive_histogram_equalization_2d(inputImage, radius=[20]*2, alpha=0.3, beta=0.3)