# Rotating image with trigonometric functions

## Rotating Images Using Trigonometric Functions

Rotating an image can be done using trigonometric functions by decomposing the rotation matrix. The rotation matrix can be described as matrices and the corresponding code snippet below:

The code will iterate to populate the rotated image by evaluating the corresponding point in the original image. Some things to note is that padding the image with zeroes is required to allow for the picture not to be clipped after rotation. For a more detailed derivation of the rotation matrix please check out this post: How do rotation matrices work?

Code Snippet:

``````X_Displacement = Row - X_Midpoint;
Y_Displacement = Column - Y_Midpoint;

X_Prime = X_Displacement*cosd(Angle) + Y_Displacement*sind(Angle);
Y_Prime = -X_Displacement*sind(Angle) + Y_Displacement*cosd(Angle);
``````

In the above code snippet `cosd()` and `sind()` are used so that angles can be accepted in terms of degrees. If you’d like to alternatively use radians use the standard `cos()` and `sin()` functions. Here nearest-neighbour interpolation is applied when `round()` is used. This is due to the nature of rotating an image. A small case is rotating a 3 by 3 image. This brings an issue to light where you essentially can only pivot the pixels surrounding the centre pixel in 9 different ways which not representative of all the possible angles.

## For Greyscale Images:

``````clear;
Angle = 30;

subplot(1,2,1); imshow(Original_Image);
title("Original Image");

[Image_Height,Image_Width] = size(Original_Image);

Rotated_Image = zeros(Image_Height,Image_Width);

%Finding the centre points%

X_Displacement = Row - X_Midpoint;
Y_Displacement = Column - Y_Midpoint;

X_Prime = X_Displacement*cosd(Angle) + Y_Displacement*sind(Angle);
Y_Prime = -X_Displacement*sind(Angle) + Y_Displacement*cosd(Angle);

X_Prime = round(X_Prime + X_Midpoint);
Y_Prime = round(Y_Prime + Y_Midpoint);

if(X_Prime >= 1 && Y_Prime >= 1 && X_Prime <= Padded_Image_Height && Y_Prime <= Padded_Image_Width)
end

end
end

Rotated_Image = uint8(Rotated_Image);
subplot(1,2,2); imshow(Rotated_Image);
title("Rotated Image");

%Saving rotated image with transparency%
Transparent_Region = (Rotated_Image ~= 0);
Transparent_Region = uint8(255.*Transparent_Region);
imwrite(Rotated_Image,'Rotated.png','Alpha',Transparent_Region);
``````

## For Coloured Images:

``````clear;
Angle = 30;

subplot(1,2,1); imshow(Original_Image);
title("Original Image");

[Image_Height,Image_Width,~] = size(Original_Image);

Rotated_Image = zeros(Image_Height,Image_Width,3);

%Finding the centre points%

X_Displacement = Row - X_Midpoint;
Y_Displacement = Column - Y_Midpoint;

X_Prime = X_Displacement*cosd(Angle) + Y_Displacement*sind(Angle);
Y_Prime = -X_Displacement*sind(Angle) + Y_Displacement*cosd(Angle);

X_Prime = round(X_Prime + X_Midpoint);
Y_Prime = round(Y_Prime + Y_Midpoint);

if(X_Prime >= 1 && Y_Prime >= 1 && X_Prime <= Padded_Image_Height && Y_Prime <= Padded_Image_Width)
end

end
end

Rotated_Image = uint8(Rotated_Image);
subplot(1,2,2); imshow(Rotated_Image);
title("Rotated Image");

%Saving rotated image with transparency%
Transparent_Region = (Rotated_Image(:,:,1) ~= 0);
Transparent_Region = uint8(255.*Transparent_Region);
imwrite(Rotated_Image,'Rotated.png','Alpha',Transparent_Region);
``````

Ran using MATLAB R2019b