医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

本节大纲

sitk图像的构造

sitk中的常见属性值

像素的相关操作

SimpleITK和Numpy的相互转换

类型转化中的index顺序

sitk图像的构造

sitk有几种构建图像的方式,可以构建2D或者RGB格式的图像

image_2D = sitk.Image(64, 64, sitk.sitkFloat32)
image_2D = sitk.Image([32,32], sitk.sitkUInt32)
image_RGB = sitk.Image([128,128], sitk.sitkVectorUInt8, 3)

其中Uint代表无符号整数,即unsigned

sitk中的常见属性值

print(image.GetSize())
print(image.GetOrigin())
print(image.GetSpacing())
print(image.GetDirection())
print(image.GetNumberOfComponentsPerPixel())

sitk中有以上五种常见的属性,分别可以使用get的方式获取

其中Size代表图像的大小,为图像的每一个维度的长度,即像素点的多少

Origin为图像的原点坐标

Space的代表的是体素或像素之间的间距。体素可以理解为像素在三维立体图像中的另一种称呼,即每个像素点或者体素点之间的距离,通常单位为mm,在医学影像中,由于不同的机器设备不同,其体素间距也不完全相同,所以在处理图像前一般需要对其进行重采样。Spacing的更加直观理解可以参照下图:

医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

Direction则是指图像的方向,由于图像未必是正交,所以会用方向向量来指明不同轴的方向。

GetNumberOfComponentsPerPixel(): 这个方法的字面意思是每一个像素是由几个部分组成的,由于sitk中包含一种数据类型为Vector类型,对于三维RGB类型,该属性值为3,因为其由RGB三个元素组成:

image_RGB = sitk.Image([128,128], sitk.sitkVectorUInt8, 3)

如上一节中定义的VectorUInt,其虽然定义的为128*128的维度,但是由于其中数据类型为VectorUint,所以其输出为3.

同时我们可以通过下面代码获得一个图像的长度、宽度以及深度

print(image.GetWidth())
print(image.GetHeight())
print(image.GetDepth())

医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

我们还可以获取图像的维度和数据类型,其中三维图像的维度为3,RGB图像虽然属于三通道图像,但是其依旧是二维的彩色图像,所以他的维度是2.

Pixel ID的指的是图像中像素点的数据类型,会使用Pixel ID来表示,为数据类型的一个数字标识,如果我们想把这个ID转成文字,可以使用GetPixelIDTypeAsString(),会直接输出数据类型。

print(image.GetDimension())
print(image_RGB.GetDimension())
Pixel ID means the data type ID in an image.

print(image.GetPixelIDValue())
GetPixelIDTypeAsString convert type ID to type name
print(image.GetPixelIDTypeAsString())

医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

二维和三维图像的深度是怎么样的?

对于二维图像来说,不论是RGB的彩色图像还是普通图像,其深度均为0

print(image_2D.GetDepth())
print(image_2D.GetSize())
print(image.GetDepth())
print(image.GetSize())
print(image_RGB.GetDepth())

医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

2D RGB图像的相关属性值:

2维的彩色图像的维度为2,同时由于其是Vector类型,所以其每个像素是由三维向量组成的,GetNumberOfComponentsPerPixel()的输出为3。

print(image_RGB.GetDimension())
print(image_RGB.GetSize())
print(image_RGB.GetSpacing())
print(image_RGB.GetNumberOfComponentsPerPixel())

医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

像素的相关操作

我们可以使用GetPixel或SetPixel来获取或修改像素值。

print(image.GetPixel(0, 0, 0))
image.SetPixel(0, 0, 0, 1)
print(image.GetPixel(0, 0, 0))

医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

也可以使用更简单的方式来完成上述操作:

print(image[0, 0, 0])
image[0, 0, 0] = 10
print(image[0, 0, 0])

SimpleITK和Numpy的相互转换

SimpleITK的图像类型,和numpy的ndarray类型可以很方便的相互转换

nda = sitk.GetArrayFromImage(image)
img = sitk.GetImageFromArray(nda)

值得注意的是,在RGB的vector类型转换中可能会遇到一些维度上的问题

nda = sitk.GetArrayFromImage(image_RGB)
img = sitk.GetImageFromArray(nda)
print(image_RGB.GetSize())
print(img.GetSize())

医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

在将array转换回sitk时,三通道会默认变成第三维,转换为3128128

如果想继续保持vector的数据类型,需要加入参数isVector=True

img = sitk.GetImageFromArray(nda, isVector=True)

类型转化中的index顺序

在sitk中,默认的存取顺序为x, y, z.而在numpy中会成为z, y, x。两者完全相反,所以在类型转换中需要格外注意index的顺序变化。

import numpy as np

multi_channel_3Dimage = sitk.Image([2, 4, 8], sitk.sitkVectorFloat32, 5)
x = multi_channel_3Dimage.GetWidth() - 1
y = multi_channel_3Dimage.GetHeight() - 1
z = multi_channel_3Dimage.GetDepth() - 1
multi_channel_3Dimage[x, y, z] = np.random.random(multi_channel_3Dimage.GetNumberOfComponentsPerPixel())

nda = sitk.GetArrayFromImage(multi_channel_3Dimage)

print("Image size" + str(multi_channel_3Dimage.GetSize()))
print("Numpy array size:" + str(nda.shape))

!!Notice the index order and channel access are different
print("First channel value in image:" + str(multi_channel_3Dimage[x, y, z][0]))
print("First channel value in numpy array: " + str(nda[z, y, x, 0]))

医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

同时,仍然需要注意,numpy可以直接使用[z, y, x, 0]获取通道值,但是在sitk中需要[x, y, z][0]两个中括号获取channel的数据。

Original: https://blog.csdn.net/pyd20/article/details/124076890
Author: pyd20
Title: 医学影像处理工具:SimpleITK学习笔记(二)图像基本操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/519967/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球