Matlab图像处理常用语句(1)--批量读取文件夹内图像

来源:互联网 发布:客户特殊要求矩阵图 编辑:程序博客网 时间:2024/06/05 10:03

编程过程中发现有好多语句都比较常用,然而有时候会突然忘记怎么用,所以还是要记下来,如有何不妥,还请多多指教。

批量读取文件夹内图像

单个文件夹读取

% 选择文件夹folder_name_all = uigetdir('\input\');  % 获取该文件夹中所有png格式的图像  img_path_list = dir(strcat(folder_name_all,'\','*.png'));   % 获取图像总数量  img_num = length(img_path_list);   % 逐一读取图像  for i = 1:img_num     image_name = img_path_list(i).name;  %  图像名     Vol(:,:,i) = imread(strcat(folder_name_all,'\',image_name)); end

多个文件夹读取

p = genpath('\input\');  % 这些路径存在字符串p中,以';'分割   length_p = size(p,2);  %字符串p的长度  % 建立一个单元数组,数组的每个单元中包含一个目录  path = {};   temp = [];%寻找分割符';',一旦找到,则将路径temp写入path数组中for k = 1:length_p     if p(k) ~= ';'        temp = [temp p(k)];    else        temp = [temp '\'];        path = [path ; temp];        temp = [];    end  end clear p length_p temp;  %至此获得data文件夹及其所有子文件夹的路径,存于数组path中。file_num = size(path,1);   % 子文件夹的个数for i = 1:file_num    file_path = path{i};   % 第i个文件夹路径    img_path_list=dir(strcat(file_path,'*.png'));    img_num = length(img_path_list);       for j = 1:img_num        image_name = img_path_list(j).name;        image{j,1}=imread(strcat(file_path,image_name));    endend
0 0