I have a question about slices ordering:
I have about 80 pictures of hip joint but there are not arranged from feet to head or head to feet.
Is there any way to arrange them in an intended order?
JavaScript
x
37
37
1
# pixel aspects, assuming all slices are the same
2
ps = slices[0].PixelSpacing
3
ss = slices[0].SliceThickness
4
5
6
ax_aspect = ps[1]/ps[0]
7
sag_aspect = ps[1]/ss
8
cor_aspect = ss/ps[0]
9
10
# create 3D array
11
img_shape = list(slices[0].pixel_array.shape)
12
img_shape.append(len(ct_images))
13
print(img_shape)
14
15
img3d = np.zeros(img_shape)
16
17
# fill 3D array with the images from the files
18
for i, s in enumerate(slices):
19
img2d = s.pixel_array
20
img3d[:, :, i] = img2d
21
22
# plot 3 orthogonal slices
23
a1 = plt.subplot(2,2,1)
24
plt.imshow(img3d[:,:,img_shape[2]//2])
25
a1.set_aspect(ax_aspect)
26
27
a2 = plt.subplot(2,2,3)
28
plt.imshow(img3d[:,img_shape[1]//2,:])
29
a2.set_aspect(sag_aspect)
30
31
a3 = plt.subplot(2,2,2)
32
plt.imshow(img3d[img_shape[0]//2,:,:].T)
33
a3.set_aspect(cor_aspect)
34
35
36
plt.show()
37
SOP Class UID is CT Image Storage.
This is the code and These are the pictures of results.
Advertisement
Answer
slices = sorted(slices, key=lambda x: x.ImagePositionPatient[2])
is probably what you want, depending on what your SOP Class UID is.