How to create subplot with .png images in a loop

Hey all, I have 134 .png images for each 5 trials. I'd like to create subplot of 134x5 to observe the difference in each row of the 5 trials. To check my code I've used k=1:2 (instead of 5) and i=1:5 (instead of 134) but my code creates attached plot, deletes the .png files from i=1 and doesn't create a subplot what I want. Also I've quality problem of the plots. If you're able to help I'd be appreciate. Thanks in advance.

 Accepted Answer

Try montage in image processing toolbox for display multiple images.
a = imread('onion.png');
imageArray = repmat(a, [1 1 1 3*5]);
montage(imageArray, 'size', [3 5])

8 Comments

I'm pretty new at image processing. I've tried attached code but my figure as in attached.Instead of replicating I'd like to show each image in related row and cell.
figure(1)
hold all
c{i}= imread(filename);
imshow(c{i});
imagesc([1 1], [6 6], c{i});
montage(c{i}, 'size', [5 2])
There is an example for display multiple images in a folder. doc montage. You just need to specify all image file names as a cell array to montage. The above example of replicating peppers is just for illustration as I don't have your data.
---------------------------
Create Montage from Images in Files
Create a montage from a series of images in files. Make the montage a 2-by-5 rectangle. Then, create a second montage, this time using the DisplayRange parameter to highlight structures in the image.
Display the Images as a Rectangular Montage
Create a string array containing a series of file names.
fileFolder = fullfile(matlabroot,'toolbox','images','imdata');
dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.tif'));
fileNames = string({dirOutput.name});
Display the images as a montage. Specify the shape of the montage as a 2-by-5 rectangle.
montage(fileNames, 'Size', [2 5]);
I've tried lots of ways but still get an error:
montage(fileNames, 'Size', [1 nsyll]);
Error using images.internal.getImageFromFile (line 13)
Cannot find the specified file: "Frame 0001.png".
I couldn't fix the error. If you're able to help again really appreciate it
% Do you know where you .png file is stored? pname2 is not used.
[fname2,pname2] = uigetfile('*.png','LOAD .png FILE');
% mainfolder2 is an output folder?
mainfolder2=sprintf('Movie Frames from New-Normal_M1_T%d_Mean',k);
if ~exist(mainfolder2, 'dir'); mkdir(mainfolder2); end
% You are creating output folder. What do you want to output?
fileFolder = fullfile(mainfolder,mainfolder2);
dirOutput = dir(fullfile(fileFolder,'Frame *.png'));
% fileNames should be the .png files you want to display
% if dirOutput contains the .png file names (exluding the path),
% then:
%fileNames = string({dirOutput.name});
fileNames = fullfile(pname2, string({dirOutput.name})
% Set a break point here to check if the fileNames are correct
montage(fileNames, 'Size', [1 nsyll]);
My .png files are in pname and/or fileFolder. Actually I don't know these are output folder. Sorry
This time when I run the code for k=1:5 montage
(fileNames, 'Size', [nsyll 5]); it creates so weird montage.I wish to create 1x134 plots for each run add tehm up and at the end should have 5x134. So that I can observe the differences for each row. I realise I really concentrated and think about my algorithm.Many thanks again.
You read in all 5x134 filenames first and store them in an array: fileNames(134, 5). Then get the fullfile (include the correct path). Finally
montage(fileNames(:), 'Size', [5 134]);
% Here fileNames(:) is 134*5 files in a column vector
Millon thanks, I've created cell array for fileNames and finally able to create true montage but how can I adjust the size of the images? Mines are really small,
first, try size [5 134] rather than [134 5]. That can make the montage layout in a horizontal way and make size larger. You can also use size [10 67] so that every two rows corresponding to one experiment.

Sign in to comment.

More Answers (0)

Asked:

on 25 Jul 2021

Commented:

on 27 Jul 2021

Community Treasure Hunt

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

Start Hunting!