Example #1
0
    def get_resampled_stack(self,
                            resampling_grid=None,
                            spacing=None,
                            interpolator="Linear",
                            default_pixel_value=0.0,
                            filename=None):

        if (resampling_grid is None and spacing is None) or \
                (resampling_grid is not None and spacing is not None):
            raise IOError(
                "Either 'resampling_grid' or 'spacing' must be specified")

        # Get SimpleITK-interpolator
        try:
            interpolator_str = interpolator
            interpolator = eval("sitk.sitk" + interpolator_str)
        except:
            raise ValueError("Error: interpolator is not known. "
                             "Must fit sitk.InterpolatorEnum format. "
                             "Possible examples include "
                             "'NearestNeighbor', 'Linear', or 'BSpline'.")

        if resampling_grid is not None:
            resampled_stack_sitk = sitk.Resample(self.sitk, resampling_grid,
                                                 sitk.Euler3DTransform(),
                                                 interpolator,
                                                 default_pixel_value,
                                                 self.sitk.GetPixelIDValue())

            resampled_stack_sitk_mask = sitk.Resample(
                self.sitk_mask, resampling_grid, sitk.Euler3DTransform(),
                sitk.sitkNearestNeighbor, 0, self.sitk_mask.GetPixelIDValue())
        else:
            resampler = simplereg.resampler.Resampler
            resampled_stack_sitk = resampler.get_resampled_image_sitk(
                image_sitk=self.sitk,
                spacing=spacing,
                interpolator=interpolator,
                padding=default_pixel_value,
                add_to_grid_unit="mm",
            )
            resampled_stack_sitk_mask = resampler.get_resampled_image_sitk(
                image_sitk=self.sitk_mask,
                spacing=spacing,
                interpolator=sitk.sitkNearestNeighbor,
                padding=0,
                add_to_grid_unit="mm",
            )

        # Create Stack instance
        if filename is None:
            filename = self._filename + "_" + interpolator_str
        stack = self.from_sitk_image(
            image_sitk=resampled_stack_sitk,
            slice_thickness=resampled_stack_sitk.GetSpacing()[-1],
            filename=filename,
            image_sitk_mask=resampled_stack_sitk_mask,
        )

        return stack
Example #2
0
    def get_isotropically_resampled_stack(self,
                                          resolution=None,
                                          interpolator="Linear",
                                          extra_frame=0,
                                          filename=None,
                                          mask_dilation_radius=0,
                                          mask_dilation_kernel="Ball"):

        # Choose interpolator
        try:
            interpolator_str = interpolator
            interpolator = eval("sitk.sitk" + interpolator_str)
        except:
            raise ValueError("Error: interpolator is not known")

        if resolution is None:
            spacing = self.sitk.GetSpacing()[0]
        else:
            spacing = resolution

        # Resample image and its mask to isotropic grid
        resampler = simplereg.resampler.Resampler
        isotropic_resampled_stack_sitk = resampler.get_resampled_image_sitk(
            image_sitk=self.sitk,
            spacing=spacing,
            interpolator=interpolator,
            padding=0.0,
            add_to_grid=extra_frame,
            add_to_grid_unit="mm",
        )
        isotropic_resampled_stack_sitk_mask = resampler.get_resampled_image_sitk(
            image_sitk=self.sitk_mask,
            spacing=spacing,
            interpolator=sitk.sitkNearestNeighbor,
            padding=0,
            add_to_grid=extra_frame,
            add_to_grid_unit="mm",
        )

        if mask_dilation_radius > 0:
            dilater = sitk.BinaryDilateImageFilter()
            dilater.SetKernelType(eval("sitk.sitk" + mask_dilation_kernel))
            dilater.SetKernelRadius(mask_dilation_radius)
            isotropic_resampled_stack_sitk_mask = dilater.Execute(
                isotropic_resampled_stack_sitk_mask)

        # Create Stack instance
        if filename is None:
            filename = self._filename + "_" + interpolator_str + "Iso"
        stack = self.from_sitk_image(
            image_sitk=isotropic_resampled_stack_sitk,
            filename=filename,
            slice_thickness=isotropic_resampled_stack_sitk.GetSpacing()[-1],
            image_sitk_mask=isotropic_resampled_stack_sitk_mask,
        )

        return stack