How to read & display multiple images from a folder

254 ビュー (過去 30 日間)
Prashant Kashid
Prashant Kashid 2012 年 12 月 24 日
編集済み: DGM 2023 年 2 月 12 日
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 件のコメント
shobhana
shobhana 2023 年 2 月 3 日
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 2023 年 2 月 3 日
@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.

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

採用された回答

Sabarinathan Vadivelu
Sabarinathan Vadivelu 2012 年 12 月 24 日
編集済み: MathWorks Support Team 2018 年 11 月 27 日
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 件のコメント
Md. Mostafizur Rahman
Md. Mostafizur Rahman 2017 年 11 月 28 日
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 2019 年 3 月 1 日
移動済み: DGM 2022 年 12 月 20 日
Thanks sabarinathan and matlab support team ..it helped

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

その他の回答 (5 件)

Delowar Hossain
Delowar Hossain 2015 年 10 月 19 日
編集済み: Walter Roberson 2015 年 10 月 19 日
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 件のコメント
Noor Fatima
Noor Fatima 2022 年 4 月 4 日
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?
Image Analyst
Image Analyst 2022 年 4 月 4 日
@Noor Fatima, try montage() or imtile().

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


Image Analyst
Image Analyst 2012 年 12 月 24 日
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 件のコメント
Image Analyst
Image Analyst 2016 年 7 月 5 日
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.
ayushi
ayushi 2016 年 7 月 6 日
ok sir

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


murk hassan memon
murk hassan memon 2018 年 2 月 11 日
編集済み: Image Analyst 2018 年 2 月 11 日
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 件のコメント
AP
AP 2020 年 6 月 9 日
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 2020 年 6 月 12 日
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]);

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


Shel
Shel 2019 年 2 月 4 日
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 件のコメント
Image Analyst
Image Analyst 2019 年 2 月 4 日
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.

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


sarah fteih
sarah fteih 2019 年 10 月 10 日
編集済み: DGM 2023 年 2 月 12 日
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?????
  1 件のコメント
Image Analyst
Image Analyst 2019 年 10 月 10 日
The code I gave in my comment here should work.

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

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by