Skip to content
Advertisement

DICOM slicing orders in python

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?

# pixel aspects, assuming all slices are the same
ps = slices[0].PixelSpacing
ss = slices[0].SliceThickness


ax_aspect = ps[1]/ps[0]
sag_aspect = ps[1]/ss
cor_aspect = ss/ps[0]

# create 3D array
img_shape = list(slices[0].pixel_array.shape)
img_shape.append(len(ct_images))
print(img_shape)

img3d = np.zeros(img_shape)

# fill 3D array with the images from the files
for i, s in enumerate(slices):
    img2d = s.pixel_array
    img3d[:, :, i] = img2d

# plot 3 orthogonal slices
a1 = plt.subplot(2,2,1)
plt.imshow(img3d[:,:,img_shape[2]//2])
a1.set_aspect(ax_aspect)

a2 = plt.subplot(2,2,3)
plt.imshow(img3d[:,img_shape[1]//2,:])
a2.set_aspect(sag_aspect)

a3 = plt.subplot(2,2,2)
plt.imshow(img3d[img_shape[0]//2,:,:].T)
a3.set_aspect(cor_aspect)


plt.show()

SOP Class UID is CT Image Storage.

This is the code and These are the pictures of results.

All pictures of transverse view

3D view

Advertisement

Answer

slices = sorted(slices, key=lambda x: x.ImagePositionPatient[2]) is probably what you want, depending on what your SOP Class UID is.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement