Index exceeds the number of array elements. Index must not exceed 0.

2 views (last 30 days)
clc;
clear all;
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;
dsTrain = transform(patchVolDS,@(x) preprocessVolumetricPatchDS(x));
minibatch = read(dsTrain)
function data = niftiread(x)
data = niftiread(x);
if ndims(data) == 3
data = rgb2gray(data);
end
data = im2uint8(mat2gray(data));
end
function batchOut = preprocessVolumetricPatchDS(batchIn)
numRows = size(batchIn,1);
batchOut = batchIn;
% 5 augmentations: nil,rot90,fliplr,flipud,rot90(fliplr)
augType = {@(x) x,@rot90,@fliplr,@flipud,@(x) rot90(fliplr(x))};
for idx = 1:numRows
img = batchIn{idx,1}{1};
resp = batchIn{idx,2}{1};
rndIdx = randi(5,1);
imgAug = augType{rndIdx}(img);
respAug = augType{rndIdx}(resp);
batchOut(idx,:) = {imgAug,respAug};
end
end
Error
Error using matlab.io.datastore.ImageDatastore/readimage (line 32)
Index exceeds the number of array elements. Index must not exceed 0.
Error in matlab.io.datastore.PixelLabelDatastore/readimage (line 574)
[C, info] = readimage(this.ImageDatastore,i);
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 Suntitled (line 14)
patchds = randomPatchExtractionDatastore(imds,pxds,patchSize,'PatchesPerImage',patchPerImage);
  3 Comments
Max Walliam
Max Walliam on 27 Nov 2021
there are images .nii format and the error is
Index exceeds the number of array elements. Index must not exceed 0
Walter Roberson
Walter Roberson on 29 Nov 2021
There are no .nii files being found in /MATLAB Drive/HGG/A or any subdirectory of that directory.

Sign in to comment.

Answers (1)

yanqi liu
yanqi liu on 29 Nov 2021
sir,may be check the data folder,such as
clc;
clear all;
fpath='/MATLAB Drive/HGG';
filepath=fullfile(fpath,'A');
imageDir = fullfile(fpath,'A');
labelDir = fullfile(fpath,'A');
fs = ls(fulfile(imageDir, '*.nii'));
disp(fs)
fs2 = ls(fulfile(labelDir, '*.nii'));
disp(fs2)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!