read 20 images in ordered manner

1 visualización (últimos 30 días)
talayeh ghodsi
talayeh ghodsi el 13 de Oct. de 2019
Editada: Image Analyst el 13 de Oct. de 2019
Hi everybody. I have 20 images named 1.png,2.png,...,20.png
I want to read them in ordered manner and save them in a matrix called pic_3D. I wrote this code:
surf_read_dir='E:\phd\zahra taati\extract only heart\h70%\';
files=dir('E:\phd\zahra taati\extract only heart\h70%\*.jpg');
for im=1:size(files)
fdir = strcat(surf_read_dir , files(im).name);
slice_im = load(fdir);
pic = imread(fdir);
for i=1:500
frt_data(im,:,i)=pic(i,:,1);
end
pic_3D(:,:,im) = pic(:,:,1);
end
end
but it doesn't read them from 1 to 20 in order and it reads them randomly. For example it saves 20.png before 5.png.
How should I correct my code?

Respuestas (2)

Walter Roberson
Walter Roberson el 13 de Oct. de 2019
Or, in your case because you know the file names are sequential, you could skip the dir() step and use
for im = 1 : 20
fdir = fullfile(surf_read_dir, sprintf('%d.png'));
pic = imread(fdir);
frt_data(im, :, :) = pic(1:500,:,1);
pic_3D(:,:,im) = pic(:,,:,1);
end
I would suggest, though, that instead of constructing frd_data at that point, that instead you construct just pic_3D in the loop, and then after the loop,
frt_data = permute( pic_3D(1:500,:,:), [2 3 1]);

Image Analyst
Image Analyst el 13 de Oct. de 2019
Editada: Image Analyst el 13 de Oct. de 2019
Part of the problem was in using a file pattern of *.jpg when you actually have *.png files. But also other errors. Try this (untested):
surf_read_dir='E:\phd\zahra taati\extract only heart\h70%\';
filePattern = fullfile(surf_read_dir, '*.png');
files = dir(filePattern)
for k = 1 : length(files) % May be 20 or whatever - this makes it not matter - it will do them all.
baseFileName = sprintf('%k.PNG', k);
fullFileName = fullfile(files(k).folder , baseFileName);
if ~isfile(fullFileName)
fprintf('%s not found. Skipping slice %d.\n', fullFileName, k);
continue;
end
thisSlice = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(thisSlice);
if numberOfColorChannels > 1
thisSlice = thisSlice(:, :, 1); % Take red channel.
end
if k == 1
frt_data = zeros(rows, columns, length(files), 'uint8');
end
frt_data(:,:, k) = thisSlice;
fprintf('Inserting slice %d (%s) into 3-D array.\n', k, baseFileName);
end
pic_3D = frt_data; % Another variable with the same name for some reason.

Categorías

Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by