How do I rotate a 3d image without using imrotate?

I have a CT image which I want to rotate, how can I do this without using imrotate? After rotating, I want to use that image to perform a MIP. I also want to know how the matrix changes.

 Accepted Answer

If you want to rotate it for display purposes, then parent the graphics object against a hgtransform group, and to do the rotation, change the rotation matrix associated with the hgtransform group.
If you want to rotate it computationally, then create a rotation matrix and multiply the rotation matrix by the coordinates of the image location in order to get the new coordinates.
But if you are creating a new array from that, then you need to be careful about whether you "push" values or "pull" values.
"Push" in this sense is to start with the original coordinates and apply the rotation matrix, and to assign the pixel values into the calculated locations. It is known that if you do that, then you can end up with gaps in the output image.
"Pull" on the other hand starts with output coordinates and from each and (effectively) the inverse of the rotation matrix, calculates the source coordinates and extracts values from there. If you do this, you will not end up with gaps in the output image.
Any given output location is likely to involve values from multiple input locations.
If you are working on the topic of Projection Pursuit, then you should consider using one of the established packages, probably written in another programming language. I used to use a nice package for multidimensional projection pursuit, but that was 20-ish years ago and I know longer recall the program name :( Ah, I see even some MATLAB code, https://github.com/erikapat/Projection-Pursuit

3 Comments

Thank you for your answer. I want to rotate it computationally. I tried the rotation matrix, but my matrix size is 512x512x384. How can i generate a rotation matrix that can be multiplied with this, taking into account the laws of matrix multiplication?
You do not apply the rotation matrix to the contents of the array: you apply the rotation matrix to the coordinates.
For example,
YourMatrix = sort( randi([0 255], 512, 512, 384, 'uint8') ); %sample data
[Y, X, Z] = ndgrid(1:size(YourMatrix,1), 1:size(YourMatrix,2), 1:size(YourMatrix,3));
XYZ0 = [X(:), Y(:), Z(:), zeros(numel(X),1)];
M = makehgtform('yrotate', 7 * pi/180); %7 degrees
newXYZ0 = XYZ0 * M;
nX = reshape(newXYZ0(:,1), size(X));
nY = reshape(newXYZ0(:,2), size(Y));
nZ = reshape(newXYZ0(:,3), size(Z));
Now the revised coordinates for the X, Y, Z points are the corresponding nX, nY, nZ values.
You know exactly where each original pixel will be located after the rotation. The image has been rotated in one very real and important sense. For example you could now
surf(nX(:,:,1), nY(:,:,1), nZ(:,:,1), YourImage(:,:,1) 'edgecolor', 'none')
to display the first plane.
... and now what?
Wasn't too clear with concepts before, thanks for helping me!

Sign in to comment.

More Answers (1)

You can use imwarp... But what advantage over imrotate are you looking for?

5 Comments

Thank you for your answer. I want to use imrotate to use the least number of projections to perform the MIP.
MIP is always relative to a particular axis, and requires only a single rotation once the axis direction is known.
I agree with Walter. I don't see why imrotate wouldn't work, unless the axis of the MIP is not parallel to the image slices.
imrotate() is always relative to the center of the images as if it were 2D. imwrap() can take an arbitrary transform matrix.
You're right. Imrotate is perfect. Made a rookie mistake. Thanks for your help!

Sign in to comment.

Asked:

on 17 Jul 2018

Commented:

on 18 Jul 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!