How can I use semantic segmentation for gray scale images?

1 view (last 30 days)
Hi,
I labeled gray scale images using Pixel Label in Image Labeler app (MATLAB 2018a). I am using semantic segmentation (https://www.mathworks.com/help/vision/examples/semantic-segmentation-using-deep-learning.html) to classify different objects in the images. When I ran this part of the code (I changed the third element in the imageSize to 1 because I am using the gray scale images):
imageSize = [144 176 1];
numClasses = numel(classes);
lgraph = segnetLayers(imageSize,numClasses,'vgg16');
I got this error:
imageSize must have three elements and the third element must be 3 when creating SegNet based off of VGG-16 or VGG-19.
Error in segnetLayers (line 170)
iCheckImageSizeHasThreeElementsForVGG(args.imageSize);
Error in semanticSegmentation (line 23)
lgraph = segnetLayers(imageSize,numClasses,'vgg16');
Does SegNet support gray scale images? If yes, how can I solve the issue?
Thank you,
Abbas

Answers (1)

Andrey Gizdov
Andrey Gizdov on 27 Nov 2018
Edited: Andrey Gizdov on 27 Nov 2018
Hi,
I was having exactly the same issue as you. To solve it, I created a simple functions which checks for the number of color channels in each of image of the 'imds' ImageDatastore from your example. The function is as follows:
function [] = checkDataSize(imds)
%Check if all provided training images are in RGB format
imagesPath = [imds.Files];
for i=1:numel(imagesPath)
currentImage = readimage(imds, i);
[~, ~, colorChannels] = size(currentImage);
if colorChannels == 1 || colorChannels == 2
newImage = cat(3, currentImage, currentImage, currentImage);
imwrite(newImage, imagesPath{i});
fprintf("Found and overwrote image with a single color channel in path %s \n", imagesPath{i});
end
end
end
Probably not the best solution, but it works around the problem and did the trick for me.
Hope that helps!

Community Treasure Hunt

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

Start Hunting!