Resampling volume to axial view with SimpleITK

Hi all,

I want to resample medical volume datasets such that they are always oriented axial. I’ve managed to get it to work more or less using the following code (C# but should be clear enough:)

                Image lImage = fVolume.InputITKImage;
                var lInputSize = lImage.GetSize();

                var lMat =lPatientMatrix.Transpose().Inverse().SubMatrix(0, 3, 0, 3);
                AffineTransform lTransformReorient = new AffineTransform(3);
                lTransformReorient.SetMatrix(new VectorDouble(lMat.ToRowMajorArray()));

                var lImageSize = lImage.GetSize();
                var lNewSize = new VectorUInt32((lMat * Vector<double>.Build.DenseOfArray(new double[] { lImageSize[0], lImageSize[1], lImageSize[2] }))
                    .ToArray()
                    .Select(fDouble => (uint)Math.Abs(fDouble)));

                var lSpacing = lImage.GetSpacing();
                var lNewSpacing = new VectorDouble((lMat * Vector<double>.Build.DenseOfArray(new double[] { lSpacing[0], lSpacing[1], lSpacing[2] }))
                    .ToArray()
                    .Select(fDouble => Math.Abs(fDouble)));

                var lOrigin = lImage.GetOrigin();
                lOrigin[1] = -lNewSize[1] * lNewSpacing[1]; // test


                lResampledImage = SimpleITK.Resample(lImage, lNewSize, lTransformReorient, InterpolatorEnum.sitkLinear, lOrigin, lNewSpacing);

The lPatientMatrix contains the orientation of the volume where the column vectors correspond to one direction. E.g. for a coronal view, where the slices go from anterior to posterior, the matrix would be:
1 0 0
0 0 1
0 -1 0

This works more or less.

First question: It seems that ITK uses row vectors as I don’t get a result if I don’t transpose the matrix, is this observation correct?

Second question: When I leave the origin at (0, 0, 0) I see only one slice, that’s why I shift the y component of the origin by the Y Length * Y Spacing. When and how do I shift the origin correctly?

Third question: The volume is resampled correctly, but the Y and Z axis are flipped, ie. positive Y values point to the anterior, and positive Z values point to the feet.

Any advice would be much appreciated!

1 Like

Have you tried DICOMOrientImageFilter?

2 Likes

Thanks, that’s exactly what I want.

I also managed to get my initial code correct. One of the major problems was that the transformation was around the origin (0, 0, 0), so the origin after transformation is of course different.

1 Like