Presenting faces at random without repetition

I need help in presenting a set of images (10 in total, at random without any repetitions. In the files, however, I have a total of 20 images and only certain specific pictures have been decided to be presented. How do I specify such criteria? Also, I tried to insert a specification where each of the pictures will be presented for 2 seconds but could not seem to figure out where shall the code be put at.
The following are the codes that I have written but it does not seem to comply. If there are any correction please do help me.
%loop start for recognition phase
%where my codes actualy START
A = imread('matlabfaces');
B = ['x','y','z'];
C = B(randi(numel(B)));
files = dir('matlabfaces'); %specify again the file location
numberoffiles = length(files); %total amount of files?
randomIndexs = randperm(numberoffiles); %randomise the files
%start loop
for j = 1:numberoffiles
thisFilename = files(randomIndexs(j)).name;
rgbimage = imread('matlabfaces');
image (rgbimage);
axis('image', 'off');
pause(3);
end
sca;

1 Kommentar

John D'Errico
John D'Errico am 19 Dez. 2022
Bearbeitet: John D'Errico am 19 Dez. 2022
Please don't keep on posting the same question. If you don't understand the answer, then say so, and ask for further explanation.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

John D'Errico
John D'Errico am 18 Dez. 2022
Bearbeitet: John D'Errico am 19 Dez. 2022
Pick the 10 images to be shown immediately, as 10 numbers from the set 1:23, WITHOUT replaceemt. randperm will do that for you. Then just display the chosen images in that sequence.
help randperm
RANDPERM Random permutation. P = RANDPERM(N) returns a vector containing a random permutation of the integers 1:N. For example, RANDPERM(6) might be [2 4 5 6 1 3]. P = RANDPERM(N,K) returns a row vector containing K unique integers selected randomly from 1:N. For example, RANDPERM(6,3) might be [4 2 5]. RANDPERM(N,K) returns a vector of K unique values. This is sometimes referred to as a K-permutation of 1:N or as sampling without replacement. To allow repeated values in the selection, sometimes referred to as sampling with replacement, use RANDI(N,1,K). RANDPERM calls RAND and therefore changes the state of the random number generator that underlies RAND, RANDI, and RANDN. Control that shared generator using RNG. See also NCHOOSEK, PERMS, RAND, RANDI, RNG. Documentation for randperm doc randperm Other uses of randperm gpuArray/randperm RandStream/randperm
So all you need is
randomIndexs = randperm(numberoffiles,10); %randomise the files
ans = 1×10
1 7 3 20 8 22 9 4 14 18
And now your for loop will start out as:
for j = 1:10
As far as the 2 second pause goes, you already have it doing a 3 second pause. That is what pause(3) should do. Perhaps you are not counting the seconds carefully when you look at it? Or perhaps, even though you say you want a 2 second pause, you really want a longer pause and do not realize it?

Weitere Antworten (0)

Kategorien

Mehr zu Images finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by