how to crop images within a DataStore with labeled bounding boxes

5 ビュー (過去 30 日間)
Raphael
Raphael 2023 年 2 月 2 日
回答済み: Dominique 2024 年 5 月 3 日
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?

回答 (2 件)

Aylin
Aylin 2023 年 2 月 2 日
Does BoxLabelDatastore (R2019b and newer) work for you? See: https://www.mathworks.com/help/vision/ref/boxlabeldatastore.html
  3 件のコメント
Aylin
Aylin 2023 年 2 月 2 日
編集済み: Aylin 2023 年 2 月 2 日
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!
Raphael
Raphael 2023 年 2 月 3 日
Thanks

サインインしてコメントする。


Dominique
Dominique 2024 年 5 月 3 日
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!

Translated by