Random image display script
16 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (3)
KALYAN ACHARJYA
il 17 Ott 2018
Modificato: KALYAN ACHARJYA
il 17 Ott 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 Commento
KALYAN ACHARJYA
il 17 Ott 2018
Modificato: KALYAN ACHARJYA
il 17 Ott 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
Image Analyst
il 17 Ott 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 Commenti
Manuel
il 20 Dic 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
il 21 Dic 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
Mudathir Bakhit
il 19 Ott 2018
1 Commento
Image Analyst
il 19 Ott 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.
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!