How to randomize image display?

조회 수: 17 (최근 30일)
Jae
Jae 2013년 1월 9일
Hello,
I was trying to load images from a folder and display them randomly. I did my best to write the codes but when I run the program it only prints "166" on the screen for non-stop 5 minutes. This is my first programming and I need your help.
The function code I wrote for loading images was: I appreciate any comments. Thank you.
function words = words_load(N)
D=dir('.../matlab/BMP/*.bmp');
a={D.name};
b=numel(a);
c=[randperm(b)];
for i=1:b
filenumber=c(:,b)
file=sprintf('w_%02d.bmp',filenumber)
img=imread(file)
end

답변 (3개)

Image Analyst
Image Analyst 2013년 1월 9일
편집: Image Analyst 2013년 1월 9일
Try it like this instead:
allFiles = dir('*.bmp');
baseFileNames = {allFiles.name}
numberOfFiles = length(baseFileNames)
randomOrder=[randperm(numberOfFiles)]
for k = 1 : numberOfFiles
filenumber = randomOrder(k)
fullFileName = fullfile(pwd, baseFileNames{filenumber})
% Display the image in the current axes.
% img = imread(fullFileName)
% imshow(img);
% Prompt user to continue or quit.
message = sprintf('Now showing %s', fullFileName);
promptMessage = sprintf('%s\nDo you want to Continue processing,\nor Cancel to abort processing?', message);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end

Jan
Jan 2013년 1월 9일
편집: Jan 2013년 1월 9일
D = dir('.../matlab/BMP/*.bmp');
FileList = {D.name};
Index = randperm(length(a));
for ii = Index
img = imread(FileList{ii});
% what should happen with the image now?
image(img);
pause(0.5);
end

Thorsten
Thorsten 2013년 1월 9일
myimagedir = '.../matlab/BMP/*.bmp';
d = dir(myimagedir);
for i = randperm(numel(d)) % show all images in random order
imshow(d(i).name)
pause(2) % wait for 2 s until the next image is shown
end

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by