20 """ Example demonstrating a slice-by-slice function decorator """
24 from functools
import wraps
25 import SimpleITK
as sitk
28 def slice_by_slice_decorator(func):
30 A function decorator which executes func on each 3D sub-volume and
31 *in-place* pastes the results into the input image. The input image type
32 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
35 argument and returns an Image as results.
37 :return: A decorated function.
43 def slice_by_slice(image, *args, **kwargs):
45 dim = image.GetDimension()
49 image = func(image, *args, **kwargs)
52 extract_size = list(image.GetSize())
53 extract_size[iter_dim:] = itertools.repeat(0, dim - iter_dim)
55 extract_index = [0] * dim
56 paste_idx = [slice(
None,
None)] * dim
59 extractor.SetSize(extract_size)
61 for high_idx
in itertools.product(
62 *[range(s)
for s
in image.GetSize()[iter_dim:]]
67 extract_index[iter_dim:] = high_idx
68 extractor.SetIndex(extract_index)
74 paste_idx[iter_dim:] = high_idx
75 image[paste_idx] = func(extractor.Execute(image), *args, **kwargs)
83 print(
"Usage: SubDimensionProcess inputImage outputImage", file=sys.stderr)
89 adaptive_histogram_equalization_2d = slice_by_slice_decorator(
90 sitk.AdaptiveHistogramEqualization
93 adaptive_histogram_equalization_2d(inputImage, radius=[20] * 2, alpha=0.3, beta=0.3)