Manipulate performs common image transformations such as cropping, padding and changing resolution.
Usage
crop
takes an image array and crops the array according to the input dimensions.
See the example below:
import pkg_resources
DATA_ROOT = pkg_resources.resource_filename('I2T2', 'data')
# test
knee_data_path = os.path.join(DATA_ROOT,'knee')
dcm_df = dicom_dataframe(path_to_dicom_dir=knee_data_path, dicom_extension='dcm')
image_array = dcm_df.get_pixel_data()
cropped_image = crop(image_array=image_array,
final_dims_in_pixels=[400, 400, 22])
cropped_zero_image = crop(image_array=image_array, final_dims_in_pixels=[
400, 400, 22], zero_fill_mode=True)
# original image
plt.figure(figsize=(16, 16))
plt.subplot(131)
plt.title('Original image')
plt.imshow(image_array[:, :, 6])
# cropped image
plt.subplot(132)
plt.title('Cropped image')
plt.imshow(cropped_image[:, :, 6])
# cropped image zero'd
plt.subplot(133)
plt.title("Cropped zero'd image")
plt.imshow(cropped_zero_image[:, :, 6])
plt.show()
Usage
pad
takes an image array and crops the array according to the input dimensions.
See the example below:
dcm_df = dicom_dataframe(path_to_dicom_dir=knee_data_path, dicom_extension='dcm')
image_array = dcm_df.get_pixel_data()
padded_image = pad(image_array=image_array,
final_dims_in_pixels=[800, 800, 22])
padded_zero_image = pad(image_array=image_array, final_dims_in_pixels=[
800, 800, 22], zero_fill_mode=True)
# original image
plt.figure(figsize=(16, 16))
plt.subplot(131)
plt.title('Original image')
plt.imshow(image_array[:, :, 6])
# padded image
plt.subplot(132)
plt.title('Padded image')
plt.imshow(padded_image[:, :, 6])
# padded image zero'd
plt.subplot(133)
plt.title("Padded zero'd image")
plt.imshow(padded_zero_image[:, :, 6])
plt.show()
Usage
resample
takes an image array and resamples it to the dimensions given in target_pixel_dims_list
.
resample_by
is similar to resample but user gives a compression factor instead (e.g. [0.1,0.1,1.0] will resample the image to 10% in x and y directions)
See the examples below:
dcm_df = dicom_dataframe(path_to_dicom_dir=knee_data_path, dicom_extension='dcm')
image_array = dcm_df.get_pixel_data()
downsampled_image = resample(image_array=image_array, target_pixel_dims_list=[
51, 51, 22], is_seg=False)
upsampled_image = resample(image_array=downsampled_image, target_pixel_dims_list=[
512, 512, 22], is_seg=False)
# Plot
plt.figure(figsize=(16, 16))
plt.subplot(131)
plt.title('Original image')
plt.imshow(image_array[:, :, 6])
plt.subplot(132)
plt.title('Downsampled image')
plt.imshow(downsampled_image[:, :, 6])
plt.subplot(133)
plt.title('Re-upsampled image')
plt.imshow(upsampled_image[:, :, 6])
plt.show()
Example of using resample_by
:
dcm_df = dicom_dataframe(path_to_dicom_dir=knee_data_path, dicom_extension='dcm')
image_array = dcm_df.get_pixel_data()
downsampled_image = resample_by(image_array=image_array,
compression_factor_list=[0.1, 0.1, 1.0],
is_seg=False)
upsampled_image = resample_by(image_array=downsampled_image,
compression_factor_list=[10, 10, 1.0],
is_seg=False)
# Plot
plt.figure(figsize=(16, 16))
plt.subplot(131)
plt.title('Original image')
plt.imshow(image_array[:, :, 6])
plt.subplot(132)
plt.title('Downsampled image')
plt.imshow(downsampled_image[:, :, 6])
plt.subplot(133)
plt.title('Re-upsampled image')
plt.imshow(upsampled_image[:, :, 6])
plt.show()
# read data
segmentation_path = os.path.join(DATA_ROOT,'knee-segmentation','knee_segmentation.h5')
dcm_df = dicom_dataframe(path_to_dicom_dir=knee_data_path, dicom_extension='dcm')
image_array = dcm_df.get_pixel_data()
knee_segmentation = load_h5(segmentation_path)
knee_segmentation = knee_segmentation.get('seg')
print('Knee segmentation dims:', knee_segmentation.shape)
print('Knee original dims:', image_array.shape)
# resample to desired shape
resampled_segmentation = resample(image_array=knee_segmentation,
target_pixel_dims_list=(512, 512, 22),
is_seg=True)
print('Knee segmentation dims after resampling:', resampled_segmentation.shape)
# Plot
from matplotlib.colors import ListedColormap
plt.figure(figsize=(16, 16))
plt.subplot(131)
plt.title('Original image')
plt.imshow(image_array[:, :, 6])
plt.subplot(132)
plt.title('Segmentation image')
plt.imshow(resampled_segmentation[:, :, 6])
colors = ['white', 'purple', 'red', 'green', 'blue']
cmap = ListedColormap(colors)
plt.subplot(133)
plt.title('Overlaid segmentation')
# I would add interpolation='none'
plt.imshow(image_array[:, :, 6], cmap='gray')
plt.imshow(resampled_segmentation[:, :, 6], cmap=cmap,
vmin=0, vmax=4, alpha=0.6) # interpolation='none'
plt.show()
# read data
dcm_df = dicom_dataframe(path_to_dicom_dir=knee_data_path, dicom_extension='dcm')
image_array = dcm_df.get_pixel_data()
knee = load_h5(segmentation_path)
knee_image = knee.get('img')
knee_segmentation = knee.get('seg')
print('Knee segmentation dims:', knee_segmentation.shape)
print('Knee resampled dims:', knee_image.shape)
print('Knee original dims:', image_array.shape)
xres, yres = _get_tag_from_loaded_dicom(dcm_df.dataframe['DS'].loc[0], 'PixelSpacing')
zres = _get_tag_from_loaded_dicom(dcm_df.dataframe['DS'].loc[0], 'SpacingBetweenSlices')
xres, yres, zres = float(xres), float(yres), float(zres)
resized_image = resize(image_array=image_array, original_pixel_spacing_list=[xres, yres, zres], target_pixel_spacing_list=[0.5, 0.5, 0.5])
# Plot
from matplotlib.colors import ListedColormap
plt.figure(figsize=(16, 16))
plt.subplot(131)
plt.title('Original image')
plt.imshow(image_array[:,:,10])
plt.subplot(132)
plt.title('Resized image')
plt.imshow(resized_image[:,:,72])
colors = ['white', 'purple', 'red', 'green', 'blue']
cmap = ListedColormap(colors)
plt.show()