matlab 读取子文件夹下的所有图片
程序员文章站
2024-03-24 18:23:58
...
matlab 读取子文件夹下的所有图片 并拷贝到另外一个文件夹下面。
cifar10数据按照不同的分类存储在10个文件夹下面,要复制到同一个文件夹train_image下
filetrain = genpath('D:\CaffeInfo\B_DataCreate\cifar-10\train');
length_filetrain = size(filetrain,2);%字符串p的长度
path = {};%建立一个单元数组,数组的每个单元中包含一个目录
temp = [];
for i = 1:length_filetrain %寻找分割符';',一旦找到,则将路径temp写入path数组中
if filetrain(i) ~= ';'
temp = [temp filetrain(i)];
else
temp = [temp '\']; %在路径的最后加入 '\'
path = [path ; temp];
temp = [];
end
end
for i = 2 : length(path)
sub_path = mat2str( cell2mat( path(i) ) );
sub_path = sub_path(2:end-1);
im_train = dir(sub_path);
for j = 3:length(im_train)
[i j]
I = imread([sub_path, im_train(j).name]);
imwrite(I,['D:\CaffeInfo\B_DataCreate\train_image\', 'train', '_class-', num2str(i-2), '_num-', num2str(j-3), '.tif'])
end
end
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fileval = genpath('D:\CaffeInfo\B_DataCreate\cifar-10\val');
length_fileval = size(fileval,2);%字符串p的长度
path = {};%建立一个单元数组,数组的每个单元中包含一个目录
temp = [];
for i = 1:length_fileval %寻找分割符';',一旦找到,则将路径temp写入path数组中
if fileval(i) ~= ';'
temp = [temp fileval(i)];
else
temp = [temp '\']; %在路径的最后加入 '\'
path = [path ; temp];
temp = [];
end
end
for i = 2 : length(path)
sub_path = mat2str( cell2mat( path(i) ) );
sub_path = sub_path(2:end-1);
im_val = dir(sub_path);
for j = 3:length(im_val)
[i j]
I = imread([sub_path, im_val(j).name]);
imwrite(I,['D:\CaffeInfo\B_DataCreate\val_image\', 'val', '_class-', num2str(i-2), '_num-', num2str(j-3), '.tif'])
end
end
上一篇: Java读取文件夹下的所有文件
下一篇: 集合List根据多个字段进行排序