コード例 #1
0
    def convert(
            picklable_brain_image: PicklableBrainImage
    ) -> structure.BrainImage:
        """Converts a :class:`PicklableBrainImage` to :class:`BrainImage <data.structure.BrainImage>`.

        Args:
            picklable_brain_image (PicklableBrainImage): A pickable brain image.

        Returns:
            BrainImage: The brain image.
        """

        images = {}
        for key, np_img in picklable_brain_image.np_images.items():
            images[key] = conversion.NumpySimpleITKImageBridge.convert(
                np_img, picklable_brain_image.image_properties)

        feature_images = {}
        for key, np_feat_img in picklable_brain_image.np_feature_images.items(
        ):
            feature_images[key] = conversion.NumpySimpleITKImageBridge.convert(
                np_feat_img, picklable_brain_image.image_properties)

        brain_image = structure.BrainImage(picklable_brain_image.id_,
                                           picklable_brain_image.path, images)
        brain_image.feature_matrix = picklable_brain_image.feature_matrix
        return brain_image
コード例 #2
0
ファイル: pipeline_utilities.py プロジェクト: Jonas55/MIALab
def pre_process(id_: str, paths: dict, **kwargs) -> structure.BrainImage:
    """Loads and processes an image.

    The processing includes:

    - Registration
    - Pre-processing
    - Feature extraction

    Args:
        id_ (str): An image identifier.
        paths (dict): A dict, where the keys are an image identifier of type structure.BrainImageTypes
            and the values are paths to the images.

    Returns:
        (structure.BrainImage):
    """

    print('-' * 10, 'Processing', id_)

    # load image
    path = paths.pop(
        id_, '')  # the value with key id_ is the root directory of the image
    img = {img_key: sitk.ReadImage(path) for img_key, path in paths.items()}
    img = structure.BrainImage(id_, path, img)

    # construct T1 pipeline
    pipeline_t1 = fltr.FilterPipeline()
    if kwargs.get('zscore_pre', False):
        pipeline_t1.add_filter(fltr_prep.NormalizeZScore())
    if kwargs.get('registration_pre', False):
        pipeline_t1.add_filter(fltr_reg.MultiModalRegistration())
        pipeline_t1.set_param(fltr_reg.MultiModalRegistrationParams(atlas_t1),
                              1)

    # execute pipeline on T1 image
    img.images[structure.BrainImageTypes.T1] = pipeline_t1.execute(
        img.images[structure.BrainImageTypes.T1])

    # construct T2 pipeline
    pipeline_t2 = fltr.FilterPipeline()
    if kwargs.get('zscore_pre', False):
        pipeline_t2.add_filter(fltr_prep.NormalizeZScore())

    # execute pipeline on T2 image
    img.images[structure.BrainImageTypes.T2] = pipeline_t2.execute(
        img.images[structure.BrainImageTypes.T2])

    if kwargs.get('registration_pre', False):
        # get transformation
        transform = pipeline_t1.filters[1].transform

        # apply transformation of T1 image registration to T2 image
        image_t2 = img.images[structure.BrainImageTypes.T2]
        image_t2 = sitk.Resample(image_t2, atlas_t1, transform,
                                 sitk.sitkLinear, 0.0,
                                 image_t2.GetPixelIDValue())
        img.images[structure.BrainImageTypes.T2] = image_t2

        # apply transformation of T1 image registration to ground truth
        image_ground_truth = img.images[structure.BrainImageTypes.GroundTruth]
        image_ground_truth = sitk.Resample(
            image_ground_truth, atlas_t1, transform, sitk.sitkNearestNeighbor,
            0, image_ground_truth.GetPixelIDValue())
        img.images[structure.BrainImageTypes.GroundTruth] = image_ground_truth

        # update image properties to atlas image properties after registration
        img.image_properties = conversion.ImageProperties(atlas_t1)

    # extract the features
    feature_extractor = FeatureExtractor(img, **kwargs)
    img = feature_extractor.execute()

    img.feature_images = {}

    return img
コード例 #3
0
def pre_process(id_: str, paths: dict, **kwargs) -> structure.BrainImage:
    """Loads and processes an image.

    The processing includes:

    - Registration
    - Pre-processing
    - Feature extraction

    Args:
        id_ (str): An image identifier.
        paths (dict): A dict, where the keys are an image identifier of type structure.BrainImageTypes
            and the values are paths to the images.

    Returns:
        (structure.BrainImage):
    """

    print('-' * 10, 'Processing', id_)

    # load image
    path = paths.pop(
        id_, '')  # the value with key id_ is the root directory of the image
    path_to_transform = paths.pop(
        structure.BrainImageTypes.RegistrationTransform, '')
    img = {img_key: sitk.ReadImage(path) for img_key, path in paths.items()}
    transform = sitk.ReadTransform(path_to_transform)
    img = structure.BrainImage(id_, path, img, transform)

    # construct pipeline for brain mask registration
    # we need to perform this before the T1w and T2w pipeline because the registered mask is used for skull-stripping
    pipeline_brain_mask = fltr.FilterPipeline()
    if kwargs.get('registration_pre', False):
        pipeline_brain_mask.add_filter(fltr_prep.ImageRegistration())
        pipeline_brain_mask.set_param(
            fltr_prep.ImageRegistrationParameters(atlas_t1, img.transformation,
                                                  True),
            len(pipeline_brain_mask.filters) - 1)

    # execute pipeline on the brain mask image
    img.images[
        structure.BrainImageTypes.BrainMask] = pipeline_brain_mask.execute(
            img.images[structure.BrainImageTypes.BrainMask])

    # construct pipeline for T1w image pre-processing
    pipeline_t1 = fltr.FilterPipeline()
    if kwargs.get('registration_pre', False):
        pipeline_t1.add_filter(fltr_prep.ImageRegistration())
        pipeline_t1.set_param(
            fltr_prep.ImageRegistrationParameters(atlas_t1,
                                                  img.transformation),
            len(pipeline_t1.filters) - 1)
    if kwargs.get('skullstrip_pre', False):
        pipeline_t1.add_filter(fltr_prep.SkullStripping())
        pipeline_t1.set_param(
            fltr_prep.SkullStrippingParameters(
                img.images[structure.BrainImageTypes.BrainMask]),
            len(pipeline_t1.filters) - 1)
    if kwargs.get('normalization_pre', False):
        pipeline_t1.add_filter(fltr_prep.ImageNormalization())

    # execute pipeline on the T1w image
    img.images[structure.BrainImageTypes.T1w] = pipeline_t1.execute(
        img.images[structure.BrainImageTypes.T1w])

    # construct pipeline for T2w image pre-processing
    pipeline_t2 = fltr.FilterPipeline()
    if kwargs.get('registration_pre', False):
        pipeline_t2.add_filter(fltr_prep.ImageRegistration())
        pipeline_t2.set_param(
            fltr_prep.ImageRegistrationParameters(atlas_t2,
                                                  img.transformation),
            len(pipeline_t2.filters) - 1)
    if kwargs.get('skullstrip_pre', False):
        pipeline_t2.add_filter(fltr_prep.SkullStripping())
        pipeline_t2.set_param(
            fltr_prep.SkullStrippingParameters(
                img.images[structure.BrainImageTypes.BrainMask]),
            len(pipeline_t2.filters) - 1)
    if kwargs.get('normalization_pre', False):
        pipeline_t2.add_filter(fltr_prep.ImageNormalization())

    # execute pipeline on the T2w image
    img.images[structure.BrainImageTypes.T2w] = pipeline_t2.execute(
        img.images[structure.BrainImageTypes.T2w])

    # construct pipeline for ground truth image pre-processing
    pipeline_gt = fltr.FilterPipeline()
    if kwargs.get('registration_pre', False):
        pipeline_gt.add_filter(fltr_prep.ImageRegistration())
        pipeline_gt.set_param(
            fltr_prep.ImageRegistrationParameters(atlas_t1, img.transformation,
                                                  True),
            len(pipeline_gt.filters) - 1)

    # execute pipeline on the ground truth image
    img.images[structure.BrainImageTypes.GroundTruth] = pipeline_gt.execute(
        img.images[structure.BrainImageTypes.GroundTruth])

    # update image properties to atlas image properties after registration
    img.image_properties = conversion.ImageProperties(
        img.images[structure.BrainImageTypes.T1w])

    # extract the features
    feature_extractor = FeatureExtractor(img, **kwargs)

    img = feature_extractor.execute()

    img.feature_images = {
    }  # we free up memory because we only need the img.feature_matrix
    # for training of the classifier

    return img