reading multiple images from a folder in Matlab

I want to read many images from a folder in the Matlab directory using imread() and then make different operations in every image individually , i wrote this code but it disagrees about (+k+):
num_file=1;
file = dir('image.orig');
num_file = numel(file);
NF=num_file;
Y=1;Z=1;
images = cell(1,NF,T);
T=cell(Y,Z,3);
for k = 1:NF
images{1,k}(Y,Z,3) = imread('C:Work\image.orig\'+k-1+'.JPEG');
end
also, i want to save the matrix of each image in a cell array and i don't if what i wrote is right or not and i cannot have a permission to read from the folder, i checked the folder and found that it is read only, what do you think?
Thank you in advance

 Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Nov. de 2011
file = dir('image.orig');
NF = length(file);
images = cell(NF,1);
for k = 1 : NF
images{k} = imread(fullfile('image.orig', file(k).name));
end

8 comentarios

yasmine
yasmine el 7 de Nov. de 2011
Thank you but it still gives me this error:
Can't open file "image.orig\." for reading;
you may not have read permission.
Error in ==> fg1 at 10
images{k} = imread(fullfile('image.orig', file(k).name));
Walter Roberson
Walter Roberson el 7 de Nov. de 2011
Ah yes, you took the dir of a directory and need to throw out the subdirectories...
file = dir('image.orig');
file = file(~[file.isdir]);
NF = length(file);
images = cell(NF,1);
for k = 1 : NF
images{k} = imread(fullfile('image.orig', file(k).name));
end
yasmine
yasmine el 8 de Nov. de 2011
Thank you so much for help
isn't imread supporting the JPEG format ?
so, why it gives me this error:
Unable to determine the file format.
Error in ==> fg2 at 8
images{k}=imread(fullfile('image.orig', file(k).name));
Walter Roberson
Walter Roberson el 8 de Nov. de 2011
Perhaps you have non-jpeg files in the directory as well.
You may wish to change the first line to
file = dir(fullfile('image.orig', '*.jpg'));
yasmine
yasmine el 8 de Nov. de 2011
yes i realized that not all the files are jpg
now i want to handle all image types
i tried this, is that right?
file = dir(fullfile('image.orig','*');
Thank you alot
Walter Roberson
Walter Roberson el 8 de Nov. de 2011
You cannot handle all image types: there are an infinite number of image types that imread() is not able to handle.
Using file = dir(fullfile('image.orig','*'); will not make any difference compared to file = dir('image.orig');
If you have files that imread() is not able to handle (because the file is not an image file, or because the image format details are private and hacked up or proprietary and unpublished), or because the image format is simply not one that imread() knows how to handle, then you are advised to use a try/catch block:
for K = 1 : NF
thisfile = fullfile('image.orig', file(k).name);
try
images{k} = imread(thisfile);
catch
images{k} = [];
fprintf('Unreadable file: %s\n', thisfile);
end
end
yasmine
yasmine el 10 de Nov. de 2011
This code was running the other day very good but now it gives no output and no errors , i changed the name of the folder but no good
is there anything in matlab i have to update or something, here is the code:
file = dir('image');
file = file(~[file.isdir]);
NF = length(file);
images = cell(NF,1);
for k = 1 : NF
disp(file(k).name);
images{k} = imread(fullfile('image', file(k).name));
end
Thank you
Walter Roberson
Walter Roberson el 10 de Nov. de 2011
I would put a breakpoint in and check each step -- for example, does file come out empty?
Check to be sure that you have not accidentally created your own dir.m or imread files:
which -all dir
which -all imread

Iniciar sesión para comentar.

Más respuestas (4)

yasmine
yasmine el 8 de Dic. de 2011
Would you please help me : i'm dividing an image into blocks then making some analysis on each block i divided the image using mat2cell
now i want to convert each block into a numeric array but this error appears to me:
Assignment has more non-singleton rhs dimensions than non-singleton
subscripts
Here is the code:
nb(1) = ceil(256/ b(1)); % number of blocks in each dimension
nb(2) = ceil(384/ b(2)); % number of blocks in each dimension
C=mat2cell(images,ones(256/ b(1),1)*b(1),ones(384/ b(2),1)*b(2),3);% dividing the Image into blocks.
A= size(C);
m=1;
while (m < ((nb(1)*nb(2))+1))
for i=1:nb(1)%256
for j=1:nb(2)%384
A(i,j)=cell2mat(C{i,j});% here is the error
figure(1),subplot(nb(1),nb(2),m), image(A(i,j));
m=m+1;
end
end
end

6 comentarios

Walter Roberson
Walter Roberson el 8 de Dic. de 2011
That is an unrelated matter and should have been posted to a distinct Question.
Count the number of dimensions you have provided in mat2cell: ones-this, ones-that, and 3. So each cell stored will contain a 3D array whose final dimension is 3.
When you go through the de-referencing and cell2mat, you would be coming out with a 3D array. And you try to store that full 3D array in to a single element of the numeric array A -- an array that you initialized to just contain size(C) . One has to wonder what you think you are doing?
Remember too: the result of mat2cell applied to numeric data is going to be a cell array each entry of which contains numeric data. De-referencing that cell array using {} is going to give you a numeric array. You then try to apply cell2mat() to that numeric array even though it is not a cell by that point.
cell2mat is for converting a _group_ of cell entries back in to a single array, and is not called for at all in accessing the contents of a single cell entry.
yasmine
yasmine el 8 de Dic. de 2011
ya i know cell2mat is used to convert a group of blocks into a single array but i used this:
A(i,j)=C{i,j};
and gave me this error:
Assignment has more non-singleton rhs dimensions than non-singleton
subscripts
would you explain using code example, i didn't get what you mean in your first point.
yasmine
yasmine el 9 de Dic. de 2011
i got what you mean but can't translate it into matlab code
i want to make an array of 3D arrays to store in it the values of each block,so how to declare an array of arrays in matlab?
Thank you
Walter Roberson
Walter Roberson el 9 de Dic. de 2011
There is no way to make a nested numeric array. The closest MATLAB equivalent is the cell array whose elements are numeric arrays, and you have already created that (it is your "C")
yasmine
yasmine el 9 de Dic. de 2011
yes i understand what you mean but i want to make a histogram for each block and when i did so :
for i=1:nb(1)%256
for j=1:nb(2)%384
[counts(i,j), x(i,j)] = imhist(C{i,j});
end
end
this error occurred:
??? Error using ==> iptcheckinput
Function IMHIST expected its first input, I or X, to be two-dimensional.
Error in ==> imhist>parse_inputs at 281
iptcheckinput(a,
{'double','uint8','int8','logical','uint16','int16','single','uint32', 'int32'}, ...
Error in ==> imhist at 59
[a, n, isScaled, top, map] = parse_inputs(varargin{:});
Error in ==> fll1 at 26
[counts(i,j), x(i,j)] = imhist(C{i,j});
so what do you think?
Walter Roberson
Walter Roberson el 9 de Dic. de 2011
I think this should definitely have gone in its own thread ;-)
You are trying to apply imhist() to a (subsection of) a truecolor image (which is thus a 3D matrix). imhist() is only for grayscale images. You should convert your whole image to grayscale before you break it up in to blocks; or you should convert each block to grayscale and imhist that as you go; or you should imhist() each of the three color planes separately.

Iniciar sesión para comentar.

Shaveta Arora
Shaveta Arora el 9 de Abr. de 2016

0 votos

If I have .tiff images and .tif images that I want to read, how they can be read ? I know how to read one type of images but how to read two types of images.

4 comentarios

dstats1 = dir('*.tiff');
dstats2 = dir('*.tif');
dstats = [dstats1; dstats2];
filenames = {dstats.name};
for K = 1 : length(filenames)
load_images{K} = imread(filenames{K});
end
Emily Leung
Emily Leung el 18 de Jun. de 2021
Hi this doesnt work for me, filenames comes up empty. Do you know what might be the issue?
@Emily LeungIn the code I posted here, the filenames would come up empty if you do not have any .tif or .tiff files in the current directory.
In some cases, depending upon operating system configuration, files ending with extension .TIF or .TIFF might not be treated as if they ended in .tif or .tiff -- sometimes files are case sensitive.
The code I posted is not designed to look for files in a different directory; the changes to look in a different directory are not difficult though.
Emily Leung
Emily Leung el 18 de Jun. de 2021
@Walter Roberson i solved the issue, thank you very much this approach saved me many an if loop!

Iniciar sesión para comentar.

Kumar Vaibhav
Kumar Vaibhav el 1 de Ag. de 2016
Editada: Walter Roberson el 1 de Ag. de 2016
I=imread(sprintf('C:/Users/kumar.vaibhav/Documents/MATLAB/Visually Similar Images/%d.jpeg',i));

Preguntada:

el 7 de Nov. de 2011

Comentada:

el 18 de Jun. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by