error during selecting dynamically image from folder

1 view (last 30 days)
Error using randi
First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN less than or
equal to IMAX.
Error in tt (line 25)
thisfile = files(randi(num_files)).name;
clc;
clear all;
tic;
%% Training images
id_test = 0;
id_train = 0;
for folder_idx = 1 : 10 % no of classes 100
for i = 1 : 8 % no of images per class 8
%thisfile = fullfile('ROITrain', num2str(folder_idx), [num2str(i) '.bmp ']);
files = dir( fullfile('ROITrain', num2str(folder_idx), [num2str(i) '.bmp ']));
num_files = numel(files);
thisfile = files(randi(num_files)).name;
%image = imread(filename);
B = imread(thisfile );
X = double(B);
X = imresize(X,[300 250],'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('db2.mat','reduced_traindata');
toc
  2 Comments
Stephen23
Stephen23 on 9 Feb 2020
Edited: Stephen23 on 9 Feb 2020
files is empty, so num_files is zero, which is not valid for randi.
But in any case it makes no sense to use dir with the actual name of a file:
dir( fullfile(..., [num2str(i) '.bmp ']));
It looks like the code mixes up the two main ways of processing multiple files:
Your code is very badly aligned. Badly aligned code is one way that beginners hide basic bugs in their code. I strongly recommend that you align your code consistently using the default MATLAB Editor settings. You can apply them to existing code by selecting all code text and pressing ctrl+i.
Balaji M. Sontakke
Balaji M. Sontakke on 9 Feb 2020
I want to randomaly select 8 images from specific folder how i can do that

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 9 Feb 2020
"I want to randomaly select 8 images from specific folder how i can do that"
Something like this:
P = 'path to the folder where the files are saved';
S = dir(fullfile(P,'*.bmp'));
N = numel(S);
X = randperm(N);
F = {S(X(1:8)).name} % cell array of 8 randomly selected filenames from that folder
  3 Comments
Stephen23
Stephen23 on 9 Feb 2020
Edited: Stephen23 on 14 Feb 2020
"no sir not cell array,"
Why not? Cell arrays are trivial to loop over.
"I need to pass one by one images to my ext_vein() function,"
I don't see why using a cell array to store filenames prevents you from doing that.
"repeat my question.."
This is not a "repeat" of your question. Take another look at your original "question": does it contain anything like the explanation you just gave now? (hint: no, it contains an error message, buggy code, and no other text whatsoever).
"I have 10 folders named 1,2,....10 each folder contains 12 images, just i want to randmoly select 8 images from 12 images and pass those images one by one to ext_vein function."
Finally, an actual explanation!
I am sure that you can adapt my intentionally simple answer to suit your needs, e.g.:
P = 'ROITrain'; % i.e. relative/absolute path to where the subfolders are.
for ii = 1:10
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:8)).name}; % randomly select 8 filenames.
for jj = 1:numel(F)
fn = fullfile(P,D,F{jj});
im = imread(fn);
...
... ext_vein(...)
end
end
Balaji M. Sontakke
Balaji M. Sontakke on 9 Feb 2020
Its simply wow!!! hats of you sir, Great and thanks

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!