1.HWC转换成CHW,
SimpleITK 和 Nibabel 的区别:
SimpleITK 加载数据是channel_first,即(155,240,240);
Nibabel 是 channel_last,即(240,240,155),其中155是图像通道数,也就是155张图像,可以把nii看成二维图像,也可以看成三维。看成二维就是[HWC]
但是 nibabel加载出来的图像被旋转了90度,横过来了
但是pytorch中计算的话是[CHW]
转换方式有两种一是
a=[
[[0.,0.,0.],
[0.,0.,0.],
[0.,0.,0.],
[0.,0.,0.],
[0.,0.,0.]],
[[1.,1.,1.],
[1.,1.,1.],
[1.,1.,1.],
[1.,1.,1.],
[1.,1.,1.]],
[[2.,2.,2.],
[2.,2.,2.],
[2.,2.,2.],
[2.,2.,2.],
[2.,2.,2.]],
[[3.,3.,3.],
[3.,3.,3.],
[3.,3.,3.],
[3.,3.,3.],
[3.,3.,3.]],
[[4.,4.,4.],
[4.,4.,4.],
[4.,4.,4.],
[4.,4.,4.],
[4.,4.,4.]]
]
a=np.array(a)
print(a.shape) #(H,W,C)
print(a.shape[-1])
c=[]
for i in range(a.shape[-1]):
b=a[:,:,i]
print(b)
print(b.shape)
c.append(b)
print(c)
d=np.array(c)
print(d.shape)
方法2:一行代码搞定得到的结果和上面的方法一样
print(a.shape)
e=a.transpose(2, 0, 1) #hcw 转换成chw
print(e)
print(e.shape)