Esempio n. 1
0
    def _run_interface(self, runtime):

        in_file = self.inputs.in_file

        uneq_img = nb.load(in_file)
        uneq_data = uneq_img.get_data()

        mask = nb.load(self.inputs.mask_file)
        bool_mask = mask.get_data() > 0
        data_voxels = uneq_data[bool_mask]

        # Do a clip on 2 to 98th percentile
        bottom_2, top_98 = np.percentile(data_voxels,
                                         np.array([1, 99]),
                                         axis=None)
        clipped_b0 = np.clip(data_voxels, 0, top_98)
        eq_data = histeq(clipped_b0, num_bins=512)
        output = np.zeros_like(mask.get_data())
        output[bool_mask] = eq_data
        eq_img = nb.Nifti1Image(output, uneq_img.affine, uneq_img.header)
        self._results['out_file'] = fname_presuffix(self.inputs.in_file,
                                                    suffix='_equalized',
                                                    newpath=runtime.cwd)
        eq_img.to_filename(self._results['out_file'])
        return runtime
Esempio n. 2
0
	#############################################################

	b0_mask, mask = median_otsu(data, , , , , ) # Creating the binary mask and the mask
	mask_img = nib.Nifti1Image(mask.astype(np.float32), img.affine) # Number of bits used, in this case 32, it is saved as float32
	b0_img = nib.Nifti1Image(b0_mask.astype(np.float32), img.affine) # Number of bits used, in this case 32, it is saved as float32
	nib.save(mask_img, subject + '_binary_mask.nii.gz') # Just for necessary
	nib.save(b0_img, subject + '_mask.nii.gz') # Just for necessary
	src = join(home, '.dipy', subject + '_binary_mask.nii.gz') # Where the source file is
	dst = join(dname, subject + "_binary_mask.nii.gz") # The new destination for the file
	shutil.move(src, dst) # Moving the file to the right directory
	src = join(home, '.dipy', subject + '_mask.nii.gz') # Where the source file is
	dst = join(dname, subject + "_mask.nii.gz") # The new destination for the file
	shutil.move(src, dst) # Moving the file to the right directory

	#############################################################
	# 				Plotting					#
	#############################################################
	sli = data.shape[2] // 2
	plt.figure('Brain segmentation')
	plt.subplot(1, 2, 1).set_axis_off()
	plt.imshow(histeq(data[:, :, sli].astype('float')).T,
    cmap='gray', origin='lower')

	plt.subplot(1, 2, 2).set_axis_off()
	plt.imshow(histeq(b0_mask[:, :, sli].astype('float')).T,
    cmap='gray', origin='lower')

  b0_mask_crop, mask_crop = median_otsu(data, 4, 4, autocrop=True) # Crop the outputs to remove the largest possible number of background voxels
  # May save files once decided

  counter+=1 # An additional subject is completed
Esempio n. 3
0
def dwi_to_mask(data,
                subject,
                affine,
                outpath,
                masking='median',
                makefig=False,
                vol_idx=None,
                median_radius=5,
                numpass=6,
                dilate=2,
                forcestart=False,
                header=None,
                verbose=False):

    data = np.squeeze(data)
    binarymask_path = os.path.join(outpath,
                                   subject + '_dwi_binary_mask.nii.gz')
    maskeddwi_path = os.path.join(outpath, subject + '_dwi_mask.nii.gz')
    if not os.path.exists(binarymask_path) and not os.path.exists(
            maskeddwi_path) and not forcestart:
        if masking == 'median':
            b0_mask, mask = median_otsu(data,
                                        vol_idx=vol_idx,
                                        median_radius=median_radius,
                                        numpass=numpass,
                                        dilate=dilate)
        if masking == 'extract':
            if np.size(np.shape(data)) == 3:
                mask = data > 0
            if np.size(np.shape(data)) == 4:
                mask = data[:, :, :, 0] > 0
            mask = mask.astype(np.float32)
            b0_mask = applymask_array(data, mask)

        if verbose:
            txt = f"Creating binarymask at {binarymask_path} and masked data at {maskeddwi_path}"
            print(txt)
        if header is None:
            save_nifti(binarymask_path, mask, affine)
            save_nifti(maskeddwi_path, b0_mask.astype(np.float32), affine)
        else:
            binarymask_nii = nib.Nifti1Image(mask, affine, header)
            nib.save(binarymask_nii, binarymask_path)
            maskeddwi_nii = nib.Nifti1Image(b0_mask, affine, header)
            nib.save(maskeddwi_nii, maskeddwi_path)
    else:
        mask = load_nifti(binarymask)
        mask = mask[0]
        b0_mask = load_nifti(maskeddwi)
        b0_mask = b0_mask[0]

    if makefig:
        sli = data.shape[2] // 2
        if len(b0_mask.shape) == 4:
            b0_mask_2 = b0_mask[:, :, :, 0]
        else:
            b0_mask_2 = b0_mask
        if len(data.shape) == 4:
            data = data[:, :, :, 0]
        plt.figure('Brain segmentation')
        plt.subplot(1, 2, 1).set_axis_off()
        plt.imshow(histeq(data[:, :, sli].astype('float')).T,
                   cmap='gray',
                   origin='lower')

        plt.subplot(1, 2, 2).set_axis_off()
        plt.imshow(histeq(b0_mask_2[:, :, sli].astype('float')).T,
                   cmap='gray',
                   origin='lower')
        plt.savefig(outpath + 'median_otsu.png')

    return (mask.astype(np.float32), b0_mask.astype(np.float32))
Esempio n. 4
0
fname = 'se_1.5t'
nib.save(mask_img, fname + '_binary_mask.nii.gz')
nib.save(b0_img, fname + '_mask.nii.gz')

"""
Quick view of the results middle slice using matplotlib.
"""

import matplotlib.pyplot as plt
from dipy.core.histeq import histeq

sli = data.shape[2] / 2
plt.figure('Brain segmentation')
plt.subplot(1, 2, 1).set_axis_off()
plt.imshow(histeq(data[:, :, sli].astype('float')).T,
           cmap='gray', origin='lower')

plt.subplot(1, 2, 2).set_axis_off()
plt.imshow(histeq(b0_mask[:, :, sli].astype('float')).T,
           cmap='gray', origin='lower')

plt.savefig('median_otsu.png')

"""
.. figure:: median_otsu.png
   :align: center

   **An application of median_otsu for brain segmentation**.

``median_otsu`` can also automatically crop the outputs to remove the largest
Esempio n. 5
0
fname = 'se_1.5t'
save_nifti(fname + '_binary_mask.nii.gz', mask.astype(np.float32), affine)
save_nifti(fname + '_mask.nii.gz', b0_mask.astype(np.float32), affine)
"""
Quick view of the results middle slice using ``matplotlib``.
"""

import matplotlib.pyplot as plt
from dipy.core.histeq import histeq

sli = data.shape[2] // 2
plt.figure('Brain segmentation')
plt.subplot(1, 2, 1).set_axis_off()
plt.imshow(
    histeq(cp.asnumpy(data[:, :, sli]).astype('float')).T,
    cmap='gray',
    origin='lower',
)

plt.subplot(1, 2, 2).set_axis_off()
plt.imshow(histeq(b0_mask[:, :, sli].astype('float')).T,
           cmap='gray',
           origin='lower')

plt.savefig('median_otsu.png')
"""
.. figure:: median_otsu.png
   :align: center

   An application of median_otsu for brain segmentation.