예제 #1
0
def resample_type3(image_folder):
    image = read_dicom_series(image_folder)
    dcm_file = glob(os.path.join(image_folder, '*.dcm'))[0]
    dcm = dicom.read_file(dcm_file)
    assert hasattr(dcm, 'WindowCenter') and hasattr(dcm, 'WindowWidth'),\
        'Needs window and level for this resampling.'
    center = dcm.WindowCenter
    width = dcm.WindowWidth
    lower_bound = center - (width - 1) / 2
    upper_bound = center + (width - 1) / 2

    image = sitk.IntensityWindowing(image, lower_bound, upper_bound, 0, 255)

    image = sitk.Cast(image, sitk.sitkUInt8)
    resampled_image = resample_sitk_image(image,
                                          spacing=(0.2, 0.2, 1),
                                          interpolator='linear',
                                          fill_value=0)
    sitk.WriteImage(resampled_image, 'im_resampled_linear.nrrd')
def resample_type4(image_folder, sigma):
    """Apply a Gaussian filter before downsampling.
    By the Nyquist-Shannon theorem we can only reliably reproduce
    information up to the Nyquist frequency. As this frequency
    is scaled by the same factor as the downsampling, we cannot have
    frequencies above alpha*Nyquist frequence in the image.
    We use a Gaussian filter as a low-pass filter.

    As the Gaussian is the same in the frequency domain,
    we can reduce the amount of information in a 85um grid,
    to one of 200um.
    Approximately, sigma = 85/200 *1/(2*sqrt(pi)), where the normalization
    is from the choice of Gaussian. Hence sigma ~ 0.11, or ~1.412 image units.
    """
    image = read_dicom_series(image_folder)
    # 0,1,2 <-> (x,y,z)
    image = sitk.RecursiveGaussian(image, sigma=sigma * 0.2, direction=0)
    image = sitk.RecursiveGaussian(image, sigma=sigma * 0.2, direction=1)

    dcm_file = glob(os.path.join(image_folder, '*.dcm'))[0]
    dcm = dicom.read_file(dcm_file)
    assert hasattr(dcm, 'WindowCenter') and hasattr(dcm, 'WindowWidth'),\
        'Needs window and level for this resampling.'
    center = dcm.WindowCenter
    width = dcm.WindowWidth
    lower_bound = center - (width - 1) / 2
    upper_bound = center + (width - 1) / 2

    image = sitk.IntensityWindowing(image, lower_bound, upper_bound, 0, 255)
    image = sitk.Cast(image, sitk.sitkUInt8)

    resampled_image = resample_sitk_image(image,
                                          spacing=(0.2, 0.2, 1),
                                          interpolator='linear',
                                          fill_value=0)
    sitk.WriteImage(resampled_image,
                    'im_resampled_gaussian_sigma_{}.nrrd'.format(sigma))