Image rotate - want white background instead of default black

46 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 6 May 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 Comments
Patrick Tuohy
Patrick Tuohy on 8 Mar 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 on 10 Jun 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.

Sign in to comment.

More Answers (2)

Mustafa Umit Arabul
Mustafa Umit Arabul on 31 Jan 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 Comments

Sign in to comment.


Naveed Salman
Naveed Salman on 11 Nov 2023
Edited: Naveed Salman on 12 Nov 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 Comments
Image Analyst
Image Analyst on 11 Nov 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 on 12 Nov 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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!