Image rotate - want white background instead of default black

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

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

Hi,
Thanks for your suggestion, I have done what you said and have made a new imrotate255 function and copied the relevant mex file with it. However, the background is still black. I also changed the following to 255 too:
B = tformarray(A, T, R, [1 2], [1 2], outputSize, [], 255);
Try putting it in the "C:\Program Files\MATLAB\R2013a\toolbox\images\images\" folder - same place as where imrotate lives. And did you also change the first line to
function varargout = imrotate255(varargin)
Those are the only things I can think of.
What if you didn't make any changes whatsoever (leave the fill value as 0)? All you did was to change the name of the m-file and the first line. Does it work in that case?
I have done all those things, now it is saying it cannot find that file:
Undefined function 'imrotate255' for input arguments of type 'uint8'.
Error in main (line 34)
gry = imrotate255(img, 90+x,'bilinear');
Isnt that toolbox directory automatically searched as a default path?
Say
which -all imrotate255
and see what it says. The IP Toolbox should be on the path. Type path at the command line to verify.
>> which -all imrotate255
'imrotate255' not found.
I have checked the path and it is there. I just doesnt detect it. Maybe it is not a good idea to place it in the reserved folder.
Anyway I was able to run the function when it is put in the same folder as the main function, however, the problem is that the background is still black after changing 0 to 255 and also I tried changing zeros() to 255*ones() and 255.*ones()
Edit: Finally works now after increasing the padding also from [2 2] to [20 20]. Thanks!
This no longer seems to work, I can't find "A = padarray(A,[2 2],0,'both');"
Anyone know how else to solve this?
@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)

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
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

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.
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.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!