Matlab 关于如何读取文件夹中的所有图片(3种方法)

来源:互联网 发布:经传主力统计指标源码 编辑:程序博客网 时间:2024/05/02 02:32

Matlab读取图片的方法有很多种, 我给出的方法思想和他们的差不多一样,但是代码的风格可能有点区别, 可以学习。

方法1:

首先定义文件夹的名称:

[cpp] view plaincopy
  1. imgDir='.\coimg\';  
  2. imgDir2='.\\coimg\\%s';  用于读取图片  


 

 具体代码:

[cpp] view plaincopy
  1. oldPwd = pwd;  
  2. cd(imgDir);  
  3. x = dir;  
  4. listOfImages = [];  
  5. for i = 1:length(x),  
  6.     if x(i).isdir == 0,  
  7.           listOfImages = [listOfImages; x(i)];  
  8.     end;  
  9. end;  
  10. cd(oldPwd);  
  11.   
  12. fid=imgDir2;  
  13. for j = 1:length(listOfImages)  
  14.     fileName = listOfImages(j).name;  
  15.     rfid=sprintf(fid,fileName);  
  16.     Irgb=imread(rfid);  
  17.     Iset{j}=Irgb;  
  18. end  

文中 x(i).isdir==0 其实意思是跳过i=1,2时,那是isdir==1,其实是为了跳过'.','..',这个应该是操作系统的知识吧。。

最后将读取的图片放在Iset里面。

代码很简单。自己手写,测试成功

两幅图片在Iset里面啦 。。

小技巧值得注意。。。

 

 

方法2:

适合文件夹里面的图片批量处理,非常好的算法,应该值得学习。。

[cpp] view plaincopy
  1. function database = build_database(rt_data_dir,suffix)  
  2. % This function is to build a database for the image sets   
  3. % Input:  rt_data_dir -- direction of image sets  
  4. %         suffix      -- image format like 'jpg'  
  5. % Output: database    -- database that contains all the information of  
  6. %                        images  
  7.   
  8. % Written by Wei Q  
  9. % July. 16, 2013  
  10.   
  11. fprintf('dir the database...');  
  12. subfolders = dir(rt_data_dir);     
  13.   
  14. database = [];  
  15.   
  16. database.imnum = 0; % total image number of the database  
  17. database.cname = {}; % name of each class  
  18. database.label = []; % label of each class  
  19. database.path = {}; % contain the pathes for each image of each class  
  20. database.nclass = 0;  
  21.   
  22. for ii = 1:length(subfolders),  
  23.     subname = subfolders(ii).name;  
  24.       
  25.     if ~strcmp(subname, '.') & ~strcmp(subname, '..'),  
  26.         database.nclass = database.nclass + 1;  
  27.           
  28.         database.cname{database.nclass} = subname;  
  29.           
  30.         frames = dir(fullfile(rt_data_dir, subname, suffix));  
  31.         c_num = length(frames);  
  32.                       
  33.         database.imnum = database.imnum + c_num;  
  34.         database.label = [database.label; ones(c_num, 1)*database.nclass];  
  35.           
  36.         for jj = 1:c_num,  
  37.             c_path = fullfile(rt_data_dir, subname, frames(jj).name);  
  38.             database.path = [database.path, c_path];  
  39.         end;      
  40.     end;  
  41. end;  
  42. disp('done!');  


应该试着自己写写。

方法3:(这种方法有点特别)

 

 

[cpp] view plaincopy
  1. ext = {'*.jpeg','*.jpg','*.png','*.pgm'};  
  2.   
  3. images = [];  
  4. for i = 1:length(ext)  
  5.     images = [images dir([path ext{i}])];  
  6. end  
  7.   
  8. % images are returned with absolute path  
  9. for i = 1:length(images)  
  10.     images(i).name = [path images(i).name];  
  11. end  
0 0
原创粉丝点击