0
votes

There are 100 jpg files in my folder called "f1"

I want to load all jpg images in folder "f1" with MATLAB and work one by one.

  1. dList = dir ('C: / f1 / *. Jpg'); To fetch all the jpgs in f1.
  2. k = length (dList); In this way.
  3. for i = 1: 1: k

But I do not know how to do it one by one

But I want to import it using imread.

What should I do then?

im = imread ('C: / f1 / *. jpg');
k = length (im);
for i = 1: 1: k
   {
    ...
   }

Can I use this?

-------------------------------------------

I get an error every time.

dList = dir ('C: / Set14 / *. BMP');
k = length (dList);
for i = 1: 1: k
im = imread ('C: \ f1 \ *. jpg' \\\\\\<- read the "k"th image!//////);

How would you like to do this?

2
Loading all images in memory is a bad idea, especially if you process them one by one. Load a single image in memory and work on it in a for-loop, you will save memory.Bentoy13
Have you ever read the documentation of imread and dir? I think you can find the solution in these pagesBentoy13
How can I get one by one? im = imread ('C: \ f1 \ *. jpg'); I get an error. How do I code it?ONION
What error do you get every time? Not knowing what you see makes it difficult to help you. Also, all your code snippets have invalid MATLAB syntax.Cris Luengo

2 Answers

2
votes

Another approach is to create a datastore of all the images and then read them 1 by 1. See the e.g. the documentation (this example is from there)

location = fullfile(matlabroot,'toolbox','matlab','demos'); %Example path
%location = 'C: / f1 /'; %Your path
ds = datastore(location,'Type','image','FileExtensions',{'.jpg'}); %Read .jpg images

for i = 1:length(ds.Files)
    data = readimage(ds,i);
    %DO something
end

The datastore has two main benefits (the third is personal):

1) it allows you to collect all images in a single variable, without having to actually read them. I find this much easier to debug, especially if the files come from multiple places.

2) Depending on what you want to return, you can combine it with mapreduce to parallelize your code

3) I personally find this notation much less confusing than having to loop over the list of files myself. But again this is a personal preference.

1
votes

dir() will return an array of structures, with separate fields for folder and file name. So you can loop through the array to load the single images one by one:

dList = dir ('C: / f1 / *. Jpg');
for k = 1 : length(dList)
    im = imread([dList(k).folder '/' dList(k).name]);
    % do your stuff
end