How to read & display multiple images from a folder

136 views (last 30 days)
I have 20 TIFF images in MATLAB directory. How can I read them all & show them in different windows i.e. 20 windows for 20 images? I am new to MATLAB & currently using MATLAB & Simulink Release 2009a. After reading the images, how to display them one by one on screen?
  16 Comments
shobhana
shobhana on 3 Feb 2023
hi can you help me regarding MATLAB in test image i wanna run multiple images folders so how i can run group of images in the matlab
Image Analyst
Image Analyst on 3 Feb 2023
@shobhana try this:
% Process a sequence of files.
filePattern = fullfile(pwd, '*.png');
imds = imageDatastore(filePattern) % Create an image Datastore
% Get all filenames into one cell array. Filenames have the complete path (folder prepended).
allFileNames = imds.Files;
numFiles = numel(imds.Files);
for k = 1 : numel(allFileNames)
% Get this file name.
fullFileName = allFileNames{k};
fprintf('Processing %s\n', fullFileName);
% Now do something with fullFileName, such as passing it to imread.
end
If that does not work for you then try starting your own question so we can figure it out.

Sign in to comment.

Accepted Answer

Sabarinathan Vadivelu
Sabarinathan Vadivelu on 24 Dec 2012
Edited: MathWorks Support Team on 27 Nov 2018
Create a datastore by specifying the location of your image files.
location = 'E:\New Folder\*.tif'; % folder in which your images exists
ds = imageDatastore(location) % Creates a datastore for all images in your folder
Loop through the datastore, read and display each image in its own window.
while hasdata(ds)
img = read(ds) ; % read image from datastore
figure, imshow(img); % creates a new window for each image
end
For more information on reading and managing a collection of image files, see:
  26 Comments
Md. Mostafizur Rahman
Md. Mostafizur Rahman on 28 Nov 2017
Hi, I also apply this code but it can't show the image in the different window. But in the Workspace, it shows an empty array of i.
Can you tell me what's I wrong?
Bharath AS
Bharath AS on 1 Mar 2019
Moved: DGM on 20 Dec 2022
Thanks sabarinathan and matlab support team ..it helped

Sign in to comment.

More Answers (5)

Delowar Hossain
Delowar Hossain on 19 Oct 2015
Edited: Walter Roberson on 19 Oct 2015
jpgFiles = dir('*.jpg');
numFiles = length(jpgFiles);
mydata = cell(1,numFiles);
% mydata = zeros(numFiles);
for k = 1:numFiles
mydata{k} = imread(jpgFiles(k).name);
end
for k = 1:numFiles
% subplot(4,5,k);
figure;
imshow(mydata{k});
end
  9 Comments
Noor Fatima
Noor Fatima on 4 Apr 2022
I want to make a sub-plot with all the images in directory, although ypu have commented subplot line but I'm not getting the point . May I please request you that how can I plot all those image in to one plot?

Sign in to comment.


Image Analyst
Image Analyst on 24 Dec 2012
You might want to consider the montage() function in the Image Processing Toolbox.
Otherwise you can use set(handle, 'Position'...) to arrange your figures on screen.
There is also a way to "dock" figures as tabbed windows in a single figure that you can size to take up the whole screen, though I forget what the code is to dock multiple figures into one. I'm sure Jan or someone else knows though.
  11 Comments
Image Analyst
Image Analyst on 5 Jul 2016
That does not make sense to me. Like I said, remove the word "function" when you actually call the function from your main routine. The word "function" should only be where you actually define/write the function, NOT where you call it from some other function.

Sign in to comment.


murk hassan memon
murk hassan memon on 11 Feb 2018
Edited: Image Analyst on 11 Feb 2018
srcFiles = dir('E:\New Folder\IM_*.dcm');
for i = 1 : length(srcfiles)
filename = strcat('E:\New Folder\',srcFiles(i).name);
I = dicomread(filename);
figure, imshow(I);
end
i have tried this code but it is not displaying any output on screen just showing number of images and there names on workspace. kindly help me that how to read and display multiple images from folder
I have also tried this code
myfile=dir('C:\Users\Documents\*.jpg'); % the folder inwhich ur images exists
for i = 1 : length(myfile)
filename = strcat('C:\Users\Documents\',myfile(i).name);
k=importdata(filename);
end
but it displays all images on one screen so that the last image is displayed, but I want all these images on different window screen so what should I do?
Help me out please
  4 Comments
AP
AP on 9 Jun 2020
Hello sir... i have tried this code but it is giving me error. Like Reference to non-existent field 'folder'.
Please guide me through this.
Walter Roberson
Walter Roberson on 12 Jun 2020
AP, you are probably using an old MATLAB release.
folder = 'C:\Users\Documents\';
filePattern = fullfile(folder, '*.jpg');
myFiles = dir(filePattern); % the folder inwhich ur images exists
rows = ceil(sqrt(length(myFiles)))
for k = 1 : length(myFiles)
fullFileName = fullfile(folder, myFiles(k).name);
subplot(rows, rows, k);
imshow(fullFileName);
title(myFiles(k).name, 'Interpreter', 'none');
drawnow;
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);

Sign in to comment.


Shel
Shel on 4 Feb 2019
Hello,
I have 1986 tif images and now I want to read the series but I do not know how I should do would you please guide me?
How can I read my series of images?
here is the name of my files and my code:
names:
T4-1-140-0001
T4-1-140-0002
...
T4-1-140-1986
code:
clc
clear all
image_folder='C:\Users\Shel\1';
source_dir=uigetdir([]);
d=dir([source_dir,'\T4-1-140-000*.tif']);
filename=dir(fullfile(image_folder, 'T4-1-140-*.tif'));
H='result.txt';
file1=fopen(H,'w');
total_image=numel(filename);
tic
for n=1:total_image
fname=['T4-1-140-000',num2str(i), '.tif'];
inimg=imread(fullfile(source_dir, fname))
%f=fullfile(image_folder,filename(n).name);
%indicom=imread(f);
image_totpx=numel(inimg);
miangin=mean(mean(inimg));
mmax = max(inimg(:));
mmin=min(min(inimg));
img_NB=length(inimg(inimg==255));
area=img_NB
P=area/image_totpx;
fprintf(file1,' %2.0f %10s %2.5f \r\n', i, ' ', p);
end
toc
fclose(file1);
Thank you very much
  1 Comment
Image Analyst
Image Analyst on 4 Feb 2019
Change this:
for n=1:total_image
fname=['T4-1-140-000',num2str(i), '.tif'];
to this
for n=1:total_image
fname=sprintf('T4-1-140-000%4.4d.tif', n);
You need to use n instead of i since n is the loop iterator, and use 4 digits for the number, which num2str() can't do, but sprintf() can.

Sign in to comment.


sarah fteih
sarah fteih on 10 Oct 2019
Edited: DGM on 12 Feb 2023
hi
i have done this code for k means algorithm segmenation as a part of image processing:
%% K-means Segmentation (option: K Number of Segments)
clc
clear all
close all
%% Load Image
I = im2double(imread('D:\PHD\Data\ADNI\Batch Output 2019-06-13-1234\ADNI_002_S_0295_MR_Axial_PD_T2_FSE__br_raw_20060418201146219_97_S13404_I13718.jpg')); % Load Image
F = reshape(I,size(I,1)*size(I,2),3); % Color Features
%% K-means
K = 4; % Cluster Numbers
CENTS = F( ceil(rand(K,1)*size(F,1)) ,:); % Cluster Centers
DAL = zeros(size(F,1),K+2); % Distances and Labels
KMI = 10; % K-means Iteration
for n = 1:KMI
for i = 1:size(F,1)
for j = 1:K
DAL(i,j) = norm(F(i,:) - CENTS(j,:));
end
[Distance, CN] = min(DAL(i,1:K)); % 1:K are Distance from Cluster Centers 1:K
DAL(i,K+1) = CN; % K+1 is Cluster Label
DAL(i,K+2) = Distance; % K+2 is Minimum Distance
end
for i = 1:K
A = (DAL(:,K+1) == i); % Cluster K Points
CENTS(i,:) = mean(F(A,:)); % New Cluster Centers
if sum(isnan(CENTS(:))) ~= 0 % If CENTS(i,:) Is Nan Then Replace It With Random Point
NC = find(isnan(CENTS(:,1)) == 1); % Find Nan Centers
for Ind = 1:size(NC,1)
CENTS(NC(Ind),:) = F(randi(size(F,1)),:);
end
end
end
end
X = zeros(size(F));
for i = 1:K
idx = find(DAL(:,K+1) == i);
X(idx,:) = repmat(CENTS(i,:),size(idx,1),1);
end
T = reshape(X,size(I,1),size(I,2),3);
%% Show
figure()
subplot(121); imshow(I); title('original')
subplot(122); imshow(T); title('segmented')
disp('number of segments ='); disp(K)
i have more than 100 images and i want read them at this code and get the output of segmenation on new folder on my pc
please please can you help me?????

Community Treasure Hunt

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

Start Hunting!