P.C*_*.C. 7 matlab image-processing computer-vision
但是,这些边界框的边缘与x和y轴成一定角度.例如,

我想使用边界框坐标裁剪出手,然后旋转指针,使它们与x或y轴对齐.
编辑:
手的表示如下:
但是,请记住矩形不是直的.所以,我必须旋转它才能理顺它.

好的!
计算矩形的大小
width = sqrt( sum( (b-a).^2 ) );
height = sqrt( sum( (c-b).^2 ) );
Run Code Online (Sandbox Code Playgroud)
计算从a... d到正直图像的仿射变换
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
Run Code Online (Sandbox Code Playgroud)
请注意,尽管我们允许fo H为仿射,但角的选择(取决于width和height)将确保H不会扭曲裁剪的矩形.
可选使用cp2tform:
H2 = cp2tform( [Xin;Yin]', [Xout;Yout]', 'nonreflectivesimilarity' );
Run Code Online (Sandbox Code Playgroud)
使用变换获取相关图像部分
thumb = tformarray( img, maketform( 'affine', H' ), ... %//'
makeresampler( 'cubic', 'fill' ), ...
1:2, 1:2, ceil( [height width] ), [], 0 );
Run Code Online (Sandbox Code Playgroud)
可选使用imtransform:
thumb = imtransform( img, H2, 'bicubic' );
Run Code Online (Sandbox Code Playgroud)
取决于如何存储角的坐标(a... d)前两个步骤可以很容易地矢量化.
您可以使用该命令旋转图像imrotate。
您可以使用索引来裁剪图像(一旦正确旋转)。IE
subimg = img( c(1):b(1), c(2):d(2) )
Run Code Online (Sandbox Code Playgroud)
(注意,上面的行假设您已经通过命令跟踪了角点imrotate,因此 c(2) == b(2)、c(1) == d(1) 等)