Random image display script

14 views (last 30 days)
Mudathir Bakhit
Mudathir Bakhit on 17 Oct 2018
Commented: Image Analyst on 21 Dec 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

Answers (3)

KALYAN ACHARJYA
KALYAN ACHARJYA on 17 Oct 2018
Edited: KALYAN ACHARJYA on 17 Oct 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 Comment
KALYAN ACHARJYA
KALYAN ACHARJYA on 17 Oct 2018
Edited: KALYAN ACHARJYA on 17 Oct 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

Sign in to comment.


Image Analyst
Image Analyst on 17 Oct 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 Comments
Manuel
Manuel on 20 Dec 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 on 21 Dec 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

Sign in to comment.


Mudathir Bakhit
Mudathir Bakhit on 19 Oct 2018
Thank you all for the answers, much appreciated.
  1 Comment
Image Analyst
Image Analyst on 19 Oct 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.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!