How to preserve edges when rotating images

조회 수: 7 (최근 30일)
Elysi Cochin
Elysi Cochin . 2021년 1월 7일
댓글: Walter Roberson . 2022년 4월 12일
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

채택된 답변

Walter Roberson
Walter Roberson 2021년 1월 7일
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
Walter Roberson
Walter Roberson 2022년 4월 12일
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));
h1 = imshow(Out8);
title('no alpha');
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);
h3 = imshow(trimmed8);
h3.AlphaData = alphadata;
title('alpha mask after trim')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)


Help CenterFile Exchange에서 Create Block Masks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by