File Exchange

image thumbnail


version (312 KB) by Dave
Fast 3D image rotations


Updated 05 Aug 2017

View Version History

View License

This function performs fast rotation of three-dimensional matrices.
When working with transformation of large three-dimensional data, speed can be critical. The imwarp function, and (released in R2017a) the imrotate3 function (which uses imwarp) in the image processing toolbox perform these rotations orders of magnitude slower than the imrotate function does in two dimensions. The cost is not simply due to the increased complexity of a three-dimensional rotation, but rather is because the imrotate function uses some super-speedy mex code.
Here, I've defined a three dimensional rotation problem in terms of an ordered series of two dimensional rotations, and used the permute function to apply imrotate to the appropriate dimensions. Even with the cost of rearranging the dimensions, this approach produced much quicker results.
imrotate3_fast provides a few other potential speed advantages. 1. Because rotations are computed in series, rotations applied to only one or two of the axes are even faster. 2. Because imwarp seems to work natively with floating points, it becomes much slower with integers, conversely imrotate works faster with integers. So for 3D rotations of volumetric integer data (e.g. microscopy data), the speed differences are enormous, and allow for reduced memory demands.
Note: imrotate3 performs a single rotation around an arbitrary axis (axis-angle), whereas imrotate3_fast takes a series of rotations around the x,y,z axes (Euler angles). It made sense to me to keep the inputs in this format, but I've included convenience functions for converting between Euler and Axis-Angle.
Speed tests were performed on a 64-bit PC with an i7-4790 and 32gb of RAM, running Windows. I included data (separately) collected under conditions where ample free physical memory was available, and where it fell short. The code for these tests is also included.

Cite As

Dave (2020). imrotate3_fast (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)


Lei - as far as I can make out, it's always a judgment call which way is clockwise and which way is counterclockwise!

Lei Xu

I am just curious why you specifically changed the sign for the angle related to x-axis rotation in function "dopermrotate".

Lei Xu

Natalie Larson

Very helpful if you have a large dataset in uint8.
I kept running out of memory using imwarp because it converts the data to double, and performs all of the translations and rotations at once (presumably storing intermediate stages).
With this script, you can perform translations step-by-step (ie. first do x-rotation, then y-rotation, etc.), and clear the 3D matrix that is no longer being used to ensure you don't run out of memory.

MATLAB Release Compatibility
Created with R2017a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!