features extraction for images with different features size in a matrix error?

I am trying to extract features from a dataset of images the problem that the features extraction algorithm gives different features size for each image, for example, the first image 1000 features and the second 2000. and it gives me an error because of that when I try to store them in a matrix. what should I do??
imds=imageDatastore('E:\dataset test','IncludeSubFolders',true,'LabelSource','foldernames');
trainingFeatures=[];
[imds_5k, imds_extra] = splitEachLabel(imds,15);
trainingLabels=imds_5k.Labels;
for i = 1:numel(imds_5k.Files) % Read images using a for loop
img = readimage(imds_5k,i);
trainingFeatures(i,:) = example(img);
end
this is the error: Unable to perform assignment because the size of the left side is 1-by-86386 and the size of the right side is 1-by-73638.

 Réponse acceptée

You can use a cell array
imds=imageDatastore('E:\dataset test','IncludeSubFolders',true,'LabelSource','foldernames');
[imds_5k, imds_extra] = splitEachLabel(imds,15);
trainingLabels=imds_5k.Labels;
trainingFeatures=cell(1,numel(imds_5k.Files));
for i = 1:numel(imds_5k.Files) % Read images using a for loop
img = readimage(imds_5k,i);
trainingFeatures{i} = example(img);
end

6 commentaires

thank you for your answer, now I can not use the data or export it as an Xls file it will export it as 1*81465, not the real data and I can not convert it into a matrix since each row have a different length??
Following code will append 0 to make all vectors of equal lengths and the create a matrix which can be written to xls file
n_max = max(cellfun(@numel, trainingFeatures));
trainingFeatures = cellfun(@(x) {[x; zeros(n_max-numel(x),1)]}, trainingFeatures);
trainingFeatures = [trainingFeatures{:}];
writematrix(trainingFeatures, 'file.xls');
it gives this error
Dimensions of arrays being concatenated are not consistent.
Error in
Untitled3>@(x){[x;zeros(n_max-numel(x),1)]}
Oh! I forgot that example() returns row vector. Try the following code
n_max = max(cellfun(@numel, trainingFeatures));
trainingFeatures = cellfun(@(x) {[x zeros(1,n_max-numel(x))]}, trainingFeatures);
trainingFeatures = vertcat(trainingFeatures{:});
writematrix(trainingFeatures, 'file.xls');

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by