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

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 Commenti

What happens if imds is empty? If no images are found?
there are images .nii format and the error is
Index exceeds the number of array elements. Index must not exceed 0
There are no .nii files being found in /MATLAB Drive/HGG/A or any subdirectory of that directory.

Accedi per commentare.

Risposte (1)

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)

Prodotti

Release

R2021b

Richiesto:

il 27 Nov 2021

Commentato:

il 29 Nov 2021

Community Treasure Hunt

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

Start Hunting!

Translated by