如何分离和保存图像的一部分?

时间:2016-11-18 16:39:56

标签: matlab bounding-box

我正在尝试将图像中的每个矩形保存为单独的 jpeg图像,以便能够训练神经网络。这是我在卡车上画一个矩形的方式:

function hdl = BBox(img, samp)

I = imread(img);
hdl = imshow(img);
hold on;

s = size(samp,1);

for i=1:s

bbox = samp(i,:);

X = [bbox(3)-(bbox(5)), bbox(3)-(bbox(5)), bbox(3)-(bbox(5))+2*bbox(5), bbox(3)-(bbox(5))+2*bbox(5), bbox(3)-(bbox(5))];
Y = [bbox(4)-(bbox(6)), bbox(4)-(bbox(6))+2*bbox(6), bbox(4)-(bbox(6))+2*bbox(6), bbox(4)-(bbox(6)), bbox(4)-(bbox(6))];

Cx = bbox(3);
Cy = bbox(4);

H = [X;Y;ones(1,5)]; %// points as 3D homogeneous coordinates
Tc = [1 0 -Cx; 0 1 -Cy; 0 0 1]; %// translation as a matrix
Tr = [cosd((-bbox(7))) -sind((-bbox(7))) 0; sind((-bbox(7))) cosd((-bbox(7))) 0; 0 0 1]; %// rotation
Hr = inv(Tc) * Tr * Tc * H; %// all transformations as matrix products

plot( Hr(1,:), Hr(2,:) ); %// the rotated rect
disp(Hr);

end

我认为 imcrop 命令不适用于旋转的矩形。

如何将每个矩形保存为不同的图像?

enter image description here

1 个答案:

答案 0 :(得分:0)

我想一个简单的方法就是使用extractRotatedPatch File Exchange Submission

{ content: ' [' attr(href) ']'; }

旁注:

虽然这不是Matlab问题,但我只是想提醒你,在机器学习开始取得不错的成绩之前,你可能需要在数百万张图片上训练它。