how to plot 120 images on same figure by using loops

Suppose i need to loop the output of 120 images and show it on a figure
But in the code below it shows on separate figures:
%Load the folder in which input images exists
myfile=dir('E:\dataset\Input_images\*.jpg');
%Counts the number of files
numFiles = length(myfile)
numRows = ceil(sqrt(numFiles))
%Load the folder in which ground truth images exists
myfile1=dir('E:\dataset\GT\*.png');
%Counts the number of files
numFiles1 = length(myfile1)
numRows1 = ceil(sqrt(numFiles1))
for k = 1 : numFiles
for j = 1 : numFiles1
thisFileName = fullfile(myfile(k).folder, myfile(k).name);
thisImage = imread(thisFileName);
grey=rgb2gray(thisImage);
thisFileName1 = fullfile(myfile1(k).folder, myfile1(k).name);
thisImage1 = imread(thisFileName1);
if (numRows == numRows1)
%Segmentation of the image
seg = imsubtract(thisImage1,grey);
figure;
subplot(2,4,k);
imshow(seg);
drawnow;
end
end
end

Answers (2)

Use
plotCount = 1
for k = 1 : numFiles
for j = 1 : numFiles1
subplot(12, 10, plotCount);
plotCount = plotCount + 1;
imshow(............
etc.
Don't use figure() anywhere in that code to create a new figure.
You're creating a new figure upon each iteration. I haven't sifted through all of your code and I'm not sure how 120 images are going to be placed on one figure. But the solution will be to move the call to figure() outside of the loops if you're only creating one figure. It looks like there will only be 8 subplots so I'm still not sure how you're organizing the 120 images on the same figure.
figure(); % <--- create figure outside of loop
for k = 1 : numFiles
for j = 1 : numFiles1
% ignoring bulk of code
if (numRows == numRows1)
%Segmentation of the image
seg = imsubtract(thisImage1,grey);
%figure; % <---- remove this
subplot(2,4,k);
imshow(seg);
drawnow;
end
end
end

4 Comments

Sir i am getting the images on the same figure but iam not getting all the images
also the positions are also not filled in the whole figure
Please run this code sir it does not work
I can't run the code because I don't have your png images. The title of your question is "how to plot 120 images on same figure by using loops" and that's what my answer does. If you'd like to do something different, you'll have to explain it better or perhaps share an illustration of what you'd like to do.
Sir i have two datasets of 120 images of thermograms and 120 images of its ground truth.
I get the ROI i.e. segmented image by subtracting ground truth from the input image as shown in the following figures:
Input thermogram:
IR_0089.jpg
Its Ground truth:
IR_0089-BIN.png
Segmented Image:
second.jpg
But i want to get all these segmented 120 images of output on the same figure and not on different figures.
I dont know how to get all these done in the same for loop using subplot
"But i want to get all these segmented 120 images of output on the same figure and not on different figures."
I assum you want them tiled and displayed in a grid (rather than overlaying them all with transparency).
Matlab's subplot function produces a grid of subplots but with large margins. Squeezing 120 subplots onto one figure will result in tiny subplots.
Instead, I recommend using this FEX submission. tight_subplot() which allows you to more easily control the margin space.
Here's an example that produces a [12 x 10] grid of subplots with vertical and horizontal spacing of 0.005 (normalized units), bottom margin on 0.01, top margin 0.1, and left & right margins 0.01.
h = tight_subplot(12, 10, [.005, .005], [.01, .1], [.01, .01]);
for i = 1:120 % or i = 1:length(h)
imshow(image, 'Parent', h(i)); %specify axis handle
end
190401 085636-Figure 1.jpg

Sign in to comment.

Products

Release

R2018b

Tags

Asked:

on 26 Mar 2019

Commented:

on 1 Apr 2019

Community Treasure Hunt

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

Start Hunting!