However, the edges of these bounding boxes are at an angle with the x & y axis. For example,
I want to crop out the hands using the bounding box coordinates & then rotate the hands such that they are aligned with the x or y axis.
EDIT:
The hand is represented as follows:
However, please keep in mind that the rectangle is NOT straight. So, I'll have to rotate it to straighten it out.
How to Rotate the Yolo_mark Format Bounding Box? To rotate a point (x,y) by θ, we need to multiply it by the rotation matrix. A point (x,y) will be rotated counterclockwise by angle θ. when multiplied by the rotation matrix.
Some images in . jpg format contain orientation information in Exif metadata. If available, the Exif metadata for the image contains the orientation. In the Exif metadata, you can find the image's orientation in the orientation field.
Good one!
Compute the size of the rectangle
width = sqrt( sum( (b-a).^2 ) );
height = sqrt( sum( (c-b).^2 ) );
Compute an affine transformation from a
...d
to an upright image
Xin = [a(2) b(2) c(2) d(2)];
Yin = [a(1) b(1) c(1) d(1)];
Xout = [width 1 1 width];
Yout = [1 1 height height];
A = [Xin;Yin;ones(1,4)]';
B = [Xout; Yout]';
H = B \ A; % affine transformation
Note that despite the fact that we allow fo H
to be affine, the choise of corners (depending on width
and height
) will acertain that H
will not distort the cropped rectangle.
optionally use cp2tform
:
H2 = cp2tform( [Xin;Yin]', [Xout;Yout]', 'nonreflectivesimilarity' );
Use the transformation to get the relevant image part
thumb = tformarray( img, maketform( 'affine', H' ), ... %//'
makeresampler( 'cubic', 'fill' ), ...
1:2, 1:2, ceil( [height width] ), [], 0 );
optionally use imtransform
:
thumb = imtransform( img, H2, 'bicubic' );
depends on how the coordinates of the corners are stored (a
...d
) the first two steps can be easily vectorize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With