How to read & display multiple images from a folder

조회 수: 258 (최근 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?????

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by