Search code examples
matlabimage-processingimage-rotationzero-padding

Rotating image with trigonometric functions and image padding


While rotating image with trigonometric function we are using image padding.

If we rotate without padding we have this picture:

enter image description here

If we pad the picture, For instance with these codes:

`Padding_Bottom_And_Top = zeros(round(Image_Height/2),Image_Width,3);
Side_Padding = zeros(Image_Height+2*size(Padding_Bottom_And_Top,1),Image_Width/2,3);
Side_Padding =zeros(Image_Height+2*size(Padding_Bottom_And_Top,1),Image_Width/2,3);
Padded_Image = [Padding_Bottom_And_Top; Original_Image];
Padded_Image = [Padded_Image; Padding_Bottom_And_Top];
Padded_Image = [Side_Padding Padded_Image];
Padded_Image = [Padded_Image Side_Padding];`

We are having this picture,

enter image description here

First, i want to learn how this code piece is working? Secondly, what we achieve with using image padding?


Solution

  • Image Padding

    This playground script below might help to put zero-padding into perspective. Here the padding used is half the image height for the top and bottom padding and half the image width for the side padding. Essentially padding is concatenating arrays of zeros (black pixels) to the sides of the image. In this case, it allows the image to be rotated without clipping the image. The padded image will have some margin so that the corners do not overflow the bounds of the image upon rotation. Using the padarray() function can achieve the same results which is also a suitable method. For details regarding rotating specifically see: Rotating image with trigonometric functions

    Alternative Method Using the padarray() Function:

    clf;
    Original_Image = imread("peppers.png");
    [Image_Height,Image_Width,Number_Of_Colour_Channels] = size(Original_Image);
    Padded_Image = padarray(Original_Image,[Image_Height/2 Image_Width/2],0,'both');
    imshow(Padded_Image);
    

    Padding Image Example

    Playground Script:

    clf;
    Original_Image = imread("peppers.png");
    [Image_Height,Image_Width,Number_Of_Colour_Channels] = size(Original_Image);
    
    Padding_Bottom_And_Top = zeros(round(Image_Height/2),Image_Width,Number_Of_Colour_Channels);
    Side_Padding = zeros(Image_Height+2*size(Padding_Bottom_And_Top,1),Image_Width/2,Number_Of_Colour_Channels);
    
    subplot(2,4,1); imshow(Padding_Bottom_And_Top);
    title("Top and Bottom Padding");
    
    subplot(2,4,2); imshow(Side_Padding);
    title("Side Padding"); 
    
    Padded_Image = [Padding_Bottom_And_Top; Original_Image];
    subplot(2,4,5); imshow(Padded_Image);
    title("Top Padding");
    
    Padded_Image = [Padded_Image; Padding_Bottom_And_Top];
    subplot(2,4,6); imshow(Padded_Image);
    title("Top and Bottom Padding");
    
    
    Padded_Image = [Side_Padding Padded_Image];
    subplot(2,4,7); imshow(Padded_Image);
    title("Top, Bottom and Left Padding");
    
    
    Padded_Image = [Padded_Image Side_Padding];
    subplot(2,4,8);imshow(Padded_Image);
    title("Top, Bottom, Left and Right Padding");
    

    Ran using MATLAB R2019b