How to set default fill value in imageDataAugmenter for categorical image

2 views (last 30 days)
I am using imageDataAugmenter for semantic segmentation, i.e. pixel classification. The setting is as the following. The ground truth pixel label images contains class label for each pixel, and these labels are made categorical by using image data store. I have two classes, ie background and foreground. The code is shown below. imgDir = './gtdata/images'; labelDir = './gtdata/labels'; imds = imageDatastore(imgDir,'ReadFcn',@customreader); classes = {'background','foreground'}; pxds = pixelLabelDatastore(labelDir,classes,[0,0,0;1,1,1]);%,'ReadFcn',@customreader); pxds.ReadFcn = @customreaderlbl;
% split data rng(0); numFiles = numel(imds.Files); shuffledIndices = randperm(numFiles);
% Use 60% of the images for training. N = round(0.60 * numFiles); trainingIdx = shuffledIndices(1:N);
% Use the rest for testing. testIdx = shuffledIndices(N+1:end);
% Create image datastores for training and test. trainingImages = imds.Files(trainingIdx); testImages = imds.Files(testIdx); imdsTrain = imageDatastore(trainingImages,'ReadFcn',@customreader); imdsTest = imageDatastore(testImages,'ReadFcn',@customreader);
% Extract class and label IDs info. classes = pxds.ClassNames; labelIDs = 1:numel(pxds.ClassNames);
trainingLabels = pxds.Files(trainingIdx); testLabels = pxds.Files(testIdx); pxdsTrain = pixelLabelDatastore(trainingLabels, classes, labelIDs); pxdsTest = pixelLabelDatastore(testLabels, classes, labelIDs); pxdsTrain.ReadFcn = @customreaderlbl; pxdsTest.ReadFcn = @customreaderlbl;
% Apply image augmentation to training set imageAugmenter = imageDataAugmenter(... 'FillValue',0,... 'RandRotation',[-90,90],... 'RandXReflection',true,... 'RandYReflection',true);
pxaugTrainimds = pixelLabelImageDatastore(imdsTrain,pxdsTrain,'DataAugmentation',imageAugmenter); pxTestimds = pixelLabelImageDatastore(imdsTest,pxdsTest,'DataAugmentation',imageAugmenter);
Note that I did random rotation in imageDataAugmenter. Some out of bounds pixel will be inserted. When I read image from pxaugTrainimds, I found that the augmented label image (not the augmented image) added a '<undefined>' category for those out of bounds pixels. However, I want them to be background class/category. So when the label image is rotated, the added pixels (out of bounds pixels) are treated as background because I don't really care about background. How can I do that? My images (not the label images) are grayscale. As said in the help page, I can only supply scalar to FillValue property. So what is a right value for one of the classes in pixel labels?
Many thanks!

Answers (1)

Christian Huggler
Christian Huggler on 27 Jan 2022
augmenter = imageDataAugmenter(...
'RandXTranslation',[-300 300],...
'RandXReflection', false,...
'RandYReflection', true,...
'RandYTranslation',[-100 100],...
'RandRotation',[-10 10],...
'RandXShear', [0 0],...
'RandScale', [0.98 1.02]...
);
pximdsTrain = pixelLabelImageDatastore(imdsTrain ,pxdsTrain, 'DataAugmentation', augmenter);
pximdsTrain = transform(pximdsTrain, @backgroundFiller, 'IncludeInfo', true);
function [data, info] = backgroundFiller(data, info)
label=data.pixelLabelImage{1};
label(isundefined(label)) = 'background';
data.pixelLabelImage{1} = label;
end

Community Treasure Hunt

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

Start Hunting!