0

I have a folder(s80) which contain 101 sub folders and each of them contain around 400 images. I want to do some processing on them and save in new folders. I have problem with how to read them from the different folders and saving them in a new directory.

Indeed, I have below codes for doing my processing on one folder. Everything is fine if I give the directory of one specific folder but I'm not sure how to run it for all 101 folders and save them in a new directory. (code is for converting the black pixels to white and vice versa)

images = dir(fullfile('C:\data\s80\2436', '*.jpg'));

   for i=1:size(images, 1);
       PATHNAME =(images(i).name);
       imwrite(uint8(255 - double(imread(PATHNAME))),...
       fullfile('C:\data\s80\2436',[num2str(i) '.jpg']));
    end
nkjt
  • 7,825
  • 9
  • 22
  • 28
Sam
  • 457
  • 1
  • 7
  • 15

1 Answers1

1

You can list the subfolders and files of the folder 'C:\data\s80\2436' with the function 'dir' as you did for the image: http://www.mathworks.fr/fr/help/matlab/ref/dir.html

listing = dir(name)

In the structure "listing", you have a variable "isdir" which is a logical: 1 for folders, 0 otherwise. You should save all the names which are folders then with for example structfun() with 'uniform output' set to 0.

Then you could add a for loop The code might be bugging as I write out of my hat:

listing = dir('C:\data\s80')
IndfoldList = structfun(@(x) x.name(x.isdir==1), listing);
for ii = 1:length(IndfoldList)
   foldPath= (IndfoldList(ii).name);
   images = dir(fullfile(['C:\data\s80\',Pathfold], '*.jpg'));
   %Your code here
end
hyamanieu
  • 1,055
  • 9
  • 25
  • Thanks I did it with combination of your code and the getALLFiles.m from (http://stackoverflow.com/questions/2652630/how-to-get-all-files-under-a-specific-directory-in-matlab). Also I used cellfun instead of strucfun as it does not work for me. In line 5th of your code it is foldPath, which i think is miss spelling. thanks – Sam Apr 02 '14 at 13:31
  • Oh yeah sorry, I'm French and we put the adjective after the noun, I forgot to correct both lines to "anglo-saxon" fashion grammar :) . Glad I could help! – hyamanieu Apr 02 '14 at 14:19
  • Also I have found this short interesting code to get ride of hidden files. if anyone needs.... Here is a shortcut to remove them: s = dir(target); % 'target' is the investigated directory %remove hidden files s = s(arrayfun(@(x) ~strcmp(x.name(1),'.'),s)) http://stackoverflow.com/questions/5234341/how-to-filter-hidden-files-after-calling-matlabs-dir-function – Sam Apr 02 '14 at 15:05