問題描述
我正在使用 SimpleITK
import SimpleITK as sitk
for filename in filenames:
image = sitk.ReadImage(filename)
每個卷都有不同的大小、間距、原點和方向.此代碼為不同的圖像生成不同的值:
Each of the volumes has different size, spacing, origin and direction. This code yields different values for different images:
print(image.GetSize())
print(image.GetOrigin())
print(image.GetSpacing())
print(image.GetDirection())
我的問題是:如何將圖像轉換為具有相同的大小和間距,以便在轉換為 numpy
數組時它們都具有相同的分辨率和大小.比如:
My question is: how do I transform the images to have the same size and spacing so that they all have the same resolution and size when converted to numpy
arrays. Something like:
import SimpleITK as sitk
for filename in filenames:
image = sitk.ReadImage(filename)
image = transform(image, fixed_size, fixed_spacing)
array = sitk.GetArrayFromImage(image)
推薦答案
做到這一點的方法是使用具有固定/任意大小和間距的 Resample 函數.下面是一個代碼片段,展示了這個reference_image"空間的構造:
The way to do this is to use the Resample function with fixed/arbitrary size and spacing. Below is a code snippet showing construction of this "reference_image" space:
reference_origin = np.zeros(dimension)
reference_direction = np.identity(dimension).flatten()
reference_size = [128]*dimension # Arbitrary sizes, smallest size that yields desired results.
reference_spacing = [ phys_sz/(sz-1) for sz,phys_sz in zip(reference_size, reference_physical_size) ]
reference_image = sitk.Image(reference_size, data[0].GetPixelIDValue())
reference_image.SetOrigin(reference_origin)
reference_image.SetSpacing(reference_spacing)
reference_image.SetDirection(reference_direction)
有關交鑰匙解決方案,請查看 此 Jupyter 筆記本 說明了如何在 SimpleITK 中使用可變大小的圖像進行數據增強(上面的代碼來自筆記本).您也可以從 SimpleITK 筆記本存儲庫 中找到其他可用的筆記本.
For a turnkey solution have a look at this Jupyter notebook which illustrates how to do data augmentation with variable sized images in SimpleITK (code above is from the notebook). You may find the other notebooks from the SimpleITK notebook repository of use too.
這篇關于SimpleITK 調整圖像大小的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!