Image rotate - want white background instead of default black
Show older comments
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
More Answers (2)
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
Mohamed Ibrahim
on 9 May 2019
thanks alot Mustafa
Zsolt Kocsis
on 27 Jul 2020
Works fine, thank you!
Jesús Díaz
on 24 Sep 2020
It works perfectly, thanks a lot.
Celil Semih Sevincik
on 10 Jun 2021
thanks a lot
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
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
on 12 Nov 2023
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.
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!