what does this error mean?"Array formation and parentheses-style indexing with objects of class 'matlab.io​.datastore​.ImageData​store' is not allowed"

조회 수: 4 (최근 30일)
Array formation and parentheses-style indexing with objects of class 'matlab.io.datastore.ImageDatastore' is not allowed. Use objects of class 'matlab.io.datastore.ImageDatastore' only as scalars or use a cell array. Error in Untitled (line 12)
Hi, I just started using Matlab. I got the following error and I don't know what to do. I will be glad if you can help.
I have a folder Data. it has 4 subfolders(labels) containing 40 images. i am trying to do image classification using deep learning and machine learning but i am getting the above error.
code:
imdsData = imageDatastore('Data','IncludeSubfolders',true,'LabelSource','foldernames');
Error using imageDatastore
Cannot find files or folders matching: 'Data'.
[training_set, test_set] = splitEachLabel(imdsData,0.7,'randomized');
%% Create visual vocabulary
tic
bag = bagOfFeatures(training_set,'VocabularySize',30,'PointSelection','Detector');
scenedata = double(encode(bag, training_set));
toc
%% Visualize Feature Vectors
img = read(training_set(1), randi(training_set(1).Count));
featureVector = encode(bag, img);
subplot(4,2,1);
imshow(img);
bar(featureVector);
title('Visual Word Occurances'); ylabel('Frequency');
xlabel('Visual Word Index');
img = read(training_set(2), randi(training_set(2).Count));
featureVector = encode(bag, img);
subplot(4,2,3); imshow(img);
subplot(4,2,4);
bar(featureVector);title('Visual Word Occurrences');xlabel('Visual Word Index');ylabel('Frequency');
img = read(training_set(3), randi(training_set(3).Count));
featureVector = encode(bag, img);
subplot(4,2,5); imshow(img);
subplot(4,2,6);
bar(featureVector);title('Visual Word Occurrences');xlabel('Visual Word Index');ylabel('Frequency');
img = read(training_set(4), randi(training_set(4).Count));
featureVector = encode(bag, img);
subplot(4,2,7); imshow(img);
subplot(4,2,8);
bar(featureVector);title('Visual Word Occurrences');xlabel('Visual Word Index');ylabel('Frequency');
%% Create a Table using the encoded features
SceneImageData = array2table(scenedata);
sceneType = categorical(repelem({training_set.Description}', [training_set.Count], 1));
SceneImageData.sceneType = sceneType;
%% Use the new features to train a model and assess its performance using
classificationLearner
%% Can we look at Beaches and Fields and see why they are misidentified?
jj = randi(randi(training_set(ii).Count));
imshowpair(read(training_set(2),jj),read(training_set(3),jj),'montage')
title('Beaches vs. Fields');
%% Test out accuracy on test set!
testSceneData = double(encode(bag, test_set));
testSceneData = array2table(testSceneData,'VariableNames',trainedClassifier.RequiredVariables);
actualSceneType = categorical(repelem({test_set.Description}', [test_set.Count], 1));
predictedOutcome = trainedClassifier.predictFcn(testSceneData);
correctPredictions = (predictedOutcome == actualSceneType);
validationAccuracy = sum(correctPredictions)/length(predictedOutcome)
%% Visualize how the classifier works
ii = randi(size(test_set,2));
jj = randi(test_set(ii).Count);
img = read(test_set(ii),jj);
imshow(img)
% Add code here to invoke the trained classifier
imagefeatures = double(encode(bag, img));
% Find two closest matches for each feature
[bestGuess, score] = predict(trainedClassifier.ClassificationSVM,imagefeatures);
% Display the string label for img
if strcmp(char(bestGuess),test_set(ii).Description)
titleColor = [0 0.8 0];
else
titleColor = 'r';
end
title(sprintf('Best Guess: %s; Actual: %s',...
char(bestGuess),test_set(ii).Description),...
'color',titleColor)

답변 (1개)

Steven Lord
Steven Lord 2023년 5월 25일
A datastore array must be a scalar (a 1-by-1 array.) You can't make an array of them, so where you attempt to index into it MATLAB complains. It is trying to prevent you from thinking or using it like it's an array with 1 element per file.
% img = read(training_set(1), randi(training_set(1).Count));
As a simpler example demonstrating the problem:
im = imageDatastore(fullfile(matlabroot, 'toolbox', 'matlab', 'demos', '*.jpg'));
im
im =
ImageDatastore with properties: Files: { '/MATLAB/toolbox/matlab/demos/cloudCombined.jpg'; '/MATLAB/toolbox/matlab/demos/landOcean.jpg'; '/MATLAB/toolbox/matlab/demos/ngc6543a.jpg' ... and 2 more } Folders: { '/MATLAB/toolbox/matlab/demos' } AlternateFileSystemRoots: {} ReadSize: 1 Labels: {} SupportedOutputFormats: ["png" "jpg" "jpeg" "tif" "tiff"] DefaultOutputFormat: "png" ReadFcn: @readDatastoreImage
im.Files
ans = 5×1 cell array
{'/MATLAB/toolbox/matlab/demos/cloudCombined.jpg'} {'/MATLAB/toolbox/matlab/demos/landOcean.jpg' } {'/MATLAB/toolbox/matlab/demos/ngc6543a.jpg' } {'/MATLAB/toolbox/matlab/demos/street1.jpg' } {'/MATLAB/toolbox/matlab/demos/street2.jpg' }
im(1)
Array formation and parentheses-style indexing with objects of class 'matlab.io.datastore.ImageDatastore' is not allowed. Use objects of class 'matlab.io.datastore.ImageDatastore' only as scalars or use a cell array.
Eliminate your indexing on the datastore object itself. Either use the datastore by its name (as I did above when I displayed the im variable) or index into its properties (like the list of Files as I did by the im.Files command above) rather than im(1) (which as you can see threw the error you listed in your question.)

Community Treasure Hunt

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

Start Hunting!

Translated by