ROI_np)  #N.B GetImageFromArray cambia lo spacing e l'origine :(

ROI_sitk_1.SetOrigin(ROI_origin)
ROI_sitk_1.SetSpacing(ROI_spacing)

ROI_size1 = ROI_sitk_1.GetSize()
ROI_spacing1 = ROI_sitk_1.GetSpacing()
ROI_depth1 = ROI_sitk_1.GetDepth()
ROI_dimension1 = ROI_sitk_1.GetDimension()
ROI_direction1 = ROI_sitk_1.GetDirection()
ROI_numberofpixels1 = ROI_sitk_1.GetNumberOfPixels()
ROI_origin1 = ROI_sitk_1.GetOrigin()

from radiomics import imageoperations

ROI_sitk_2 = imageoperations.getMask(ROI_sitk_1, label=1)

ROI_size2 = ROI_sitk_2.GetSize()
ROI_spacing2 = ROI_sitk_2.GetSpacing()
ROI_depth2 = ROI_sitk_2.GetDepth()
ROI_dimension2 = ROI_sitk_2.GetDimension()
ROI_direction2 = ROI_sitk_2.GetDirection()
ROI_numberofpixels2 = ROI_sitk_2.GetNumberOfPixels()
ROI_origin2 = ROI_sitk_2.GetOrigin()

#Estrarre le features using pyradiomics

from radiomics import featureextractor

params = os.path.join('/home/leonardo/pyradiomics', "examples",
                      "exampleSettings", "Params.yaml")
Beispiel #2
0
    def loadImage(ImageFilePath,
                  MaskFilePath,
                  otherImageFilePath,
                  generalInfo=None,
                  **kwargs):
        """
    Load and pre-process the image and labelmap.
    If ImageFilePath is a string, it is loaded as SimpleITK Image and assigned to ``image``,
    if it already is a SimpleITK Image, it is just assigned to ``image``.
    All other cases are ignored (nothing calculated).
    Equal approach is used for assignment of ``mask`` using MaskFilePath. If necessary, a segmentation object (i.e. mask
    volume with vector-image type) is then converted to a labelmap (=scalar image type). Data type is forced to UInt32.
    See also :py:func:`~imageoperations.getMask()`.

    If normalizing is enabled image is first normalized before any resampling is applied.

    If resampling is enabled, both image and mask are resampled and cropped to the tumor mask (with additional
    padding as specified in padDistance) after assignment of image and mask.

    :param ImageFilePath: SimpleITK.Image object or string pointing to SimpleITK readable file representing the image
                          to use.
    :param MaskFilePath: SimpleITK.Image object or string pointing to SimpleITK readable file representing the mask
                         to use.
    :param generalInfo: GeneralInfo Object. If provided, it is used to store diagnostic information of the
                        pre-processing.
    :param kwargs: Dictionary containing the settings to use for this particular image type.
    :return: 2 SimpleITK.Image objects representing the loaded image and mask, respectively.
    """
        global logger

        normalize = kwargs.get('normalize', False)
        interpolator = kwargs.get('interpolator')
        resampledPixelSpacing = kwargs.get('resampledPixelSpacing')
        preCrop = kwargs.get('preCrop', False)
        label = kwargs.get('label', 1)

        logger.info('Loading image and mask')
        if isinstance(ImageFilePath,
                      six.string_types) and os.path.isfile(ImageFilePath):
            image = sitk.ReadImage(ImageFilePath)
        elif isinstance(ImageFilePath, sitk.SimpleITK.Image):
            image = ImageFilePath
        else:
            raise ValueError(
                'Error reading image Filepath or SimpleITK object')

        if isinstance(MaskFilePath,
                      six.string_types) and os.path.isfile(MaskFilePath):
            mask = sitk.ReadImage(MaskFilePath)
        elif isinstance(MaskFilePath, sitk.SimpleITK.Image):
            mask = MaskFilePath
        else:
            raise ValueError('Error reading mask Filepath or SimpleITK object')

        # Read other image for washin
        if otherImageFilePath is not None:
            if isinstance(
                    otherImageFilePath,
                    six.string_types) and os.path.isfile(otherImageFilePath):
                imageOther = sitk.ReadImage(otherImageFilePath)
            elif isinstance(otherImageFilePath, sitk.SimpleITK.Image):
                imageOther = otherImageFilePath
            else:
                raise ValueError(
                    'Error reading image Filepath or SimpleITK object')

            # Align input types
            caster = sitk.CastImageFilter()
            caster.SetOutputPixelType(sitk.sitkFloat64)
            image = caster.Execute(image)
            imageOther = caster.Execute(imageOther)

            # t1 - t0
            f_subtract = sitk.SubtractImageFilter()
            wash = f_subtract.Execute(imageOther, image)
            # (t1 - t0) / t0
            f_divide = sitk.DivideImageFilter()
            wash = f_divide.Execute(wash, image)

            # Replace max Values with 0
            f_less = sitk.LessEqualImageFilter()
            value_mask = f_less.Execute(wash, sys.float_info.max / 2)
            f_mask = sitk.MaskImageFilter()
            image = f_mask.Execute(wash, value_mask)

        # process the mask
        mask = imageoperations.getMask(mask, **kwargs)

        if generalInfo is not None:
            generalInfo.addImageElements(image)
            # Do not include the image here, as the overlap between image and mask have not been checked
            # It is therefore possible that image and mask do not align, or even have different sizes.
            generalInfo.addMaskElements(None, mask, label)

        # This point is only reached if image and mask loaded correctly
        if normalize:
            image = imageoperations.normalizeImage(image, **kwargs)

        if interpolator is not None and resampledPixelSpacing is not None:
            image, mask = imageoperations.resampleImage(image, mask, **kwargs)
            if generalInfo is not None:
                generalInfo.addImageElements(image, 'interpolated')
                generalInfo.addMaskElements(image, mask, label, 'interpolated')

        elif preCrop:
            bb, correctedMask = imageoperations.checkMask(
                image, mask, **kwargs)
            if correctedMask is not None:
                # Update the mask if it had to be resampled
                mask = correctedMask
            if bb is None:
                # Mask checks failed
                raise ValueError('Mask checks failed during pre-crop')

            image, mask = imageoperations.cropToTumorMask(
                image, mask, bb, **kwargs)

        return image, mask
Beispiel #3
0
    def loadImage(self, ImageFilePath, MaskFilePath):
        """
    Preprocess the image and labelmap.
    If ImageFilePath is a string, it is loaded as SimpleITK Image and assigned to image,
    if it already is a SimpleITK Image, it is just assigned to image.
    All other cases are ignored (nothing calculated).
    Equal approach is used for assignment of mask using MaskFilePath.

    If normalizing is enabled image is first normalized before any resampling is applied.

    If resampling is enabled, both image and mask are resampled and cropped to the tumor mask (with additional
    padding as specified in padDistance) after assignment of image and mask.
    """
        normalize = self.settings.get('normalize', False)
        interpolator = self.settings.get('interpolator')
        resampledPixelSpacing = self.settings.get('resampledPixelSpacing')
        preCrop = self.settings.get('preCrop', False)
        label = self.settings.get('label', 1)

        self.logger.info('Loading image and mask')
        if isinstance(ImageFilePath,
                      six.string_types) and os.path.isfile(ImageFilePath):
            image = sitk.ReadImage(ImageFilePath)
        elif isinstance(ImageFilePath, sitk.SimpleITK.Image):
            image = ImageFilePath
        else:
            raise ValueError(
                'Error reading image Filepath or SimpleITK object')

        if isinstance(MaskFilePath,
                      six.string_types) and os.path.isfile(MaskFilePath):
            mask = sitk.ReadImage(MaskFilePath)
        elif isinstance(MaskFilePath, sitk.SimpleITK.Image):
            mask = MaskFilePath
        else:
            raise ValueError('Error reading mask Filepath or SimpleITK object')

        # process the mask
        mask = imageoperations.getMask(mask, **self.settings)

        if self.generalInfo is not None:
            self.generalInfo.addImageElements(image)
            # Do not include the image here, as the overlap between image and mask have not been checked
            # It is therefore possible that image and mask do not align, or even have different sizes.
            self.generalInfo.addMaskElements(None, mask, label)

        # This point is only reached if image and mask loaded correctly
        if normalize:
            image = imageoperations.normalizeImage(image, **self.settings)

        if interpolator is not None and resampledPixelSpacing is not None:
            image, mask = imageoperations.resampleImage(
                image, mask, **self.settings)
            if self.generalInfo is not None:
                self.generalInfo.addImageElements(image, 'interpolated')
                self.generalInfo.addMaskElements(image, mask,
                                                 self.settings.get('label', 1),
                                                 'interpolated')

        elif preCrop:
            bb, correctedMask = imageoperations.checkMask(
                image, mask, **self.settings)
            if correctedMask is not None:
                # Update the mask if it had to be resampled
                mask = correctedMask
            if bb is None:
                # Mask checks failed
                raise ValueError('Mask checks failed during pre-crop')

            image, mask = imageoperations.cropToTumorMask(
                image, mask, bb, **self.settings)

        return image, mask