reading multiple images from a folder in Matlab

17 visualizzazioni (ultimi 30 giorni)
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+):
file = dir('image.orig');
num_file = numel(file);
images = cell(1,NF,T);
for k = 1:NF
images{1,k}(Y,Z,3) = imread('C:Work\image.orig\'+k-1+'.JPEG');
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

Risposta accettata

Walter Roberson
Walter Roberson il 7 Nov 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));
  8 Commenti
yasmine il 10 Nov 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
images{k} = imread(fullfile('image', file(k).name));
Thank you
Walter Roberson
Walter Roberson il 10 Nov 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

Accedi per commentare.

Più risposte (4)

Image Analyst
Image Analyst il 8 Nov 2011
It's always worth throwing in a plug for the FAQ:

yasmine il 8 Dic 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
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);
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));
  6 Commenti
yasmine il 9 Dic 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});
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
{'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 il 9 Dic 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.

Accedi per commentare.

Shaveta Arora
Shaveta Arora il 9 Apr 2016
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 Commenti
Walter Roberson
Walter Roberson il 18 Giu 2021
@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 il 18 Giu 2021
@Walter Roberson i solved the issue, thank you very much this approach saved me many an if loop!

Accedi per commentare.

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


Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by