MATLAB: Is it possible to get the boundary size of an object in an image same after rotation of the image by a particular angle

Image Processing Toolboxrotate an imagewithout change in size

I have a sample image which contains a square. I rotated the image through a range of angle 30 to 50?
When I take the boundary size of the object in the rotated image (for these angle) I am getting values lesser than the boundary size of the object in the original sample image.
I am attaching a sample code for the same:
======================================================
Irot_new =imread('N:\work\tex\paper\square.bmp');
Irot_new = rgb2gray(Irot_new);
Irot_new = imbinarize(Irot_new);
B = bwboundaries(Irot_new);
boundary = B{1};
x = boundary(:,2);
y = boundary(:,1);
boundary_size = size(x)
Irot_new = imrotate(Irot_new,-40,'nearest','crop');
Irot_new = imbinarize(Irot_new);
figure
imshow(Irot_new);
Irot_new = bwboundaries(Irot_new);
boundary1 = Irot_new{1};
x = boundary1(:,2);
y = boundary1(:,1);
boundary_size = size(x)
%======================================================

%I tried even without using the imrotate with the following code , but still the boundary size differ:
%======================================================
I= imread('N:\work\tex\paper\square.bmp');
I_rgb = rgb2gray(I);
image= imbinarize(I_rgb);
%image padding
[Rows, Cols] = size(image);
Diagonal = sqrt(Rows^2 + Cols^2);
RowPad = ceil(Diagonal - Rows) + 2;
ColPad = ceil(Diagonal - Cols) + 2;
imagepad = zeros(Rows+RowPad, Cols+ColPad);
imagepad(ceil(RowPad/2):(ceil(RowPad/2)+Rows-1),ceil(ColPad/2):(ceil(ColPad/2)+Cols-1)) = image;
t=30
for i=1:1:4
rads=degtorad(t);
%midpoints
midx=ceil((size(imagepad,1)+1)/2);
midy=ceil((size(imagepad,2)+1)/2);
imagerot=zeros(size(imagepad));
for i=1:size(imagerot,1)
for j=1:size(imagerot,2)
x= (i-midx)*cos(rads)+(j-midy)*sin(rads);
y=-(i-midx)*sin(rads)+(j-midy)*cos(rads);
x=round(x)+midx;
y=round(y)+midy;
if (x>=1 && y>=1 && x<=size(imagepad,2) && y<=size(imagepad,1))
imagerot(i,j)=imagepad(x,y); % k degrees rotated image
end
end
end
B = bwboundaries(imagerot);
boundary = B{1};
x = boundary(:,2);
y = boundary(:,1);
boundary_size = size(x)
figure,imagesc(imagerot);
colormap(gray(256));
t = t+10;
end
=========================================================================
Can you please suggest why the boundary size varies while its determination? Any solution to keep the boundary size constant.

Best Answer

Img =imread('square.bmp');
Imgg = rgb2gray(Img);
angles = 0:-10:-90;
nang = length(angles);
fignums = [1, 1, 1, 1, 2, 3, 1, 1, 1, 1];
bs = zeros(1, nang);
for angidx = 1 : length(angles)
ang = angles(angidx);
Irot_new = imrotate(Imgg,ang,'nearest','crop');
Irot_bin = imbinarize(Irot_new);
figure(fignums(angidx))
imshow(Irot_bin);
axis equal
title(sprintf('rotated %d', ang))
drawnow;
Irot_new = bwboundaries(Irot_bin);
boundary1 = Irot_new{1};
x = boundary1(:,2);
y = boundary1(:,1);
boundary_size = size(x);
bs(angidx) = boundary_size(1);
fprintf('angle %d boundary length %d\n', ang, bs(angidx));
end
disp([angles; bs])
angle 0 boundary length 703
angle -10 boundary length 694
angle -20 boundary length 662
angle -30 boundary length 610
angle -40 boundary length 539
angle -50 boundary length 540
angle -60 boundary length 610
angle -70 boundary length 661
angle -80 boundary length 693
angle -90 boundary length 703
0 -10 -20 -30 -40 -50 -60 -70 -80 -90
703 694 662 610 539 540 610 661 693 703
So 40 and 50 are the same to within round-off error. They differ because your image is not exactly centered.
Your changes are not as regular as you were describing. If you look at the changes, you will see that the first difference is roughly 10, the second is roughly 30, the third is roughly 50, the fourth is roughly 70. Thus there is a distinct pattern, and the 40 and 50 are not unusual.
As to why the change gets faster as you increase the angle: that is just how trig works.
You could probably express the coordinates by formula.
Related Question