Hello. I have 4 images in 1 folder and I have to load them randomly. I tried to do this.
>> a=dir(['C:\Users\Desktop\Crossroad\vehicles' '/*.png'])
and then randomly load one of 4 images
im=imread(a(randi(4)).name)
but matlab gives me an error:
Error using imread (line 349)
File "white_car.PNG" does not exist.
It. wrotes that files doesnt exist, but it actually does..Does anyone know how to write it properly? Many thanks.

 채택된 답변

Image Analyst
Image Analyst 2020년 4월 12일

0 개 추천

You need to use fullfile because a(index).name does not include the folder.
folder = 'C:\Users\Desktop\Crossroad\vehicles'
fileList = dir(fullfile(folder, '/*.png'))
randomIndex = randi(length(fileList), 1, 1) % Get random number.
fullFileName = fullfile(folder, fileList(randomIndex).name)
img = imread(fullFileName);

댓글 수: 5

Thank you, sir. It helps a lot
Adam Bodis
Adam Bodis 2021년 3월 22일
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 sem_praca_bodis (line 13)
randomIndex = randi(length(fileList), 1, 1); % Get random number.
i have this type of error. Any help please?
Image Analyst
Image Analyst 2021년 3월 22일
fileList is empty and so the length of it is zero. You must give a file name pattern to dir() that will load the variable with some file information.
ANKIT MAURYA
ANKIT MAURYA 2023년 1월 14일
What if i need to present randomly different images on different trials from a set of pictures stored in a folder ?
sortOrder = randperm(numel(fileList));
unless you want to allow repeats, then use randi.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with Image Processing Toolbox에 대해 자세히 알아보기

질문:

2020년 4월 12일

댓글:

2023년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by