matlab中,我想把一个文件夹里的所有图片都读入,如何做

2024-11-06 10:03:40
推荐回答(2个)
回答1:

这是读取两层文件夹里图片的示例。

clc;
clear;
fatherPath=['C:\Users\chen\Desktop\人脸识别\faces'];
dirs=dir(fatherPath);
dircell=struct2cell(dirs);
for i=3:length(dircell)
    subdirs=dircell(1,i);
    SonPath=[fatherPath '\' cell2mat(subdirs)];
    %SonPath=[fatherPath '\' cell2mat(subdirs) '\*.pgm'];
    dirs2=dir(SonPath);
    dircell2=struct2cell(dirs2);
    for j=3:length(dirs2)
        PictureName=dircell2(1,j);
        PicturePath=[SonPath '\' cell2mat(PictureName)];
        tempimg=imread(PicturePath);
       % imgname=[SonPath '\' cell2mat(PictureName) '.png'];
       imgname=['C:\Users\chen\Desktop\人脸识别\png\' cell2mat(PictureName) '.png'];
        imwrite(tempimg,imgname);
    end
end

回答2:

jpg格式图片是m*n*3的,你用image(:,:,u)=imread()去读取、存储图片内容,是不行的,要这样:image{u}=imread(),就是用元胞数组存储。