How to read & display multiple images from a folder
135 次查看(过去 30 天)
显示 更早的评论
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
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
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
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
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?
更多回答(5 个)
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
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
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
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.
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
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
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
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
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
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 Center 和 File Exchange 中查找有关 Image Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!