How to extract images from groundTruth object?

12 views (last 30 days)
Rummy18
Rummy18 on 7 Feb 2018
Answered: Rummy18 on 24 Jan 2020
Hello all,
is there a way to extract (i.e. saving as separate images) all the individual objects/regions which were defined as ROI label with the Image Labeler app. and stored within a groundTruth object? The aim is to apply some batch-modifications (e.g. contrast or rotation) to these images to achieve a better result of the cascade object detector.
Thank you for your help!
  1 Comment
Joel  Borja
Joel Borja on 3 Dec 2019
I would like to find an answer to this too.

Sign in to comment.

Answers (2)

awezmm
awezmm on 3 Jan 2020
Rummy18 and Joel,
You can import the labeled images and mask at different class labels. Each element in a labeled image matrix is a number representing which class a pixel in the original image belongs to.
If you want to apply rotations or contrast for some type of learning, check out data augmentation:
  4 Comments
awezmm
awezmm on 24 Jan 2020
Is this the actual labeled image? Can you please attach the labeled image after you export after labeling.

Sign in to comment.


Rummy18
Rummy18 on 24 Jan 2020
Hi Theresa,
you can export your labels using the "export labels" function from the Image Labeler App. If you choose to export the labels directly to your Matlab workspace, select "table" as the export format. Lets call this table "plant". The first column of this table indicates the path to your images used in the Image Labeler App and the second column (plant.Stomata) (=name of the table.name of your labels) shows the coordinates of your labeled rectangles (x,y,x',y'). x' and y' are the number of pixels you have to add to x and y, respectively, to describe the corners of your labeld rectangles.
You can now apply some loops and export the labels:
% set your directory to the folder, where you want to save the individual images
cd 'path/folder';
% to find the number of pictures in your table "plant" use e.g.:
number_pictures = height(plant);
% find the number of labeled rectangles:
for c = 1:number_pictures
number_rectangles = (numel(plant.Stomata{c,1})/4);
% export the rectangles as individual images
for d = 1:number_rectangles
name = plant.imageFilename{c}; % path to the image used in the Image Labeler session
picture = imread(plant.imageFilename{c}; % imports the image into Matlab
picture_crop = imcrop(picture,[plant.Stomata{c}(d,1),plant.Stomata{c}(d,2),plant.Stomata{c}(d,3),plant.Stomata{c}(d,4)]); % crops the individual label using the coordinates
imwrite(picture_cop,[name,num2str(d),'.jpg'],'Quality',100); % saves the rectangles as .jpg image to your directory
clear name
clear picture
clear picture_crop
clear number_rectangles
end
end
Hope this helps.
Rummy18

Community Treasure Hunt

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

Start Hunting!