How to Rotate Image Arrays in Julia
Image Rotation
imrotate(X, theta)
: Rotates array X by theta radians. Note that, unlike in MATLAB where the angle unit is degrees ($^{\circ})$, the angle unit here is radians. Additionally, unlike MATLAB, it rotates clockwise. If no other variables are inputted, the interpolation method defaults to bilinear, and the rotated image is not cropped. Examples of rotating the original image X by $90^\circ=\pi/2$, $180^\circ=\pi$, and $270^\circ=\frac{3}{2}\pi$, along with their results, are shown below.
using Images
using Interpolations
X=load("example\_{i}mage.png")
Y1=imrotate(X,pi/2)
Y2=imrotate(X,pi)
Y3=imrotate(X,pi*3/2)
As shown, when rotated like this, the original array fits perfectly, so there is no change in the size of the image. However, when rotated by an angle that is not a multiple of $90$, it does not align with the original shape. Therefore, the image expands to represent all points of the original image. If one wishes to maintain the original size of the image, one can add the axes()
variable.
Y4=imrotate(X,pi/6)
Y5=imrotate(X,pi/6,axes(X))
Additionally, using Constant()
from the Interpolations
package allows you to apply the nearest
1 interpolation method. Since it only uses the nearest point for calculation, the accuracy is lower but the computation is faster. Conversely, bilinear
2 involves the calculation of all four surrounding points, so it is relatively slower but more accurate. Being more accurate here means that there is less damage to the image upon rotation. Look at the pictures below. The images are large, so at first glance, there might not seem to be a difference, but upon zooming in, the differences between the two interpolation methods become clear.
Y6=imrotate(X,pi/6,Constant())
See Also
Environment
- OS: Windows10
- Version: 1.5.3 (2020-11-09)