MATLAB: How to write the labeled images for training a sematic clssifier without using image labeler

ground truthsemantic segmentation

Hi,
I want to train a semantic classifier through images made from a simulation process, I have the ground truth classification, and therefore I don't need the image labeler app.
However, I found explanation only on how to write ground truth by the image labeler app, or by commands which add labels inside a rectangle, etc…
However, I have the ground truth image with all the labels, I just want to save it in a proper manner and traing a sematic classifier from matlab, how do I do that?
I also looked in an example ground truth data, it looks like a color image, how do I write this color image from the labels?
Please help!
Thanks,
Dr. Tal Nir

Best Answer

After labeling, you should have two sets of images. The first set will be the original images and the second set will be the categorial images with labels.
For semantic segmentation you should create datastores:
imds = imageDatastore(imgDir); %corresponding to the original images
pxds = pixelLabelDatastore(labelDir,classes,labelIDs); %corresponding to labeled images
The example link above explains this in more detail.
Note that imgDir and labelDir can simply be a cell array of the filepaths to the images, in corresponding order
For instance:
imgDir = {"~/Users/awz/img1.tif", "~/Users/awz/img2.tif", "~/Users/awz/img3.tif"};
labelDir = {"~/Users/awz/label1.tif", "~/Users/awz/label2.tif", "~/Users/awz/label3.tif"};
classes = ["class1", "class2", "class3"];
labelIDs = [1 2 3];
imds = imageDatastore(imgDir);
pxds = pixelLabelDatastore(labelDir,classes,labelIDs);
Related Question