Filter löschen
Filter löschen

How to present randomly 70 different images on 70 different trials from a set of 161 images which is stored in a particular folder ?

1 Ansicht (letzte 30 Tage)
I have a folder which consists of 161 images. I want to run total 70 trials and want to present different images on different trials randomly out of these 161 pictures. I have written a code, but not able to understand what is the problem. Kindly help me. I don't know much about programming
The code I wrote is ----
folder = 'F:\Ankit \Bisection_Task \Manipulable_objects'
totalimg = dir(fullfile(folder, '/*.bmp'));
as = numel(totalimg);
imageseq = randperm(as);
for i=1:70
idx = imageseq(i);
img = imread(totalimg(idx).name);
end

Akzeptierte Antwort

dpb
dpb am 14 Jan. 2023
Bearbeitet: dpb am 14 Jan. 2023
Your code works except you're not doing anything inside the loop with each image in turn; you're just reading in and overwriting the img variable every pass so when the loop ends you always have whichever was the last image data in the permuted list.
What is your intent here; show each image to a user and interact with it or do some other processing on each image in turn? Whatever it is, you need to do something with each image before going on to the next INSIDE the loop.
One refinement could be something like
Nselect=70; % use variable, don't bury "magic numbers" in code
imageseq=randperm(numel(totalimg),Nselect); % generate the permutation vector
for i=1:Nselsect
img=imread(totalimg(imageseq(i)).name);
% now do your thing with this image here, then go on to the next...
...
end
  25 Kommentare
ANKIT MAURYA
ANKIT MAURYA am 16 Jan. 2023
Hi dpb,
I tried your code but it's repeating the same image again and again on every trial.
KKindly please look the whole code that i sent to you and tell me where is the problem.
Thank You
dpb
dpb am 16 Jan. 2023
Bearbeitet: dpb am 16 Jan. 2023
You persist in not heeding advice...there's no point in my continuing to tell you the same thing over and over if you're not going to take anything I say to heart.
Since you were producing the randomized images before, then you've broken something else, not that part....and you don't have my code in there, anyway...so, look for something else that you've changed.
There's no difference in the use of
imagseq=randperm(as);
and
imagseq=randperm(as,Nselect);
excepting that the latter will have only the number of elements in it to solve your problem of wanting only the 70 elements after the loop is done instead of all 161 from the unconstrained permutation vector. Again, read the docs.
>> randperm(6) % unconstrained permutation vector
ans =
2 4 3 6 5 1
>> randperm(6,4) % select only four out of the six instead...
ans =
4 6 5 2
>>
While it's not as clean a solution, you could simply use
imagseq=randperm(as); % return the whole permutation vector
imageseq=imagseq(1:Nselect); % keep the number wanted
and be at the same place.
Again, can't urge strongly enough to quit burying magic numbers like the 70 in the code; use variables so they can be changed in one place and use the debugger to find where you've made logic errors...
Good luck...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by