Random image display script

7 ビュー (過去 30 日間)
Mudathir Bakhit
Mudathir Bakhit 2018 年 10 月 17 日
コメント済み: Image Analyst 2022 年 12 月 21 日
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

回答 (3 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 10 月 17 日
編集済み: KALYAN ACHARJYA 2018 年 10 月 17 日
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 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 10 月 17 日
編集済み: KALYAN ACHARJYA 2018 年 10 月 17 日
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
Image Analyst 2018 年 10 月 17 日
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 件のコメント
Manuel
Manuel 2022 年 12 月 20 日
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 2022 年 12 月 21 日
% 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
Mudathir Bakhit 2018 年 10 月 19 日
Thank you all for the answers, much appreciated.
  1 件のコメント
Image Analyst
Image Analyst 2018 年 10 月 19 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by