Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

matlab - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...