error while reading and selecting images randomly from multiple folders

1 view (last 30 days)
I have a folder that contains 10 subfolders each subfolder contains 8 images, I want to dynamically select 5 images from each subfolders but i got following error.
Index exceeds matrix dimensions.
Error in tt (line 18)
fn = fullfile(P,D,F{ii});
clc;
clear all;
tic;
%% Training images
id_test = 0;
id_train = 0;
P = 'ROIBintrain'; % i.e. relative/absolute path to where the subfolders are.
for ii = 1:10 % no of classes
D = sprintf('%u',ii);
S = dir(fullfile(P,D,'*.bmp')); % get all filenames in subfolder
N = numel(S);
X = randperm(N);
F = {S(X(1:5)).name}; % randomly select 5 filenames.
for jj = 1:numel(F) % no of images per class
fn = fullfile(P,D,F{ii});
im = imread(fn);
X = double(im);
X = imresize(X,[100 120],'bilinear'); %300 250
id_train = id_train+1;
traindata{id_train}=ext_vein(X,1);
traindata = traindata';
% only four minutie is taken from one image
reduced_traindata = cellfun(@(M) M(1:min(end,4), :), traindata, 'uniform', 0);
end
end
%% save data
save('db2.mat','reduced_traindata');
toc

Accepted Answer

Walter Roberson
Walter Roberson on 14 Feb 2020
F = {S(X(1:5)).name}; % randomly select 5 filenames.
That gives you 5 filenames relative to the current folder
fn = fullfile(P,D,F{ii});
That tries to index one of the five filenames according to the folder index. So if you had reached the 10th folder, you would be trying to access the 10th filename out of 5.
You are looping on jj at that point, so you should be using F{jj} not F{ii}

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!