Random image display script

7 visualizaciones (últimos 30 días)
Mudathir Bakhit
Mudathir Bakhit el 17 de Oct. de 2018
Comentada: Image Analyst el 21 de Dic. de 2022
Can someone help me with writing a script for displaying images randomly? The images are 40 and the display duration is 3 sec for each. The images are located on the desktop in a folder named "images" The display of the 40 images is for one cycle and stop. thanks

Respuestas (3)

KALYAN ACHARJYA
KALYAN ACHARJYA el 17 de Oct. de 2018
Editada: KALYAN ACHARJYA el 17 de Oct. de 2018
full_images=dir(fullfile(pwd,'*.jpg')); % Note on your format and keep in currect directory
for i=40
%random number generation less or equal than total nos of images
random_number= randi([1 size(full_images,1)]);
image1= full_images(random_number).name;
image(imread(image1)); %display image
pause(3);
set(gcf,'Visible','off');
end
  1 comentario
KALYAN ACHARJYA
KALYAN ACHARJYA el 17 de Oct. de 2018
Editada: KALYAN ACHARJYA el 17 de Oct. de 2018
Second way
path_directory='images'; % 'Folder images should be in current directory
original_files=dir([path_directory '/*.jpg']);
for k=1:40
random_number=randi([1 40]);
filename=[path_directory '/' original_files(random_number).name];
data=imread(filename);
imshow(data);
pause(3);
set(gcf,'Visible','off');
set(gcf,'Visible','on');
end

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 17 de Oct. de 2018
If you want to show every image without repeating any image, use randperm() and the code from the FAQ:
% Specify the folder where the files live.
myFolder = pwd; %'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
% Randomize the order
sortOrder = randperm(length(theFiles));
theFiles = theFiles(sortOrder);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
  2 comentarios
Manuel
Manuel el 20 de Dic. de 2022
How can you change this so that you are able to display a sequence of images that can be repeating and longer than just the length of theFiles and without randperm?
Image Analyst
Image Analyst el 21 de Dic. de 2022
% Specify the folder where the files live.
myFolder = pwd; %'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
% Randomize the order
numFiles = length(theFiles)
numIterations = 100;
processingOrder = randi(numFiles, 1, numIterations)
for k = 1 : numIterations
index = processingOrder(k);
baseFileName = theFiles(index).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s on iteration #%d of %d\n', fullFileName, k, length(processingOrder));
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
% imageArray = imread(fullFileName);
% imshow(imageArray); % Display image.
% drawnow; % Force display to update immediately.
end

Iniciar sesión para comentar.


Mudathir Bakhit
Mudathir Bakhit el 19 de Oct. de 2018
Thank you all for the answers, much appreciated.
  1 comentario
Image Analyst
Image Analyst el 19 de Oct. de 2018
You're welcome. Just realize that the difference is that KALYAN's way shows 40 random images with possible repeats and possible images that were never shown, while mine shows all images without any repeats or missing images. Use whichever way you want.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by