image processing - Rotate a Bounding Box in Matlab -
i have plotted bounding box on image:
bbox = [50 20 200 50]; figure; imshow('coins.png'); hold on; rectangle('position', bbox, 'edgecolor','r', 'linewidth', 3); hold off;
how can rotate bounding box bbox
30 degrees around centroid , obtain coordinates of new box, can use inpolygon
?
update: please use bounding box defined [x y width height].
to rotate coordinates of bounding box need define proper rotation matrix.
start defining coordinates of 4 corners:
x = [bbox(1), bbox(1), bbox(1)+bbox(3), bbox(1)+bbox(3), bbox(1)]; y = [bbox(2), bbox(2)+bbox(4), bbox(2)+bbox(4), bbox(2), bbox(2)];
rotation rotates around origin (0,0)
, if want rotate around center of box need adjust x
, y
before , after rotation
cx = bbox(1)+0.5*bbox(3); cy = bbox(2)+0.5*bbox(4);
rotating
xr = x-xc; %// subtract center yr = y-cy; xr = cosd(30)*xr-sind(30)*yr; %// rotate yr = sind(30)*xr+cosd(30)*yr; xr = xr+xc; %// add center yr = yr+yc;
now can plot rotated box
plot( xr, yr );
you can use xr
, yr
xv
, yv
arguments inpolygon
.
all these algebraic manipulations can done more elegantly using homogeneous coordinates, allows translation (subtracting/adding center of rect) expressed matrix multiplication.
h = [x;y;ones(1,5)]; %// points 3d homogeneous coordinates tc = [1 0 -cx; 0 1 -cy; 0 0 1]; %// translation matrix tr = [cosd(30) -sind(30) 0; sind(30) cosd(30) 0; 0 0 1]; %// rotation hr = inv(tc) * tr * tc * h; %// transformations matrix products plot( hr(1,:), hr(2,:) ); %// rotated rect
Comments
Post a Comment