logo

How to Rotate Image Arrays in Julia 📂Julia

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)

1.PNG

2.PNG

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))

3.PNG

4.PNG

Additionally, using Constant() from the Interpolations package allows you to apply the nearest1 interpolation method. Since it only uses the nearest point for calculation, the accuracy is lower but the computation is faster. Conversely, bilinear2 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())

5.PNG

6.PNG

7.PNG

See Also

Environment

  • OS: Windows10
  • Version: 1.5.3 (2020-11-09)

  1. Nearest-neighbor interpolation ↩︎

  2. Bilinear interpolation ↩︎