Rotate images with automatic cropping

version 1.0 (4.2 KB) by David Young
The blank triangles that appear in the corners of rotated images are cropped away.


Updated 2 Dec 2014

View License

When an image is rotated, using imrotate() for example, blank triangles appear in the corners of the output unless it is reduced in size. The triangles are the parts of the output image that correspond to regions outside the boundary of the input image. This function rotates an image about its centre using imrotate(), then crops the result sufficiently to remove the triangles. Of course, some data in the corners of the original image is lost.
By default the output has the maximum possible area (i,e,. number of pixels) and the crop is symmetrical, so the centre of the output is at the centre of the input image. In this case the function takes the same arguments as imrotate().
Optionally, the aspect ratio of the output may be set to a given value, or to be the same as that of the input. It may also be set so that the corners of the output all lie on the rotated sides of the input, though this is not always possible. In general, if the aspect ratio is set the cropped image will not have the maximum possible area.
When the position of the output is not fully constrained it may optionally be adjusted by a single parameter which sets the position of the centre of the output on the long axis of the rotated input.
Note that if you are using a version of MATLAB older than R2013b you will need to replace any calls to the addParameter method of the input parser with addParamValue.

Cite As

David Young (2022). Rotate images with automatic cropping (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2014b
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!