1
我感到困惑,要沿任何角度來裁剪圖像,即pi/4,我該如何製作它?如何在matlab中以任意角度旋轉邊界框?
我感到困惑,要沿任何角度來裁剪圖像,即pi/4,我該如何製作它?如何在matlab中以任意角度旋轉邊界框?
以下示例演示瞭如何裁剪旋轉的邊界框。
該示例不顯示如何選擇邊界框和角度(排除用戶界面)。
爲了保持示例簡單,邊界框參數爲:中心,大小和角度(而不是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);
非常感謝。這解決了我的問題。 – David