如何在matlab中以任意角度旋转边界框?

时间:2016-09-24 02:22:18

标签: matlab image-processing

我觉得很困惑从任何角度裁剪图像,即pi / 4,我该怎么做?

1 个答案:

答案 0 :(得分:0)

以下示例演示如何裁剪旋转的边界框。

该示例未显示如何选择边界框和角度(用户界面被排除)。

为了使示例简单,边界框参数为:中心,大小和角度(而不是4个角) 旋转时,中心保持在原位 大小是目标裁剪区域的宽度和高度(旋转后的大小)。

如果您需要计算角点变换,可以使用旋转矩阵:https://en.wikipedia.org/wiki/Rotation_matrix

代码示例:

%Bounding box parameters: center, width, height and angle.
%Center is selected, because center is kept the same when rotating.
center_x = 256; %Center column index (applied center is 256.5)
center_y = 192; %Center row index (applied center is 192.5)
width = 300;  %Number of columns of destination (cropped) area (even integer)
height = 200; %Number of rows of destination (cropped) area (even integer)
phi = 120; %Rotation angle in degrees

%Read sample image.
I = imread('peppers.png');

%Add center cross, for verifying center is kept. 
I(center_y-1:center_y, center_x-30:center_x+30, :) = 255;
I(center_y-30:center_y+30, center_x-1:center_x, :) = 255;

%Rotate the entire input image, dimensions of J will be the same as I.
J = imrotate(I, phi, 'bicubic', 'crop');

%Crop rectangular are with size width by height around center_x, center_y from J.
C = J(center_y-height/2+1:center_y+height/2, center_x-width/2+1:center_x+width/2, :);

%Display result:
figure;imshow(C);

结果:
enter image description here