Image rotate - want white background instead of default black

12 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2013-5-6
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 个评论
Patrick Tuohy
Patrick Tuohy 2019-3-8
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 2021-6-10
@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.

请先登录,再进行评论。

更多回答(2 个)

Mustafa Umit Arabul
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 个评论

请先登录,再进行评论。


Naveed Salman
Naveed Salman 2023-11-11
编辑:Naveed Salman 2023-11-12
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 个评论
Image Analyst
Image Analyst 2023-11-11
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 2023-11-12
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by