Display a set of random images in one out of 4 specific coordinates

Hello everyone,
I am actually working on a task where I want to show images in random order and individually in one of the four quadrants of the screendisplay (so 4 specific coordinates).
I can already display the images in one coordinate but I havent been able to switch from one quadrant to the other for each image.
This is the code that I have so far:
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(Matlabimagetrial, '*.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(Matlabimagetrial, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Try to show this file by showing in a specific quadrant,
for x=0:0;y=0:0;
plot(x,y);grid on;
set(gca,'XLim',[-20 20]);
set(gca,'YLim',[-20 20]);
end
hold on
data=imread(fullFileName);
image(flipud(data), 'XData', [0 20], 'YData', [0 20]); %Define the coordinates in which you want your image to appear
pause(5); % Time for which the image is displayed (we can change it as we wish)
set(gcf,'Visible','off');
set(gcf,'Visible','on');
end

5 件のコメント

Johannes Hougaard
Johannes Hougaard 2022 年 4 月 28 日
I'm not sure I fully understand your issue...but here's a guess
If you define your figure before your image and use the subplot function it might work.
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(Matlabimagetrial, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
% Randomize the order
sortOrder = randperm(length(theFiles));
theFiles = theFiles(sortOrder);
fh = figure; % Define the figure you want to use
fh.WindowState = 'maximized'; % Maximize the window to fill the entire screen
for k = 24:32
clf(fh); % If you wish to show only one image at a time and not fill all four positions
baseFileName = theFiles(k).name;
fullFileName = fullfile(Matlabimagetrial, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data=imread(fullFileName);
plotno = randperm(4,1);
ah = subplot(2,2,plotno);
try
image(ah,data);
end
ah.YAxis.Visible = 'off';
ah.XAxis.Visible = 'off';
drawnow
pause(5);
end
Johannes Hougaard
Johannes Hougaard 2022 年 4 月 28 日
Or if what you desire is to move your figure to a differente position for each image you could use
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(Matlabimagetrial, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
% Randomize the order
sortOrder = randperm(length(theFiles));
theFiles = theFiles(sortOrder);
fh = figure; % Define the figure you want to use
ah = axes(fh);
positions = {[44 525] [44 40] [1110 40] [1110 525]};
for k = 24:32
cla(ah);
baseFileName = theFiles(k).name;
fullFileName = fullfile(Matlabimagetrial, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data=imread(fullFileName);
plotno = randperm(4,1);
try % I had some images that couldn't be displayed so had to add an ugly 'try'
image(data);
end
ah.YAxis.Visible = 'off';
ah.XAxis.Visible = 'off';
fh.Position(1:2) = positions{plotno};
drawnow
pause(5);
end
Nicolas Vetter
Nicolas Vetter 2022 年 4 月 28 日
編集済み: Nicolas Vetter 2022 年 4 月 28 日
This exactly what I was trying to do.
I didn't know the plotno function.
Thanks a lot
PS I'll accept this answer if you put it as an answer, for the script where you can move the figure to a different position for each image
Johannes Hougaard
Johannes Hougaard 2022 年 4 月 29 日
plotno is not a function :)
It's just a variable I used to index randomly into the positions cell array.
I'll post as an answer.
Nicolas Vetter
Nicolas Vetter 2022 年 4 月 29 日
Sorry I meant randperm so the function associated to plotno

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

 採用された回答

Johannes Hougaard
Johannes Hougaard 2022 年 4 月 29 日
編集済み: Johannes Hougaard 2022 年 4 月 29 日

0 投票

As described in my comment this should do the trick by storing your four coordinate sets in the cell array positions and indexing using the randperm function with inputs (4,1).
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(Matlabimagetrial, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
% Randomize the order
sortOrder = randperm(length(theFiles));
theFiles = theFiles(sortOrder);
fh = figure; % Define the figure you want to use
ah = axes(fh); % Define the axes in the figure to allow handling of axis visibility in the loop
positions = {[44 525] [44 40] [1110 40] [1110 525]};
for k = 1:length(theFiles)
cla(ah); % clearing the axes
baseFileName = theFiles(k).name;
fullFileName = fullfile(Matlabimagetrial, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data=imread(fullFileName);
plotno = randperm(4,1);
image(ah,data);
ah.YAxis.Visible = 'off';
ah.XAxis.Visible = 'off';
fh.Position(1:2) = positions{plotno};
drawnow % needed to show the image in the loop.
pause(5);
end

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by