MATLAB读取文件夹所有图片,按顺序
程序员文章站
2022-03-02 10:42:12
...
clc;
clear;
fileDir = 'E:\MATLAB_study\matlab_day01\dcm1\DCM\'; %图片所在文件夹
filePattern = [fileDir, '*.dcm']; %图片格式
dirOutput = dir(filePattern); %获取图片名称字符串,并按时间排序
[~, ind] = sort([dirOutput(:).datenum], 'ascend'); %ind 图片数量排序
a = dirOutput(ind);
for i=1:length(ind)
for j=1:2
name2=['E:\MATLAB_study\matlab_day01\dcm1\DCM\',a(i).name]; %按顺序改名字
pic = dicomread( name2,'frames',j);
pic = pic(99:671,1:766);
name = ['E:\MATLAB_study\matlab_day01\dcm1\image\',num2str(i),'_',num2str(j),'.png'];
imwrite(pic,name);
end
end