Image rotate - want white background instead of default black

19 visualizaciones (últimos 30 días)
Andy
Andy el 6 de Mayo de 2013
Comentada: Naveed Salman el 12 de Nov. de 2023
Dear Matlabneers,
I am using the following code to make the background of a rotated image "white", instead of the "black" background that imrotate applies by default. However, it is giving me a dotted line around the rotated edge of the image. Does anyone have any ideas how to fix this?
gry = imrotate(img,90+x,'bilinear');
mgry = ~imrotate(true(size(img)),90+x,'bilinear');
gry(mgry&~imclearborder(mgry)) = 255;
Thanks for any help in advance.
Regards, Andy

Respuesta aceptada

Image Analyst
Image Analyst el 6 de Mayo de 2013
Here's what I'd try (untested):
Edit imrotate.m
>> edit imrotate.m
Find this line:
A = padarray(A,[2 2],0,'both');
Change the 0 to 255 or whatever fill value you want. Then save it as a new file, imrotate255.m, somewhere in your search path. Then call imrotate255() instead of the built-in imrotate().
  7 comentarios
Patrick Tuohy
Patrick Tuohy el 8 de Mzo. de 2019
This no longer seems to work, I can't find "A = padarray(A,[2 2],0,'both');"
Anyone know how else to solve this?
Image Analyst
Image Analyst el 10 de Jun. de 2021
@Patrick Tuohy, in R2021a, it's at line 146.
A = padarray(A,[2 2],0);
Simply search for "padarray" (without searching for the whole line) and you should find it.

Iniciar sesión para comentar.

Más respuestas (2)

Mustafa Umit Arabul
Mustafa Umit Arabul el 31 de En. de 2018
I have done the same thing using imwarp.m function. And mimicked the 'crop' option of imrotate.m using spatial reference.
function rotated_image = imrotate_white(image, rot_angle_degree)
RA = imref2d(size(image));
tform = affine2d([cosd(rot_angle_degree) -sind(rot_angle_degree) 0; ...
sind(rot_angle_degree) cosd(rot_angle_degree) 0; ...
0 0 1]);
Rout = images.spatialref.internal.applyGeometricTransformToSpatialRef(RA,tform);
Rout.ImageSize = RA.ImageSize;
xTrans = mean(Rout.XWorldLimits) - mean(RA.XWorldLimits);
yTrans = mean(Rout.YWorldLimits) - mean(RA.YWorldLimits);
Rout.XWorldLimits = RA.XWorldLimits+xTrans;
Rout.YWorldLimits = RA.YWorldLimits+yTrans;
rotated_image = imwarp(image, tform, 'OutputView', Rout, 'interp', 'cubic', 'fillvalues', 255);
end
If you do not want to crop:
function rotated_image = imrotate_white(image, rot_angle_degree)
tform = affine2d([cosd(rot_angle_degree) -sind(rot_angle_degree) 0; ...
sind(rot_angle_degree) cosd(rot_angle_degree) 0; ...
0 0 1]);
rotated_image = imwarp(image, tform, 'interp', 'cubic', 'fillvalues', 255);
end
  4 comentarios
Jesús Díaz
Jesús Díaz el 24 de Sept. de 2020
It works perfectly, thanks a lot.

Iniciar sesión para comentar.


Naveed Salman
Naveed Salman el 11 de Nov. de 2023
Editada: Naveed Salman el 12 de Nov. de 2023
This is what I tried and it works well for me.
It doesn't require any m-file code. It can be written in Command line.
For white background this works
angle = 30; % Specify angle
A = imread('My_Image.jpg'); % Specify your Image file
figure(1)
imshow(A)
B = imrotate(uint8(255*ones(size(A))),angle);
C = uint8(255*ones(size(B))) - B + imrotate(A,angle);
figure(2)
imshow(C)
If you want background of some other color you can modify it to this.
Background_color = [100 150 120]; % Specify background
angle = 30; % Specify angle
A = imread('My_Image.jpg'); % Specify you Image file
figure(1)
imshow(A)
B = imrotate(uint8(ones(size(A))),angle);
C = uint8(ones(size(B))) - B;
C(:,:,1) = C(:,:,1)*Background_color(1);
C(:,:,2) = C(:,:,2)*Background_color(2);
C(:,:,3) = C(:,:,3)*Background_color(3);
C = C + imrotate(A,angle);
figure(2)
imshow(C)
  2 comentarios
Image Analyst
Image Analyst el 11 de Nov. de 2023
Why do you define
Background_color = [100 150 120]; % Specify background
yet use only 2 of the 3 elements of it?
C(:,:,2) = C(:,:,2)*Background_color(2);
C(:,:,3) = C(:,:,3)*Background_color(2);
You assign the second value (green, I guess) to both the green channel and the blue channel.
Naveed Salman
Naveed Salman el 12 de Nov. de 2023
Thank @Image Analyst for pointing this mistake out.
The correct script is like this
C(:,:,1) = C(:,:,1)*Background_color(1);
C(:,:,2) = C(:,:,2)*Background_color(2);
C(:,:,3) = C(:,:,3)*Background_color(3);
I have editing my original mistake.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by