简体   繁体   中英

How do I apply an affine transform to a 3d numpy array?

I got the DICOM images of the [180,512,512] shape with the following code. The image consists of 180 slices and is 512x512 in size.

dcm_list = glob.glob(os.path.join(PathDicom, "*.dcm"))
slices = [pydicom.read_file(dcm) for dcm in dcm_list]
slices.sort(key = lambda x: float(x.InstanceNumber))
if ('RescaleIntercept' in slices[0] and 'RescaleSlope' in slices[0]):
    print("TRUE")
    slope = slices[0].RescaleSlope
    intercept = slices[0].RescaleIntercept
    image = np.stack([s.pixel_array*slope+intercept for s in slices], axis=0)
else:
    image = np.stack([s.pixel_array for s in slices], axis=0)

However, I want to tilt these images into a 4x4 affine matrix like the one below, but I have no idea what to do.

4x4 affine matrix : 

[[ 2.1219860e-01  9.1372589e-03 -1.4462248e-02 -1.1527188e+02]
 [-9.1041764e-03  2.1220960e-01  1.0911685e-02 -9.0879768e+01]
 [ 3.1687310e-03 -2.1837775e-03  9.9983585e-01 -7.8977943e+01]
 [ 0.0000000e+00  0.0000000e+00  0.0000000e+00  1.0000000e+00]]

For 3D functionality in dicom, and especially if you want to do rotations etc, perhaps have a look at simpleITK instead of pydicom.

It natively (and very quickly) handles the full 3D aspect of 3D dicom images, and will do things like you're looking for here very simply and easily.

Pydicom is great for many things, but if you're looking for 3D manipulations you might be better served moving to SITK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM