I have a question concerning Image Processing: I have a stack of images, which I can compose to a 3D image using Image3D. Additionally I have a 4x4 affine transformation matrix. I would like to transform the 3D image using my transformation matrix.
In 2D this would be possible using ImageTransformation[image, transformationMatrix]
t = AffineTransform[{{{RandomReal[{-0.5, 5}],
RandomReal[{-0.5, 5}]}, {RandomReal[{-0.5, 5}],
RandomReal[{-0.5, 5}]}}, {RandomReal[{0.2, 0.2}],
RandomReal[{-0.2, 0.2}]}}]
ImageTransformation[ExampleData[{"TestImage", "Lena"}], t, PlotRange -> All, Padding -> None]
So this works fine for 2D images, but unfortunately ImageTransformation is not applicable to Image3D-objects.
My first attempt was to decompose the matrix into several matrices and transform the image stack in 2 steps:
- transforming each image of the image stack
- transpose the image stack and transform with the transformation matrix for the third dimension.
As an example I do the following steps for translation:
image3D = Import["ExampleData/CTengine.tiff", "Image3D"]
slices = Image3DSlices[image3D];
translation3D = TranslationTransform[{0.2, 0.1, 0.3}]
translation3D1 = TranslationTransform[{0.2, 0.1}]
translation3D2 = TranslationTransform[{0, 0.3}]
tempimageslices = ImageTransformation[#, translation3D1] & /@ slices;
transposedtempimageeslices = Image /@ (Transpose[ImageData[#] & /@ tempimageslices]);
tempimageslices2 = ImageTransformation[#, translation3D2] & /@ transposedtempimageeslices;
final = Image /@ (Transpose[ImageData[#] & /@ tempimageslices2, {2, 1, 3}]);
Image3D[final]
The Steps in this example are:
I cut the image into several slices.
I split the 4 x 4 transformation matrix "translation3D" into 2 3 x 3 matrices "translation3D1" and "translation3D2".
I transform every image in slices using ImageTransformation and the matrix "translation3D1"
I transpose the imageData and create new images in the new dimensions
I transform the images in the new dimensions using the matrix "translation3D2"
I transpose the images in order to have the original ImageDimensions
For a translation or a scaling, the two transformation matrices i need, are easy to read from the matrix. The problem I have with this method is that I can't compute the decomposition of an affine transformation matrix.
Is there an easy way to decompose the affine transformation or another solution in which the full affine transformation can be used to transform the image stack?