pixelLabelDatastore only supports pixel label image files with uint8 data

8 views (last 30 days)
Max Walliam
Max Walliam on 17 Nov 2021
Commented: Walter Roberson on 28 Nov 2021
clc;
clear all;
volReader =@niftiread;
fpath='/MATLAB Drive/HGG';
filepath=fullfile(fpath,'A');
imageDir = fullfile(fpath,'A');
labelDir = fullfile(fpath,'A');
imds = imageDatastore(imageDir,'IncludeSubfolders',true,'FileExtensions','.nii','ReadFcn',@(x) niftiread(x));
classNames = ["background","tumor"];
pixelLabelID = [0 1];
pxds = pixelLabelDatastore(labelDir,classNames,pixelLabelID,'FileExtensions','.nii','ReadFcn',@(x) niftiread(x));
patchSize = [132 132 132];
patchPerImage = 16;
miniBatchSize = 8;
patchds = randomPatchExtractionDatastore(imds,pxds,patchSize,'PatchesPerImage',patchPerImage);
patchds.MiniBatchSize = miniBatchSize;
%% The error i am getting is
By default, pixelLabelDatastore only supports pixel label image files with uint8 data. Use ReadFcn to specify a custom read
function that returns categorical labels from non-uint8 image data.
Error in matlab.io.datastore.PixelLabelDatastore/readimage (line 584)
iErrorIfNotUint8OrLogicalImage(C);
Error in matlab.io.datastore.PixelLabelDatastore/preview (line 407)
C = readimage(this,1);
Error in randomPatchExtractionDatastore (line 325)
this.NumPixelLabelsDims = ndims(preview(this.dsSecondInternal));
Error in Buntitled (line 15)
patchds = randomPatchExtractionDatastore(imds,pxds,patchSize,'PatchesPerImage',patchPerImage);

Accepted Answer

yanqi liu
yanqi liu on 18 Nov 2021
sir,may be use the self function niftiread to make the image to uint8,such as
im = im2uint8(mat2gray(im));
  2 Comments
yanqi liu
yanqi liu on 19 Nov 2021
sir,may be use the following,such as
function data = niftiread(x)
data = imread(file);
if ndims(data) == 3
data = rgb2gray(data);
end
data = im2uint8(mat2gray(data));

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 27 Nov 2021
imageDir = fullfile(fpath,'A');
labelDir = fullfile(fpath,'A');
Your imageDir and labelDir are the same.
imds = imageDatastore(imageDir,'IncludeSubfolders',true,'FileExtensions','.nii','ReadFcn',@(x) niftiread(x));
IncludeSubfolders relative to imageDir includes imageDir itself as well as subfolders. So all .nii files in imageDir itself will be read, and some other files will be read as well too.
classNames = ["background","tumor"];
pixelLabelID = [0 1];
pxds = pixelLabelDatastore(labelDir,classNames,pixelLabelID,'FileExtensions','.nii','ReadFcn',@(x) niftiread(x));
pixelLabelDatastore() by default does not look inside of subfolders, and you did not give the IncludeSubFolders option. Because of that, only the .nii files inside labelDir itself will be looked for, and will be read using the same reading function as you used for the imageDatastore .
But labelDir and imageDir are the same folder, so all .nii files inside that folder isself are going to be examined as both image files and as label files. This is quite questionable.
By default, pixelLabelDatastore only supports pixel label image files with uint8 data. Use ReadFcn to specify a custom read
function that returns categorical labels from non-uint8 image data.
Do the .nii files directly inside the directory have image data, or do they have label data?
.nii files support a variety of data formats, and are not restricted to uint8, so without further investigation, we cannot say that your .nii files that are in the common directory are uint8 format -- though the error message is telling you that they are not.
The solution from @yanqi liu in https://www.mathworks.com/matlabcentral/answers/1588689-pixellabeldatastore-only-supports-pixel-label-image-files-with-uint8-data#answer_834539 shows you have to use a different reading function that convers to uint8 . However, I would suggest to you that the problem is that you should be using different data files for the labels.
  4 Comments
Walter Roberson
Walter Roberson on 28 Nov 2021
/MATLAB Drive/B/LabelsTest appears to contain no .mat files.
function data = matread(x)
data = matread(x);
if ndims(data) ==3
data = rgb2gray(data);
end
data = im2uint8(mat2gray(data));
end
That code is recursive; once invoked it could never end.
Also, when you load() a .mat file, and assign the output to a variable, the output will be a struct with one field for each variable in the file. You would need to extract the appropriate field from the struct in order to get the data.
Be careful with mat2gray(): it scales relative to the data minimum and data maximum, not absolute values. So if you had one image that happened to have a minimum 0 and a maximum 200, and a different one that happened to have a minimum 0 and a maximum 220, then an output value of 0.5 from mat2gray() would represent value 100 in the first case but 110 in the second case. That is important because you are using the function for labels as well as for data. Labels should be absolute values, not relative values.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!