Random image display script

조회 수: 8 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by