通用约定

image.pngimage.png
image.png

图像操作

  1. img1 = sitk.Image(24,24, sitk.sitkUInt8)
  2. img1[0,0] = 0
  3. img2 = sitk.Image(img1.GetSize(), sitk.sitkUInt8)
  4. img2.SetDirection([0,1,0.5,0.5])
  5. img2.SetSpacing([0.5,0.8])
  6. img2.SetOrigin([0.000001,0.000001])
  7. img2[0,0] = 255
  8. img3 = img1 + img2
  9. print(img3[0,0])

访问和索引切片

Slicing of SimpleITK images returns a copy of the image data.This is similar to slicing Python lists and differs from the “view” returned by slicing numpy arrays.
image.png

numpy的和SimpleITK之间的转换:

  1. SimpleITKnumpy的索引访问的顺序相反!
  2. SimpleITK:图像[XYZ]
  3. numpy的:image_numpy_array [ZYX]

1. 从SimplTK到numpy

image.png

2. 从numpy到SimpleITK

image.png

3. 区别于scikit-image和numpy的转换:

image.png

读写图像

  1. SimpleITK可以读写存储在单个文件或一组文件(例如DICOM系列)中的图像。
  2. DICOM格式存储的图像具有与之关联的元数据字典,其中包括了DICOM标签。
  3. 当将DICOM系列被读为单个图像时,元数据信息不可用,因为DICOM标签是针对每个单独文件的。
  4. 如果需要元数据,可以使用面向对象的接口的ImageSeriesReader类,或者ImageFileReader类,
  5. 设置特定切片的文件名,访问元数据图像使用GetMetaDataKeysHasMetaDataKeyGetMetaData

例如,读取DICOM格式的图像,并将其写为mhd。文件格式由文件扩展名推导出。还设置了适当的像素类型-您可以覆盖它并强制选择您要的像素类型。

  1. data_directory = os.path.dirname(fdata("CIRS057A_MR_CT_DICOM/readme.txt"))
  2. series_ID = '1.2.840.113619.2.290.3.3233817346.783.1399004564.515'
  3. # Get the list of files belonging to a specific series ID.
  4. reader = sitk.ImageSeriesReader()
  5. # Use the functional interface to read the image series.
  6. original_image = sitk.ReadImage(reader.GetGDCMSeriesFileNames(data_directory, series_ID))
  7. # Write the image.
  8. output_file_name_3D = os.path.join(OUTPUT_DIR, '3DImage.mha')
  9. sitk.WriteImage(original_image, output_file_name_3D)
  10. # Read it back again.
  11. written_image = sitk.ReadImage(output_file_name_3D)
  12. # Check that the original and written image are the same.
  13. statistics_image_filter = sitk.StatisticsImageFilter()
  14. statistics_image_filter.Execute(original_image - written_image)
  15. # Check that the original and written files are the same
  16. print('Max, Min differences are : {0}, {1}'.format(statistics_image_filter.GetMaximum(),
  17. statistics_image_filter.GetMinimum()))

Fetching CIRS057A_MR_CT_DICOM/readme.txt
Max, Min differences are : 0.0, 0.0
将图像写为jpg格式,并且需要重新调整图像强度(默认为[0,255]),因为JPEG格式的显示需要转换为UInt8像素类型。

  1. sitk.WriteImage(sitk.Cast(sitk.RescaleIntensity(written_image), sitk.sitkUInt8),
  2. [os.path.join(OUTPUT_DIR, 'slice{0:03d}.jpg'.format(i))
  3. for i in range(written_image.GetSize()[2])])

图像显示

  1. mr_image = sitk.ReadImage(fdata('training_001_mr_T1.mha'))
  2. npa = sitk.GetArrayViewFromImage(mr_image)
  3. # Display the image slice from the middle of the stack, z axis
  4. z = int(mr_image.GetDepth()/2)
  5. npa_zslice = sitk.GetArrayViewFromImage(mr_image)[z,:,:]
  6. # Three plots displaying the same data, how do we deal with the high dynamic range?
  7. fig = plt.figure()
  8. fig.set_size_inches(15,30)
  9. fig.add_subplot(1,3,1)
  10. plt.imshow(npa_zslice)
  11. plt.title('default colormap')
  12. plt.axis('off')
  13. fig.add_subplot(1,3,2)
  14. plt.imshow(npa_zslice,cmap=plt.cm.Greys_r);
  15. plt.title('grey colormap')
  16. plt.axis('off')
  17. fig.add_subplot(1,3,3)
  18. plt.title('grey colormap,\n scaling based on volumetric min and max values')
  19. plt.imshow(npa_zslice,cmap=plt.cm.Greys_r, vmin=npa.min(), vmax=npa.max())
  20. plt.axis('off');

image.png

参考:

【1】SimpleITK图像基础
【2】SimpleITK-Notebooks-03_Image_Details
【3】scikit-image中的numpy速成
【4】SITK Documentation docs