1

Basically, I have a 20 subfolders in one folder with each subfolder have 100 images. Now I want to write the MATLAB code to run all of them images. I am trying to run the first subfolder to the last folder by using the loop. The first, it starts from the first image in the first subfolder to the end of these images. After that it goes to the second subfolder and so on go to the last subfolder....

Are there any suggestions for what I could do?

Thank you so much for your help!

  • 2
    Use a `dir` command once to list all of the 20 subfolders. After this, use a `for` loop to go through each of the subfolders and use another `dir` command each time to list all of your images. Go ahead and load them one at a time. Basically you'll need to chain `dir` commands together. Can you give an example of what your directory structure looks like? – rayryeng Jun 15 '14 at 15:55

3 Answers3

2

you can list the content of a folder using dir command

fldrs = dir( oneFolderName );  % list all sub folders of oneFolderName
for ii = 1:numel( fldrs )
    if fldrs(ii).name(1) == '.'
       continue; % skip '.' and '..' asuuming all other sub folders do not start with .
    end
    if ~fldrs(ii).isdir
       continue; % skip non subfolders entries
    end
    fls = dir( fullfile(oneFolderName, fldrs(ii).name, '*.jpg' ) ); % list all jpg files in subfolder
    for jj = 1:numel( fls )
        img = imread( fullfile( oneFolderName, fldrs(ii).name, fls(ii).name ) ); % read image
        % do your processing here...
    end
end 
Shai
  • 111,146
  • 38
  • 238
  • 371
  • +1 Basically the idea I had, but not in a place to write out code :) – rayryeng Jun 15 '14 at 18:06
  • Thank you very much for your comment. I have already done the code, but I have problem that the images are running by the list name as 1.jpg 10.jpg 11.jpg 12.jpg. 13. jpg. Could you tell me please how could I code as 1.jpg 2.jpg 3.jpg....and so on. Once again, thank you very much. – user3727281 Jun 15 '14 at 18:09
  • @user3727281 you can see for example [this answer](http://stackoverflow.com/a/15366423/1714410) or [this one](http://stackoverflow.com/a/14214042/1714410). – Shai Jun 16 '14 at 05:30
1

If you have the latest version of MATLAB, have a look at the image batch processor App. (It has the capability to recursively load images)

Ashish Uthama
  • 1,331
  • 1
  • 9
  • 14
0

You can also use the imageSet object in the Computer Vision System Toolbox.

Dima
  • 38,860
  • 14
  • 75
  • 115