How to crop and save plot square on an image using 4 points coordinates

I have a dataset with annotations for each image. when i read the annotation to the image it will look like this.
I want to crop all the boxes for each box will be a single image and save it to my dir. How i can do that if imcrop doesn't support 4 coordinate?
if true
function displaydata
% This function displays training data with annotations overlaid. Other
% datasets could be seen in the same way by changing the paths of image
% and annotation directories
uf = dir('training_data/images/*.jpg');
for i = 1000:length(uf)
dot = strfind(uf(i).name,'.');
imname = uf(i).name(1:dot-1);
load(['training_data/annotations/' imname '.mat']);
im = imread(['training_data/images/' uf(i).name]);
imshow(im);
for j = 1:length(boxes)
box = boxes{j};
line([box.a(2) box.b(2)]',[box.a(1) box.b(1)]','LineWidth',3,'Color','y');
line([box.b(2) box.c(2) box.d(2) box.a(2)]',[box.b(1) box.c(1) box.d(1) box.a(1)]','LineWidth',3,'Color','r');
disp([box.a(1), box.a(2), box.b(1), box.b(2), box.c(1), box.c(2), box.d(1), box.d(2)]);
end
disp(['Press any key to move onto the next image', imname]);pause;
end
end
the code above is how to generate the annotation file and show the bound box to the image. the box.a(1) is like x axis and the box.a(2) is y axis and so on to the others boxes. Really need some help for this to finish my assignment at college.

Réponses (1)

I = imread('peppers.png') ;
[m,n] = size(I(:,:,1)) ;
%%Four corners
data = [
277.0000 154.0000
350.0000 208.0000
286.0000 268.0000
182.0000 174.0000
];
x = data(:,1) ; y = data(:,2) ;
bI = poly2mask(x,y,n,m);
meas = regionprops(bI, 'BoundingBox');
cI = imcrop(I, meas.BoundingBox);

Catégories

En savoir plus sur Image Processing and Computer Vision dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by