Search code examples
pythonsimpleitkimage-registration

Registering a pair of binary masks


I'm trying to register a pair of binary masks, not a pair of images. To explain further, I'm trying to build a model that automatically strips the brain from the head skull. For the better model accuracy, I already have labeled the brain area using ITK-SNAP. I just wonder if I can do the registration between the binary masks, not the images. Is there any tool for this purpose?


Solution

  • There is a tool that can perform registration of binary masks, platipy.

    You can download from pip, but make sure to update pip first as there are some known issues with dependency resolution with older versions of pip.

    pip install -U pip
    pip install platipy
    

    This software includes functionality for linear (e.g. rigid, affine) and deformable registration.

    Here is a short demo:

    import SimpleITK as sitk
    
    from platipy.imaging.registration import linear_registration
    
    # read in images
    bin_mask_1 = sitk.ReadImage("bin_mask_1.nii.gz", sitk.sitkUInt8)
    bin_mask_2 = sitk.ReadImage("bin_mask_2.nii.gz", sitk.sitkUInt8)
    
    # perform registration
    bin_mask_2_reg, tfm = linear_registration(bin_mask_1, bin_mask_2)
    

    The code in platipy is pretty well documented, but raise an issue in the github repository if you need further help!

    N.B. There are skull-stripping tools in FreeSurfer which you might want to check out.

    If you want to apply this transformation to a new image, adapt this code:

    from platipy.imaging.registration.utils import apply_transform
    
    img_mri = sitk.ReadImage("img_mri.nii.gz")
    
    img_mri_reg = apply_transform(
        input_image=img_mri,
        reference_image=bin_mask_1,
        transform=tfm,
        default_value=0,
        interpolator=sitk.sitkLinear,
    )
    

    Note that the default value should be modified as appropriate (e.g. -1000 for CT images, usually 0 for MRI) and the interpolator can be changed (e.g. sitk.sitkNearestNeighbor for labels, sitk.sitkLinear or sitk.sitkBSpline for images).