Can someone help me to understand this error please and how to fix it? I am having a dataset of 83 images, It is divided into 5 categories, then i made a synthetic dataset that contains 83 images but with some noise and it is also divided into 5 categories, the first dataset is stored in a folder named 'processsvm' and this folder has subfolders that are named like this. "0", "1","2","3","4" , every image in every subfolder is named like this, 0, 1,2,3 etc. so what is causing this error please?
Array formation and parentheses-style indexing with objects of class 'matlab.io.datastore.ImageDatastore' is not allowed. Useobjects of class 'matlab.io.datastore.ImageDatastore' only as scalars or use a cell array.Error in HOGDigitClassificationExample (line 56)imshow(trainingSet(2).ImageLocation{3});
here is the code:
syntheticDir = fullfile('E:\sense\SVMCATA\syn-data');handwrittenDir = fullfile('E:\sense\SVMCATA\processsvm');% imageSet recursively scans the directory tree containing the images.
trainingSet = imageDatastore(syntheticDir, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');testSet = imageDatastore(handwrittenDir, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
In this example, the training set consists of 101 images for each of the 10 digits. The test set consists of 12 images per digit.
% Show training and test samples
figure;subplot(2,3,1);imshow(trainingSet(2).ImageLocation{3});subplot(2,3,2);imshow(trainingSet(4).ImageLocation{2});subplot(2,3,3);imshow(trainingSet(5).ImageLocation{4});subplot(2,3,4);imshow(testSet(2).ImageLocation{2});subplot(2,3,5);imshow(testSet(4).ImageLocation{5});subplot(2,3,6);imshow(testSet(5).ImageLocation{2});
Prior to training and testing a classifier, a pre-processing step is applied to remove noise artifacts introduced while collecting the image samples. This provides better feature vectors for training the classifier.
% Show pre-processing results
exTestImage = read(testSet(4), 5);processedImage = imbinarize(exTestImage);figure;subplot(1,2,1)imshow(exTestImage)subplot(1,2,2)imshow(processedImage)img = read(trainingSet(3), 4);% Extract HOG features and HOG visualization
[hog_2x2, vis2x2] = extractHOGFeatures(img,'CellSize',[2 2]);[hog_4x4, vis4x4] = extractHOGFeatures(img,'CellSize',[4 4]);[hog_8x8, vis8x8] = extractHOGFeatures(img,'CellSize',[8 8]);% Show the original image
figure; subplot(2,3,1:3); imshow(img);% Visualize the HOG features
subplot(2,3,4); plot(vis2x2); title({'CellSize = [2 2]'; ['Feature length = ' num2str(length(hog_2x2))]});subplot(2,3,5);plot(vis4x4); title({'CellSize = [4 4]'; ['Feature length = ' num2str(length(hog_4x4))]});subplot(2,3,6);plot(vis8x8); title({'CellSize = [8 8]'; ['Feature length = ' num2str(length(hog_8x8))]});cellSize = [4 4];hogFeatureSize = length(hog_4x4);%%Train a Digit Classifier
% Digit classification is a multiclass classification problem, where you
trainingFeatures = [];trainingLabels = [];for processsvm = 1:numel(trainingSet) numImages = trainingSet(processsvm).Count; features = zeros(numImages, hogFeatureSize, 'single'); for i = 1:numImages img = read(trainingSet(processsvm), i); % Apply pre-processing steps
img = imbinarize(img); features(i, :) = extractHOGFeatures(img, 'CellSize', cellSize); end % Use the imageSet Description as the training labels. The labels are
% the digits themselves, e.g. '0', '1', '2', etc.
labels = repmat(trainingSet(processsvm).Description, numImages, 1); trainingFeatures = [trainingFeatures; features]; %#ok<AGROW>
trainingLabels = [trainingLabels; labels ]; %#ok<AGROW>end
Next, train a classifier using the extracted features.
% fitcecoc uses SVM learners and a 'One-vs-One' encoding scheme.
classifier = fitcecoc(trainingFeatures, trainingLabels);[testFeatures, testLabels] = helperExtractHOGFeaturesFromImageSet(testSet, hogFeatureSize, cellSize);% Make class predictions using the test features.
predictedLabels = predict(classifier, testFeatures);confMat = confusionmat(testLabels, predictedLabels);helperDisplayConfusionMatrix(confMat)
Best Answer