how to crop images within a DataStore with labeled bounding boxes

15 views (last 30 days)
I have a set of labeled images created with imageLabeler (Bounding boxes, not pixel-based segmentation). The images are too large to use as-is for training and the target objects are too smal to simply resize the images so I want to crop the images and create a training set with these cropped images and the corresponding bounding boxes they contain. As reported in a previous post (https://www.mathworks.com/matlabcentral/answers/525484-augmentedimagedatastore-center-crop-does-not-return-datastore-with-labels) using augmentedImageDataStore returns only the cropped images without the bounding boxes. Using pixelLabelImageDataStore as suggested in that post does not provide a solution since I am interested in bounding boxes and not segmentic segmentation. Any suggestion?

Answers (1)

Aylin
Aylin on 2 Feb 2023
Does BoxLabelDatastore (R2019b and newer) work for you? See: https://www.mathworks.com/help/vision/ref/boxlabeldatastore.html
  3 Comments
Aylin
Aylin on 2 Feb 2023
Edited: Aylin on 2 Feb 2023
To clarify, are you trying to crop each image in the ImageDatastore using the bldsTrain data? I think you can use datastore transform to apply imcrop to each image:
function data = myTransformFcn(image, rect)
data = imcrop(image, rect); % Note, this might need to be modified slightly if image and rect are cells.
data = {data rect}; % Add the bounding boxes back to the result if you want it.
end
trainingData = transform(imdsTrain, bldsTrain, @myTransformFcn);
Note that you might need to change the myTransformFcn function depending on the box label syntax you're using.
If you're trying to generate new files out of this, you can use the writeall function for datastores: https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.writeall.html?s_tid=doc_ta
I hope this helps!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!