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);
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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…