Clear Filters
Clear Filters

how to crop images within a DataStore with labeled bounding boxes

10 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 (2)

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.


Dominique
Dominique on 3 May 2024
Not sure if you ever sorted this out, but I just stumbled upon this question when asking a different question about a function that I believe may provide a solution to your issue:
This function will essentially randomly sample blocks of a specified size (i.e. your network input size) that contain bounding boxes from your larger labelled images that are too large to use "as is" for training. It will create a blockLocationSet that you can then use to 'extract' the randomly sampled blocks in both a blockedImageDatastore and corresponding boxLabelDatastore. It will work even if you have just a single label class. The potential drawbacks are that it performs a random sampling of your images whereas you may prefer a systematic sampling, and there seems to be a lot going on 'under the hood' that isn't explained in the documentation, hence my question:

Community Treasure Hunt

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

Start Hunting!