Effacer les filtres
Effacer les filtres

Export groundTruth as single png image

4 vues (au cours des 30 derniers jours)
Jose Agustin Barrachina
Jose Agustin Barrachina le 27 Avr 2022
Commenté : Birju Patel le 5 Mai 2022
I would like to export my labels generated using imageLabeler any of the following:
  • a png RGB image
  • a 2D matrix of ImageHeightxImageWidth with the class number at each position i, j
  • or the same matrix but in 3D as the one-hot-encoded version.
  • If not possible, at least one image per class of any of the previous ones.
Having the vertices of each polygon is not useful for me obviously. Basically I need something I can then feed into a u-net network for semantic segmentation.
Possible outcomes:
  • I saw a partial solution [here](https://fr.mathworks.com/matlabcentral/answers/381245-how-to-extract-images-from-groundtruth-object#answer_411774) but it's for rectangle labels and mines are polygons.
  • According to this there is a folder called PixelLabelData which has what I need, only that I can't find it.
  • I tried exporting gTruth to the workspace and then I tried `pxds = pixelLabelDatastore(gTruth)` as suggested here but I have the error `The value of 'gTruth' is invalid. groundTruth objects must contain a label definition table with PixelLabel type.`. According to this is something about the paths but the png paths are Ok, I moved nothing. The message seems to be the LabelDefinition Type parameter which is indeed weird. It's just a '7' as shown in the attached figure.
  • Using `[imds,pxds] = pixelLabelTrainingData(gTruth)` as suggested [here](https://fr.mathworks.com/help/vision/ref/pixellabeltrainingdata.html) has the error message of [this](https://fr.mathworks.com/matlabcentral/answers/1616790-no-pixel-label-data-found-using-the-pixellabeltrainingdata-function) question.
I can try to implement the code myself but it seems like a lot of work and something that should already be implemented somewhere, looks to me like a quite normal application. How is this done?
I am really getting mad at this... This should have been a button inside the export options... Instead I am loosing a lot of time with a simple thing. I will eventually find the solution and post it here.

Réponse acceptée

Birju Patel
Birju Patel le 5 Mai 2022
As you noticed, you will have to use pixel labels instead of polygons to get to a label matrix directly from the image or video labeler apps. The label matrix is saved in a PNG file when the label type is pixel label.
However, if you have polygon labels, you can use POLY2LABEL to convert them to label matrices that can be saved as PNG files using IMWRITE:

Plus de réponses (1)

Jose Agustin Barrachina
Jose Agustin Barrachina le 28 Avr 2022
Modifié(e) : Jose Agustin Barrachina le 28 Avr 2022
Ok, new insight, apparently the problem is I used Polygon Labels instead of Pixel Labels... If I would have used Pixel Labels then most (or all) the list of possible solutions I tried previously should have workd. Basically, a Folder should have been created with what I need...
There is no way to change it so the solution won't come from matlab... Either I implement the code myself or re-label everything (it took me 2 full working days to label everything). I will now propose a feature to transform Pixel Labels <-> Polygon Labels; Rectangular Label -> Polygon Label (evidently only one way). Etc.
Here my code solution for only one image!:
clear all
index_to_transform = 1; % Index of the image file to transform
total_classes = length(gTruth.LabelDefinitions.Name);
labels = zeros(40789, 9230);
f = waitbar(0,'Initializing code...');
for c = 1:total_classes(index_to_transform)
number_of_polygons = length(gTruth.LabelData{index_to_transform, c}{1});
for polygon_index = 1:number_of_polygons(1)
label_name = gTruth.LabelDefinitions.Name(c);
waitbar(polygon_index/number_of_polygons, f, 'Loading class ' + string(label_name));
polygon = gTruth.LabelData{index_to_transform, c}{1};
polygon = polygon{polygon_index};
xv = polygon(:, 1);
yv = polygon(:, 2);
for xq = min(xv):max(xv)
for yq = min(yv):max(yv)
if inpolygon(xq,yq,xv,yv)
labels(round(xq), round(yq)) = c;
filename = fullfile(p,f + "-labels.mat");
save(filename, 'labels')
  2 commentaires
Jose Agustin Barrachina
Jose Agustin Barrachina le 28 Avr 2022
There is an optimized version. Change the las 2 loops + the if with:
%% Create the square
x_range = min(ceil(xv)):max(floor(xv));
y_range = min(ceil(yv)):max(floor(yv));
xq = zeros(length(x_range), length(y_range));
yq = zeros(length(x_range), length(y_range));
for i = 1:length(x_range)
for j = 1:length(y_range)
xq(i, j) = x_range(i);
yq(i, j) = y_range(j);
in = inpolygon(xq, yq,xv,yv);
labels(x_range, y_range) = c * in;
Birju Patel
Birju Patel le 5 Mai 2022

Connectez-vous pour commenter.




Community Treasure Hunt

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

Start Hunting!

Translated by