how to load random image from folder

17 visualizaciones (últimos 30 días)
Miroslav Jiránek
Miroslav Jiránek el 12 de Abr. de 2020
Comentada: Image Analyst el 14 de En. de 2023
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.

Respuesta aceptada

Image Analyst
Image Analyst el 12 de Abr. de 2020
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 comentarios
ANKIT MAURYA
ANKIT MAURYA el 14 de En. de 2023
What if i need to present randomly different images on different trials from a set of pictures stored in a folder ?
Image Analyst
Image Analyst el 14 de En. de 2023
sortOrder = randperm(numel(fileList));
unless you want to allow repeats, then use randi.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with Image Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by