Exemplo n.º 1
0
def write_separate_labels(hard_segmentation_path):
    """ Write binary arrays for each of the labels separately. """

    # construct the binary arrays

    hard_map = oitk.get_itk_array(hard_segmentation_path)

    enhanced = (hard_map == 1).astype('uint8')
    tumor_core = (np.logical_and(enhanced, hard_map == 2)).astype('uint8')
    whole_tumor = (hard_map > 0).astype('uint8')

    # get the paths to write the arrays to

    dirname = os.path.dirname(hard_segmentation_path)
    enhanced_path = os.path.join(dirname, 'enhanced.nii.gz')
    tumor_core_path = os.path.join(dirname, 'tumor_core.nii.gz')
    whole_tumor_path = os.path.join(dirname, 'whole_tumor.nii.gz')

    # write

    itk_proto = oitk.get_itk_image(hard_segmentation_path)
    oitk.write_itk_image(oitk.make_itk_image(enhanced, itk_proto),
                         enhanced_path)
    oitk.write_itk_image(oitk.make_itk_image(tumor_core, itk_proto),
                         tumor_core_path)
    oitk.write_itk_image(oitk.make_itk_image(whole_tumor, itk_proto),
                         whole_tumor_path)
Exemplo n.º 2
0
def get_non_uniform_supervoxels(slic_im, hard_map):
    """ Get supervoxel indices of supervoxels in slic_im 
    which have multiple values in hard_map. """

    # Quick check
    slic_ints = np.unique(slic_im[hard_map != 0])
    a = sitk.LabelStatisticsImageFilter()
    a.Execute(oitk.make_itk_image(hard_map, verbose=False),
              oitk.make_itk_image(slic_im, verbose=False))
    slics_to_split = [i for i in slic_ints \
                      if a.GetSigma(int(i))>0]

    return slics_to_split
Exemplo n.º 3
0
 def run(self, scan, output, mask=None):
     img = oitk.get_itk_image(scan)
     image = oitk.get_itk_array(img)
     if mask is None:
         mask = self.generateMask(image, self.thresh)
     if mask is False:
         mask = np.zeros(image.shape)
         mask[image > 0] = 1
     normalized = self.normalize(image, mask)
     oitk.write_itk_image(oitk.make_itk_image(normalized, proto_image=img),
                          os.path.join(output,
                                       os.path.split(scan)[1]))
Exemplo n.º 4
0
    def save_segm(self):
        """ Save the current custom segmentation into a file."""
        kwargs = {}
        if self.segm_path is not None:
            dirname, basename = os.path.split(self.segm_path)
            if not basename.startswith('corrected_'):
                basename = 'corrected_'+basename
            kwargs['initialfile'] = basename
            kwargs['initialdir'] = dirname
        path = asksaveasfilename(title="Please select a path to save your segmentation",
                                 filetypes=[('Image files',
                                             ('.nii',
                                              '.mha',
                                              '.nii.gz'))],
                                 **kwargs)

        #TODO: change here self.segm_path
        if self.segm_path is not None:
            old_segm = oitk.get_itk_image(self.segm_path)
        image = oitk.make_itk_image(self.segm, old_segm)
        oitk.write_itk_image(image, path)
Exemplo n.º 5
0
def get_borders(supervoxels):
    """ Get supervoxel borders as a binary array. Voxels on the edge of each 
    nonzero-labeled object are 1, all others are set to 0. 
    
    Parameters
    ----------
    supervoxels : np.ndarray
        integer mask of the supervoxels
    
    Returns
    -------
    border_arr : np.ndarray
        array of the same shape as supervoxels, with voxels on the edge of non-zero
        supervoxels set to 1 and all others to zero.
    """

    border_im = sitk.LabelContour(
        oitk.make_itk_image(supervoxels, verbose=False))
    border_arr = oitk.get_itk_array(border_im)
    border_arr = border_arr > 0

    return border_arr