Azzera filtri
Azzera filtri

read 20 images in ordered manner

2 visualizzazioni (ultimi 30 giorni)
talayeh ghodsi
talayeh ghodsi il 13 Ott 2019
Modificato: Image Analyst il 13 Ott 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
pic_3D(:,:,im) = pic(:,:,1);
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?

Risposte (2)

Walter Roberson
Walter Roberson il 13 Ott 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);
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 il 13 Ott 2019
Modificato: Image Analyst il 13 Ott 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);
thisSlice = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(thisSlice);
if numberOfColorChannels > 1
thisSlice = thisSlice(:, :, 1); % Take red channel.
if k == 1
frt_data = zeros(rows, columns, length(files), 'uint8');
frt_data(:,:, k) = thisSlice;
fprintf('Inserting slice %d (%s) into 3-D array.\n', k, baseFileName);
pic_3D = frt_data; % Another variable with the same name for some reason.

Community Treasure Hunt

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

Start Hunting!

Translated by