how to crop images within a DataStore with labeled bounding boxes

조회 수: 6 (최근 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!

댓글을 달려면 로그인하십시오.


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