How to preserve edges when rotating images

7 views (last 30 days)
How can i make sure that my images dont cut off when using rotation in image augmentation
imageAugmenter = imageDataAugmenter('RandRotation',[0,360]);
In = imread('input_image.bmp');
Out = augment(imageAugmenter,In);
What additional parameters, do i need to set, to ensure the image dont get cut off at the edges

Accepted Answer

Walter Roberson
Walter Roberson on 7 Jan 2021
pad the image with nan so that it becomes sqrt(2) times its original size. Do the random rotation on that. Find the bounding box of non-nan elements and crop. Fill the remaining nan with something appropriate.
Or... don't use the image augmenter, and use imrotate with a random angle, specifying an appropriate padding strategy.
  2 Comments
Wan Faiz
Wan Faiz on 11 Apr 2022
I cant find any examples on this. Can you explain how to implement it on the codes?
Walter Roberson
Walter Roberson on 12 Apr 2022
sqrt(2) does not seem to quite do the trick, but slightly larger seems to work.
The display with alpha masking shows visually that you could then proceed to trim the leading and trailing all-nan rows and columns.
imageAugmenter = imageDataAugmenter('RandRotation',[0,360], 'FillValue', nan);
In = imread('flamingos.jpg');
Indouble = im2double(In);
nrow = size(Indouble, 1);
ncol = size(Indouble, 2);
factor = .4; %(sqrt(2) - 1)/2;
Indouble = padarray(Indouble, ceil([nrow ncol 0]*factor), nan);
Outdouble = augment(imageAugmenter, Indouble);
Out8 = im2uint8(Outdouble);
mask = isnan(Outdouble);
Out8(mask) = 0;
alphadata = double(~any(mask,3));
figure
h1 = imshow(Out8);
title('no alpha');
figure
h2 = imshow(Out8);
h2.AlphaData = alphadata;
title('alpha mask')
nnmask = all(~mask,3);
hmask = any(nnmask,1);
firstcol = find(hmask, 1);
lastcol = find(hmask, 1, 'last');
vmask = any(nnmask,2);
firstrow = find(vmask, 1);
lastrow = find(vmask, 1, 'last');
trimmed = Outdouble(firstrow:lastrow, firstcol:lastcol, :);
trimmed8 = im2uint8(trimmed);
mask8 = any(isnan(trimmed),3);;
trimmed8( repmat(mask8, 1, 1, 3)) = 0;
alphadata = double(~mask8);
figure
h3 = imshow(trimmed8);
h3.AlphaData = alphadata;
title('alpha mask after trim')

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!